Copy link to clipboard
Copied
Hi,
Today is my InDesign Scripting Bug Day... grrr. In the following example, the Error e in line 6 is undefined. When I run the Function run() without doScript() it works as expected. It looks like, throw isn't implemented correct with doScript(). At least the throw statement works but does not carry the Error. This is not working in CS4 and CS5.5.
try {
app.doScript(run, ScriptLanguage.JAVASCRIPT , undefined, UndoModes.ENTIRE_SCRIPT, "PX_TEMP");
//~ run()
}
catch (e) {
$.writeln("Caught: " + e.message);
}
function run() {
throw new Error("Im an error");
}
Copy link to clipboard
Copied
I would not expect you to be able to throw an error across a doScript(). Did you check the return value?
Copy link to clipboard
Copied
You can perfectly throw an Error through doScript(). The Exception is raised, but the Error-Object does not arrive. A normal Runtime Exception like in the following Example works well. The problem is with the buggy implementation of throw.
try {
app.doScript(run, ScriptLanguage.JAVASCRIPT , undefined, UndoModes.ENTIRE_SCRIPT, "PX_TEMP");
//~ run()
}
catch (e) {
$.writeln("Caught: " + e.message);
}
function run() {
x = x; // this causes an Runtime Error
//~ throw new Error("Im an error");
}
Copy link to clipboard
Copied
The try...catch structure is very specific in that it creates (and destroys) at runtime its own variable scope to target the Error object. I don't know exactly how app.doScript is implemented but there are some reason to believe that the error thrown in the inner script cannot go back to the outer try...catch scope. The error object is probably lost when the inner script returns.
An ugly solution is to catch the error within app.doScript, to backup the object in a safe place and to re-throw the same error in the outer try block:
function run(){ throw new Error("Im an error"); }
run.error = null;
try {
app.doScript('try{ run(); } catch(eInner){ run.error=eInner; }');
if( run.error ){ throw run.error; }
// you can throw other errors here
// ...
}
catch(e)
{
alert( "Caught: " + e );
}
But I hope you have a really good excuse to do such a thing!
@+
Marc
Copy link to clipboard
Copied
@Marc As said in the previous post: A normal Runtime Error can go back to the outer try catch scope. But anyway: Your workaround is an interesting piece of code, but i cannot imagine an excuse for that
Copy link to clipboard
Copied
Hi, while attempting to make this script https://www.scriptopedia.org/en/component/phocadownload/file/115-copy-paste-baseline.html backward compatible with InDesign CS5.5 I also tried today some ugly things like that:
try {
app.doScript(
run,
ScriptLanguage.JAVASCRIPT ,
undefined,
UndoModes.ENTIRE_SCRIPT,
"PX_TEMP"
);
} catch (e) { alert (e || errMsg); }
function run() {
throw new Error(errMsg = "Im an error");
}
try {
app.doScript(
run,
ScriptLanguage.JAVASCRIPT ,
undefined,
UndoModes.ENTIRE_SCRIPT,
"PX_TEMP"
);
} catch (e) { alert (e && e.message ? e.message : errMsg); }
function run() {
throw new Error(errMsg = "Im an error");
}
My excuse being for my part to cancel the whole doScript action after showing temporary to the user the wrong result that he would get with with values out of range:
try {
app.doScript(
run,
ScriptLanguage.JAVASCRIPT ,
undefined,
UndoModes.ENTIRE_SCRIPT,
"PX_TEMP"
);
} catch (e) { alert (e && e.message ? e.message : errMsg); }
function run() {
//actions that give wrong results
if (!confirm ("Look at this mess. Do you want to keep that result anyway?")) {
throw new Error(errMsg = "Wise decision.");
}
alert ("As you wish.");
}
Find more inspiration, events, and resources on the new Adobe Community
Explore Now