Copy link to clipboard
Copied
1) I need to use a "session" targetengine because I a using a "window" type UI window which like the "palette" type cannot be run using the "main" target engine.
2) I want however that on the press of a button on the UI a script should be run in the "main" targetengine as it uses a lot of memory which I want dumped on conclusion of the script's execution.
3) I would like to avoid using 2 separate files one with the UI which activates the other file with the main function using the doScript command but my feeling is that this might be the only option. I would however like to know how to do this as I don't know .
The script below illustrate one of my pathetic (failed) attempts at solving the problem.
Would much appreciate help on the problem
#target "InDesign"
#targetengine "session"
target = "#target 'InDesign'";
targetengine="#targetengine 'main'";
w = new Window ("window", undefined, undefined,{ resizeable: true, borderless: false} );
myPanel = w.add ("panel");
w.add ("statictext", undefined, "main");
mb1=w.add ("button", undefined, "Press here!");
w.show();
mb1.onClick = function () {
app.doScript(target,ScriptLanguage.javascript, undefined, UndoModes.entireScript);
app.doScript(targetengine,ScriptLanguage.javascript, undefined, UndoModes.entireScript);
app.doScript(test, ScriptLanguage.javascript, undefined, UndoModes.entireScript);}
myVarm = "session target engine";
function test(){
alert("If you don't see this alert then the function is not being executed");
ww = new Window ("window", undefined, undefined,{ resizeable: true, borderless: false} );
myStatic = ww.add ("statictext", [0, 0, 700, 100], "If this stays on the screen then the target engine has not been changed to \"main!\"\r\rIf You can get this message not to stay on the screen then you are a better scripter than me\r\rThis is not a very difficult achivement !!",{multiline: true });
myStatic.justify = "center";
ww.show();
myMainVar = "Should so error if value is tried to be got from the data browser after the script has run";
}
I'm afraid you need to embed the whole code as a string in a single app.doScript (including the #targetengine directive).
Since the test function is declared in the session engine, it is unknown from the main engine, so you probably have to pass the body through func.toSource() and to call the func:
...#targetengine "mySession"
w = new Window("window", undefined, undefined, {resizeable:true, borderless:false} );
myPanel = w.add ("panel");
w.add("statictext", undefined, "main");
mb1=w.add("button", undefi
Copy link to clipboard
Copied
I'm afraid you need to embed the whole code as a string in a single app.doScript (including the #targetengine directive).
Since the test function is declared in the session engine, it is unknown from the main engine, so you probably have to pass the body through func.toSource() and to call the func:
#targetengine "mySession"
w = new Window("window", undefined, undefined, {resizeable:true, borderless:false} );
myPanel = w.add ("panel");
w.add("statictext", undefined, "main");
mb1=w.add("button", undefined, "Press here!");
mb1.onClick = function()
{
app.doScript("#targetengine 'main'\r"+test.toSource()+"();",
ScriptLanguage.javascript, undefined, UndoModes.entireScript);
}
w.show();
function test(){alert($.engineName);}
@+
Marc
Copy link to clipboard
Copied
Thanks a lot Marc, works great.
I think it would have taken me a few years to figure out the toSource
bit!
My next stage is to figure out how to transfer variables to the "main" session, which I see is discussed in the scripting guide using “script arguments”.
Copy link to clipboard
Copied
Hi Marc and all
I accidentally came across this post while searching for something and came to Indiscripts :: InDesign Scripting Forum Roundup #3 which reminded me of this post.
There is one major issue with the toSource() method, being that it fits the whole function on one line.
Let's look at the following to functions.
function test1(){
alert(1)
alert(2)
}
function test2(){
// some annotation here
alert(1)
}
These functions get reduced using the toSource() method to a single line.
(function test1(){ alert(1) alert(2) })
// Fails because of the lack of a ';' between the closing alert 1 bracket and alert 2
(function test2(){ // some comment alert(1) })
// fails because of // some comment
Using the toString() method does not reduce the function to a single line rather they are left as is.
As such in order to execute them one merely has to add the encasing brackets.
app.doScript("#targetengine 'main'\r("+test.toString()+")();", ScriptLanguage.javascript, undefined, UndoModes.entireScript);
I would of left a comment on your site but there was no option so I shall leave that for you.
Trevor
Copy link to clipboard
Copied
Find more inspiration, events, and resources on the new Adobe Community
Explore Now