how to run scripts within another scripts

Discussion of Photoshop Scripting, Photoshop Actions and Photoshop Automation in General

Moderators: Tom, Kukurykus

r4nd3r
Posts: 14
Joined: Fri Aug 05, 2016 12:29 pm

how to run scripts within another scripts

Post by r4nd3r »

Hi Friends,

I have a script that calls another script.

example:

var originalRulerUnits = preferences.rulerUnits;
preferences.rulerUnits = Units.PIXELS;

var Name = app.activeDocument.name.replace(/\.[^\.]+$/, '');
var Ext = decodeURI(app.activeDocument.name).replace(/^.*\./,'');
var SCRIPTS_FOLDER = decodeURI(app.path + '/' + localize("$$$/ScriptingSupport/InstalledScripts=Presets/Scripts"));
var Script1 = File(SCRIPTS_FOLDER + "/Skin1.jsx");
var Script2 = File(SCRIPTS_FOLDER + "/Madura 2.jsx");

if (app.activeDocument.width <= (300))

{

$.evalFile (Script1);
}

else
{


$.evalFile (Script2);
; // do nothing
}


_____________________________________________________________



but according to this site:

http://www.indiscripts.com/post/2010/04 ... nt-cs4-cs5


I can run a compiled script inside another script

but it did not work.
User avatar
Kukurykus
Posts: 528
Joined: Mon Jul 25, 2016 12:36 pm

Re: how to run scripts within another scripts

Post by Kukurykus »

I'd like to know that too! :)

Meantime you can check my solution I use to run scripts from scripts in this post: https://www.ps-scripts.com/viewtopic.ph ... 20#p152320

----------------------------------------------------------------------------------------------------------------------------------------------------


For example I had to combine two scripts I put them in one script at the end. There where 2 main conditions:

Condition ONE:
do stuff

Condition TWO:
do stuff


Problem was that between 'do stuff' of Condition One and begining of Condition Two I had to do manual work, which still had to be recorded by Photoshop. I couldn't save my operations by ScriptListner as I was during ran script (ScriptListner doesn't save anything if you run some script), so I had somehow to trick Photoshop, that I could record my operation and run the script at same time. Well my solution was to use AutoHotkey script which was set to start doing its job once after I finish that manual work between scripts (conditions of one script). The easiest way is to set in AHK script a delay (executated by still active Ps script. When Ps script finish its tasks, that additional AHK script will run desired Ps script (or other condition in the same script you used before AHK was activated).

Clear situation: you're setting Levels (colors in Photoshop). When your script Trigger them, 'one second" later its last step is to it call your AHKscript. During setting levels (few seconds from calling them, AHK calls your original Ps script. Then called Ps script gives signal to Photoshop, but a scripts CAN'T do anything as you have still opened Leveles window. So behind your present work with levels it simply waits, waits and wait so long you confirm your Levels settings. Only then (right after Levels setting were this way recorded by ScriptListener or put to HistoryLog like txt file or current file metadata to read out by RegEx) first condition of your original script as TRUE one is triggered to start what it had. Other words the process starts over :twisted:
JavierAroche
Posts: 29
Joined: Sat Jul 30, 2016 3:52 am
Location: San Francisco

Re: how to run scripts within another scripts

Post by JavierAroche »

You can execute another script with a little Action Descriptor.

Execute a script that's already in your Scripts folder, by simply using its name.

Code: Select all


function executeScript(NameOfYourScript) {
var idAdobeScriptAutomationScripts = stringIDToTypeID( "AdobeScriptAutomation Scripts" );
var desc320 = new ActionDescriptor();
var idjsNm = charIDToTypeID( "jsNm" );
desc320.putString( idjsNm, NameOfYourScript );
var idjsMs = charIDToTypeID( "jsMs" );
desc320.putString( idjsMs, "0" );
executeAction( idAdobeScriptAutomationScripts, desc320, DialogModes.NO );
}
Or, execute a script from a path

Code: Select all


function executeScript(PathToYourScript) {
var idAdobeScriptAutomationScripts = stringIDToTypeID( "AdobeScriptAutomation Scripts" );
var desc321 = new ActionDescriptor();
var idjsCt = charIDToTypeID( "jsCt" );
desc321.putPath( idjsCt, new File( PathToYourScript ) );
var idjsMs = charIDToTypeID( "jsMs" );
desc321.putString( idjsMs, "0" );
executeAction( idAdobeScriptAutomationScripts, desc321, DialogModes.NO );
}
User avatar
DavideBarranca
Posts: 16
Joined: Tue Jul 26, 2016 2:12 pm

Re: how to run scripts within another scripts

Post by DavideBarranca »

Several ways available.
You can include other files code with #include, and call their functions as you'd normally would (see doc for the #include directive at page 233 of the JavaScript Tools Guide CC.pdf)
Otherwise, if you have the path of the file you can use $.evalFile(), like

Code: Select all

$.evalFile(File('~/Desktop/untitled.jsx'))
What others have suggested holds true as well.
Hope this helps,

Davide Barranca
User avatar
Kukurykus
Posts: 528
Joined: Mon Jul 25, 2016 12:36 pm

Re: how to run scripts within another scripts

Post by Kukurykus »

It seems I misunderstood you. I thought you want to run script in the script but closing at same time a script which initiate second script.

Anyway if you'd like to do it as well, here's a code:

Script1.jsx:

Code: Select all

#target photoshop
File('~/Desktop/script2.jsx').execute()
Script2.jsx:

Code: Select all

#target photoshop
File('~/Desktop/script1.jsx').remove()
alert("Script1 ceased its activity, now only Script2 is working")

Of course if you added some code in Script1 after File('~/Desktop/script2.jsx').execute(), it would be ran too after Scripts2 additional code, because all of the following lines were taken to memory. So even if you remove Script1.jsx from Script2.jsx and then check from one of these scripts does still Script1 exist it say that not. Updated example:

Script1.jsx:

Code: Select all

function alr(a, s) {return alert((a1 = 'Alert ') + a + (a2 = ' from Script ') + s + (a3 = ': ') + tUC())}
function fle(n) {return File('~/Desktop/Script' + n + '.jsx')}
function tUC() {return String(fle(1).exists).toUpperCase()}

var bol; if (!bol) {
#target photoshop
fle(2).execute(); $.sleep(1000); alr(3, 1)
}
Script2.jsx:

Code: Select all

#target photoshop

bol = true, evl = "'#include ' + File('~/Desktop/Script1.jsx')"
eval(eval(evl)), alr(1, 2); for(var i = 1; i < 3; i++) fle([i]).remove()
try{eval(eval(evl)), alr(2, 2)} catch(err) {alert(a1 + 2 + a2 + a3 + err.name)}

You can do this all using just one script with 2 conditions (codes when executed is taken to memory, but sole script doesn't work anymore (so like with 2 scripts example) what will be proven when you want to reload data:

Script.jsx:

Code: Select all

function alr(a) {return alert((a1 = 'Alert ') + a + (a2 = ' from ' + s + ': ') + (a != 2 ? tUC() : ''))}
function f(n) {return File('~/Desktop/' + (s = 'Script') + (n || '') + (n ? '.txt' : '.jsx'))}
function T(w, c) {return "f(" + i + ")." + w + (c || "")}
function tUC() {return String(f().exists).toUpperCase()}

if (!f(1).exists) {
#target photoshop
for(var i = 1; i < 3; i++) eval(T("open('w')", ",") + T("write()", ",") + T("close()"))
f().execute(), $.sleep(1000), alr(3), f(1).remove()
}

if (f(2).exists) {
f(2).remove(), evl = "'#include ' + f()"; eval(eval(evl)), alr(1); f().remove()
try{eval(eval(evl)); alr(2)} catch(err) {alert(a1 + 2 + a2 + err.name)}
}
Attachments
Scripts.zip
(1.09 KiB) Downloaded 661 times
r4nd3r
Posts: 14
Joined: Fri Aug 05, 2016 12:29 pm

Re: how to run scripts within another scripts

Post by r4nd3r »

Sorry for the confusion, I actually want to run the code within 2 ofa single script by conditional "if".
Without the need for second script for this
r4nd3r
Posts: 14
Joined: Fri Aug 05, 2016 12:29 pm

Re: how to run scripts within another scripts

Post by r4nd3r »

I found this example web:
1- JSX compile the code and export it as binary,You get something like this:

@JSXBIN@ES@2.0@MyBbyBn0ACJAnASzDjNjTjHByBneNiIjFjMjMjPhMhAiXjPjSjMjEhBftgBbyBn0ABJCnABXzIjDjPjOjUjFjOjUjTCfXzBhQDfXzJjTjFjMjFjDjUjJjPjOEfjzDjBjQjQFfVBfyBnfABnzBifGnbyBn0ABJGnAEjzFjBjMjFjSjUHfRBVBfyBffABB40BiAABAzAIByB

2. Paste the jsxbin code as a literal string in do Script
example:
app.doScript("@JSXBIN@ES@2.0@MyBbyBn0ACJAnASzDjNjTjHByBneNiIjFjMjMjPhMhAiXjPjSjMjEhBftgBbyBn0ABJCnABXzIjDjPjOjUjFjOjUjTCfXzBhQDfXzJjTjFjMjFjDjUjJjPjOEfjzDjBjQjQFfVBfyBnfABnzBifGnbyBn0ABJGnAEjzFjBjMjFjSjUHfRBVBfyBffABB40BiAABAzAIByB");

But for some error is not running.
r4nd3r
Posts: 14
Joined: Fri Aug 05, 2016 12:29 pm

Re: how to run scripts within another scripts

Post by r4nd3r »

I got it.
thanks to everyone who helped me
I did not express myself very well because I used the google translator
see the result:


///////////////////////////
// SET-UP //
///////////////////////////

var Name = app.activeDocument.name.replace(/\.[^\.]+$/, '');
var Ext = decodeURI(app.activeDocument.name).replace(/^.*\./,'');
var Script1 =('''@JSXBIN@ES@2.0@MyBbyBnABMAbyBnABMKbyBn0AGJLnABjzOjKjQjHiTjBjWjFiPjQjUjJjPjOjTBfE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ZAiZMibbyBn0AEJicnA
BjzKjGjJjMjFiTjUjSjJjOjHgafEXzIjUjPiTjUjSjJjOjHgbfXzIjGjVjMjMiOjBjNjFgcfjzOjBjD
jUjJjWjFiEjPjDjVjNjFjOjUgdfnfnfJidnAEXzFjDjMjPjTjFgefjgdfRBXzQiEiPiOiPiUiTiBiWi
FiDiIiBiOiHiFiTgffjzLiTjBjWjFiPjQjUjJjPjOjThAfffJienABjzIjOjFjYjUiGjJjMjFhBfEjW
fRBEXzHjSjFjQjMjBjDjFhCfjgafRCYOhIicjEhLhJhIibieicjEidhLhJhEANyBnAMiebyBn0ABZif
nACLCLEjzGiOjVjNjCjFjShDfRBVzCjQhRhEfBffnndBVzCjQhShFfCnnADhE4B0AhAhF4C0AhAzDjT
jUjShG40BhAD0AzAhHCifffftnfOjAJjAnAEjzEjPjQjFjOhIfRBjhBfffAXzGjFjYjJjTjUjThJfjh
Bfn0DzVjPjQjFjOiOjFjYjUiOjVjNjFjSjJjDjBjMiGjJjMjFhKAjCDJRnABXzIjMjPjDjBjMjJjajF
hLfjzBhEhMfnctfgTbyBn0ADJWnASEyBXgdfjYfnftJZnASzLjOjPiFjYjUjFjOjTjJjPjOhNyBCzCh
dhdhOEXIfXDfVEfyBRBFeBhOffnndyBnftOgcbyhBn0ABJhBnAEXzIjEjPiBjDjUjJjPjOhPfjYfRCF
eEjTjBjWjFFeNiBjVjUjPhAiTjBjWjFhAiQiTiEffAVhNfyBJhHnAEjZfRBVEfyBffABnzBjFhQnnJj
DnAEjhKfnfAChN4B0AiAE40BiAACAhHByB''');


if (Ext.toLowerCase() != 'jpg')

{
// Run this action if the IF statement is true.
eval(Script2);
}
else
{
//Run this action if the IF statement is false
eval(Script1);
; // do nothing
}




// End
User avatar
Kukurykus
Posts: 528
Joined: Mon Jul 25, 2016 12:36 pm

Re: how to run scripts within another scripts

Post by Kukurykus »

Well then JavierAroche and DavideBarranca gave the most proper advices to run files from the same script conditionally.

By the occasion I have to learn how to uncomile binary form of .jsx, though I didn't know till now how to make binary code from normal one :lol:
It's because there wasn't File / Export as Binary... in ESTK 1.0 what I use with CS2 like I see now it's in ESTK provided with CS 5.1
Anyway when I'll need it I read it seems excellent tutorial how to do it in both directions: https://www.scip.ch/en/?labs.20140515

Do you know some other helpful websites, and could someone show the simplest way to reverse (originally 'Hello JavaScript") as an example: @JSXBIN@ES@2.0@MyBbyBn0ABJAnAFeRiIjFjMjMjPhAiKjBjWjBiTjDjSjJjQjUhB0DzABByB
r4nd3r
Posts: 14
Joined: Fri Aug 05, 2016 12:29 pm

Re: how to run scripts within another scripts

Post by r4nd3r »

Removed by Moderator
Last edited by Tom on Tue Aug 23, 2016 8:47 pm, edited 1 time in total.
Reason: Moderator Note: Reverse engineering JsxBin is forbidden, and sharing such tools is not welcome so I have removed the links!