Scripting: how to pass arguments to a script?
Copy link to clipboard
Copied
There are 2 scripts that can work both independently of each other, and together. To work together, they need to pass parameters to each other. Now I do it through app.putCustomOptions (but this is not very convenient).
Maybe it is possible to pass arguments when calling executeAction (AdobeScriptAutomation Scripts, desc) (since this is done, for example, by the Photoshop event subsystem)? I can write jsMs (javaScriptMessage) in the descriptor, but it may not be related to the arguments - I tried to put variables, objects, functions there, but the script of which I call with this jsMs does not see them in the arguments
Explore related tutorials & articles
Copy link to clipboard
Copied
Use ScriptUI the create a dialog for the script so that you can set the parameters you want the script you use. I you do not want a dialog. You can code the script as a Photoshop plug-in. Plug-in have a dialog that can be bypassed by recording the plug-in in an action. When you record the Action the Script will displaye its dialog. However, the script will record the parameters you use in its dialog into the Action step being recorded. When the action is played the script will bypass displaying its dialog and use the parameters passed by the action. If you want to read the code of a Plug-in Script look in Photoshop Presets\Sctipts folders at "Fit Image.jsx". To look at scripts that work together look at "Image Processor.jsx" It pass parameters to "Fit Image.jsx" To resize image to fits area sizes you set in "Images Processor dialog" for the various resizes you set in its dialog for output file sizes.
Copy link to clipboard
Copied
Thank! I did not look very carefully at the Image Processor code - there really is a useful way to call another script by its identifier with parameter transfer (as if it had been called from the operation panel with pre-saved parameters). This is exactly what will help me solve my problem!
Copy link to clipboard
Copied
I use objects and common code goes into a .jsxinc file that is included in multiple scripts. That way I can pass variables to the common code or to an object parameter. No messaging required.
Copy link to clipboard
Copied
Thanks for the advice! But I don’t like when there are a lot of files in a folder, besides I need to not only transfer, but also store variables - for this I use the "registry" of Photoshop and app.putCustomOptions 🙂🙂
The problem is that both scripts can work not only together, but also independently of each other. If you use a common set of variables, then some of them will change in cases where this is not necessary - this forces me to duplicate variables (a separate set of variables for self-contained work and a separate set for starting from the second script).
The method proposed by JJMack is almost ideal for my case - the variables are transferred only for a specific instance of the script and you do not need to duplicate the variables or monitor their state since the main settings in the registry will not change.
Copy link to clipboard
Copied
You can avoid namespace collisions with objects. I write data to XML so its on disk, but typically my scripts don't need to talk directly to each other.
Copy link to clipboard
Copied
Could you give an example of what parameters you would like to pass to a third-party script?
Copy link to clipboard
Copied
originally i called the second script through:
function s2t(s) {return stringIDToTypeID(s)}
function t2s(t) {return typeIDToStringID(t)}
var pth = new File ("e:/_Output/test.jsx")
var desc = new ActionDescriptor()
desc.putPath( s2t ("javaScript"), pth)
desc.putString( s2t("javaScriptMessage"), "tried to put variables, objects, functions here" )
executeAction(s2t( "AdobeScriptAutomation Scripts" ), desc)
i need to pass a number of parameters: integer, strings, booleans and an array of objects that describe the set of actions, for example structure of settings object:
function initObj (s)
{
s.autoStart = true
s.allowContinue = true
s.saveMode = 0
s.lastSavePath = ""
s.fileFormat = 0
s.flatten = false
s.jpgQuality = 12
s.override = false
s.options = []
s.doneCounter = -1
s.continue = 0
}
where s.options are:
[{active:true, set:"test set", action:"test action 1", stop: false}, {active:false, set:"test set", action:"test action 2", stop: true}, {active:true, set:"test set", action:"test action 3", stop: false}]
on advice JJMack now I call the script through
Copy link to clipboard
Copied
You can try this.
// script1.jsx
var param;
if (param == undefined) alert("Call without params")
else alert("Call with params=" + param.toSource())
// script1.jsx eof
// script2.jsx
call_script1();
function call_script1()
{
var param = new Object();
param.x = 1;
param.y = "2";
param.z = [1, "2"];
var script1_file = new File(new File($.fileName).parent.fullName + "/script1.jsx");
if (script1_file.exists) $.evalFile(script1_file.fsName);
}
// script2.jsx eof
Copy link to clipboard
Copied
I thought about this way. Essentially the same as #include, but with the difference that the path to the file is set during the execution of the function.
I just thought that if Photoshop could pass an ActionDescriptor as an argument when using events, then there is a native way to do the same 🙂
In any case, thank you all for your advice! I am sure that they will be useful not only to me.
call_script1();
function call_script1()
{
var param = new Object();
param.x = 1;
param.y = "2";
param.z = [1, "2"];
#include script1.jsx
}
Copy link to clipboard
Copied
I noticed ScriptListener creates 'javaScriptMessage' with """1""" value when in triggered code Window exists with OK button that then is pressed, and 2 when cancel button is pressed. So the same as win.show() returns.
I was not able to use those values for 'javaScriptMessage' to trigger script with clicked OK or Cancel button.
Probably it's only information for Photoshop listener, to note result of called script, but what for, I don't know.

