Activating Browser window from within an InDesign Javascript
Copy link to clipboard
Copied
Hi All
A part of a Javascript I have inherited, relies on Applescript to call to open the active Safari/Chrome window, and extract some information, then use this in the JS with InDesign.
What I would like to do is eliminate AS altogether, as this restricts me to using Macs, and would like to open this up to PC users too.
I know I can do a platform query, and may be an option to use VBScript to do the equivalent AS routine. I have absolutely no experience with VBScript, so would need some help if this is the option.
Ideally I would like to stay as native JS, but that may be wishful thinking on my part.
Here is a sample of the AS I am using within a doScript:
tell application "Safari"
activate
set imagePath to do JavaScript "frames[2].frames[1].document.links[0].href" in document 1
end tell
then...
tell application "InDesign"
activate
end tell
Does anyone know if this may be possible?
Cheers
Roy
Copy link to clipboard
Copied
I tried with fetch.
Is this usage correct?
#target "indesign"
if (parseFloat(BridgeTalk.getSpecifier("indesign", -100).split("-")[1]) < 18) {
alert("InDesign with UXP is not installed.");
exit();
}
if(!BridgeTalk.isRunning(BridgeTalk.getSpecifier("indesign", -100))) {
BridgeTalk.launch(BridgeTalk.getSpecifier("indesign", -100), "background");
}
var myBridgeTalk = new BridgeTalk;
myBridgeTalk.target = BridgeTalk.getSpecifier("indesign", -100);
myBridgeTalk.body = """
if (app.documents.count() == 0) {
app.documents.add(); // It doesn't seem to work without the document.
app.activeWindow.minimize();
}
app.scriptArgs.setValue("response", "");
app.doScript(\"\"\"
await (async () => {
return new Promise((resolve) => {
try {
fetch("https://reqres.in/api/users/2")
.then((response) => { return response.text(); })
.then((text) => { app.scriptArgs.setValue("response", text); })
.finally(() => { resolve(); });
} catch (e) {
app.scriptArgs.setValue("response", "error");
resolve(e);
}
})
})();
\"\"\", ScriptLanguage.UXPSCRIPT);
for (var i = 0; i < 100; i++) {
if (app.scriptArgs.getValue("response") != "") {
break;
} else { $.sleep(100); }
}
app.scriptArgs.getValue("response"); // Return value
""";
myBridgeTalk.onResult = function(result) {
alert(result.body);
}
myBridgeTalk.onError = function(error) {
alert("Error: " + error.body);
}
myBridgeTalk.send(10000); // If not supplied or 0, the message is sent asynchronously, and the function returns immediately without waiting for a result.
Copy link to clipboard
Copied
It's my understanding that one can't mix ExtendScript and UXP Scripting. Staying open-minded, I tried your code and my InDesign kindly crashed. Plus point was to open the browser from within an InDesign Script. Yours would want to call a web service if I am not mistaken.
Copy link to clipboard
Copied
If Document is not open in InDesign2023, it may not work. I modified the script slightly.
I think it is possible to mix extendScript and UXPScript by using ScriptLanguage.UXPSCRIPT in doScript.
From InDesign 2023.
Execution video
Copy link to clipboard
Copied
It seems that the return value of doScript cannot be obtained successfully if ScriptLanguage.UXPSCRIPT is specified in doScript.
// ExtendScriptSource
var returnValue = app.doScript("10;");
alert('app.doScript("10;");' + "\r" + returnValue);
returnValue = app.doScript("10;", ScriptLanguage.UXPSCRIPT);
alert('app.doScript("10;", ScriptLanguage.UXPSCRIPT);' + "\r" + returnValue);
Therefore, Label is used to exchange doScript values.
Copy link to clipboard
Copied
It seems that the return value of doScript cannot be obtained successfully if ScriptLanguage.UXPSCRIPT is specified in doScript.
By 琥珀 猫太郎
Yes, indeed, you cannot (unless something changed that I am not aware of) mix jsx and uxp scripting. They are really separate mixtures. You may want to use UXP Scripting from the beginning and possibly enjoy fetch there but you won't be able to get ES6 code to be run with the ExtendScript Script Engine. Unless I am wrong.
Copy link to clipboard
Copied
I think I can call UXP using doScript from InDesign2023's ExtendScript (.jsx), but if the language is UXP, I may not be able to get the return value of doScript.
Example of using UXP with doScript from InDesign2023 ExtendScript (.jsx).
#script "InDesign2023 ExtendScriptSource(.jsx) Using UXP with doScript"
#target "InDesign-18"
app.doScript("""
function alert(msg) {
theDialog = app.dialogs.add();
col = theDialog.dialogColumns.add();
colText = col.staticTexts.add();
colText.staticLabel = "" + msg;
theDialog.canCancel = false;
theDialog.show();
theDialog.destroy();
return;
}
alert(require('uxp').versions.uxp);
""", ScriptLanguage.UXPSCRIPT);
Copy link to clipboard
Copied
I was able to pass values from doScript using scriptArgs.
Example of using UXP with doScript from InDesign2023 ExtendScript (.jsx).
Get the clipboard value.
#script "InDesign2023 ExtendScriptSource(.jsx) Using UXP with doScript"
#target "InDesign-18"
if(!Window.confirm("Get the clipboard value.")){ exit(); }
app.scriptArgs.setValue("Clipboard", "");
app.doScript("""
await (async () => {
return new Promise((resolve) => {
try {
navigator.clipboard.readText()
.then((result) => {
app.scriptArgs.setValue("Clipboard", result["text/plain"]);
})
.finally(() => { resolve(); });
} catch (e) { resolve(e); }
})
})();
""", ScriptLanguage.UXPSCRIPT);
$.sleep(1000);
alert(app.scriptArgs.getValue("Clipboard"));
Copy link to clipboard
Copied
I think that your UXP code just fades in the limbs (or in other words, doScript call doesn't throw error or code is just not processed at all).
For ex:
this idjs code will create a doc (as it would in jsx):
addDoc.idjs
app.documents.add();
But if try your logic with a jsx script and doScript:
useUXDoScriptWithJSX.jsx
app.doScript("app.documents.add()", ScriptLanguage.UXPSCRIPT);
Nothing happens.
I really hope I am wrong here because it could be interesting to have this ability to mix both syntax (better more than less). However, not only we have been told many times jsx and idjs could'nt work altogether but the UXPScripting DOM may be subject to lacks for a while and doScript is an example of a method that is partially implemented. There was a recent post in the forum where Adobe confirmed doScript in UXP was dysfunctional.
Once again, hope I am wrong.
Loci
Copy link to clipboard
Copied
No problem in my environment.
18.1 x64 windows
Execution video
Copy link to clipboard
Copied
I was going to write that you were right as after updating InDesign, this line in a jsx worked:
app.doScript("app.documents.add()", ScriptLanguage.UXPSCRIPT);
But suddenly I had a doubt, what if this jsx compliant code was executed because "ScriptLanguage.UXPSCRIPT" was just discarded? So I went to give it another try with a more advanced ES6 syntax like this (in the jsx):
app.doScript("let arr = ['a','b','c']; arr.forEach( value=> app.documents.add().textFrames.add({contents:value}));", ScriptLanguage.UXPSCRIPT );
And this time, I get no results (while the sole idjs with the same code works fine).
Can you try it?
I will ask Adobe for this in the meantime.
Loic
Copy link to clipboard
Copied
There seems to be no ) after add({contents:value}).
ExtendScript seems to be able to use here-documents, so it might be easier to see if you use them.
app.doScript("""
let arr = ['a','b','c'];
arr.forEach((value) => {
app.documents.add().textFrames.add({ contents:value });
});
""", ScriptLanguage.UXPSCRIPT );
Reference site (Japanese)
Copy link to clipboard
Copied
UXP in general and app.doScript() in particular is known to have problems. Adobe is working on this but for the time being UXP is mainly good for experiments and testing.
Copy link to clipboard
Copied
Yes, the parenthesis was missing (copy/paste typo), thanks for pointing this out. Still, no execution anyway.
Copy link to clipboard
Copied
my environment
18.1 x64 windows
Since it can be executed without problems, there may be a problem depending on the execution environment.
Copy link to clipboard
Copied
I you can get it to work, yes that probably is. ID 18.1 on my Mac 12.5 it just doesn't.


-
- 1
- 2