-/** * Module to dialog between the apps of the Creative Suite. <br> * This module allow you to execute a function in Photoshop from InDesign * @namespace Gateway * @memberOf H * @author Bastien Eichenberger */H.Gateway = (function (my) { /** * Function to execute a script in an other software of the Creative Suite * @todo fix the bug when the user insert a comment in parameter the function_to_execute * @function call_app * @memberOf H.Gateway * @param {String} app the name of the application [photoshop, illustrator, indesign, … ] * @param {Function} function_to_execute the function to process * @param {Array} args an array of arguments * @param {Number} timeout the timeout in millisecond * @returns {*} the value returned by the function passed as parameter or null * @throws {Error} if an error occurred in the function passed as parameter or if the time exceeded without result * @example * * With parameters * H.Gateway.call_app( * 'photoshop', * function(a, b) { * alert(a + b); * }, * ['hello', 'world'] * ); * * Without parameters * H.Gateway.call_app( * 'photoshop', * function() { * alert('function without parameter'); * } * ); */ my.call_app = function (app, function_to_execute, args, timeout) { var error_message; var result; var complete; var bt = new BridgeTalk(); bt.target = app; if (args) { /** * build a string representation of the function * (function hello(a, b) * my code; * }).apply(this, []) */ bt.body = function_to_execute.toSource() + '.apply(this, ' + args.toSource() + ');'; } else { bt.body = function_to_execute.toSource() + '();'; } bt.onError = function (ex) { error_message = ex.body; }; bt.onResult = function (response) { result = response.body; complete = true; }; bt.send(timeout); // we have to wait the response of the other software // if an error occurred throw it if (error_message) { throw { name: 'Error', message: error_message, fileName: $.fileName, lineNumber: $.line }; } // if the onResult function isn't complete, the timeout exceeded if (!complete) { throw { name: 'TimeoutError', message: 'exceeded timeout of ' + timeout + ' seconds', fileName: $.fileName, lineNumber: $.line }; } // else return the result or null return result || null; } /** * Function to launch photoshop * @param {string} app the name of the application to launch * @function open * @memberOf PS.Application * @throws {Error} throw an Error if Photoshop is not installed */ my.open_app = function (app) { var specifier = BridgeTalk.getSpecifier(app); if (specifier === null) { throw { name: 'Error', message: 'The application ' + app + ' is not correctly installed', fileName: $.fileName, lineNumber: $.line }; } if (!BridgeTalk.isRunning(app)) { BridgeTalk.launch(app); } } return my;})(H.Gateway || {});
0 commit comments