Help with InDesign JavaScript for Switch

Post Reply
RichardRonnback
Newbie
Posts: 2
Joined: Mon Mar 10, 2025 3:18 pm

Help with InDesign JavaScript for Switch

Post by RichardRonnback »

I have written an InDesign JavaScript that I cannot get to successfully run in a Switch Flow

Tthe error message only states that "Object is invalid" and refers to a line in the script (which cannot possibly be the offending line so the line counting is probably offset)

The core of the script is rather simple, and only translates file paths from Mac to Windows (mixed environment) and then relinks to the appropriate platform.

The file server is Windows, and the Swith server is directly connected to it, same as the Windows clients.

The Script works when run on InDesign, both Mac and Windows, and also runs on the Switch Server, if triggered manually in the InDesign Application that is installed there.

The script also is created to that it can run in windowless mode, if triggered by "doScript", but I cannot get it to run at all in Switch, and I would really appreciate if someone could give some hints to what may be going on.

It is rather a lengthy script so I don't post the entire thing, just the part that actually tries to do anything:

Code: Select all

			// Defines the new file path
			var $myNewFilePath = '' ;
			if ($myOS == "MAC") {
				var $myNewFilePath = $myConfig['MAC'] + '/' + $myEndOfFilePath ;
			} else {
				if ($preferUNC == 'true') {
					var $myNewFilePath = $myConfig['WIN_UNC'] + '/' + $myEndOfFilePath ;
				} else if ($preferUNC == 'false') {
					var $myNewFilePath = $myConfig['WIN_DRIVELETTER'] + '/' + $myEndOfFilePath ;
				}
			}
			
			// Tries to relink to new file.
			var $myStatus = 'false' ;
			try {
				if (File ($myNewFilePath).exists == true) {
					var $myNewFile = new File ($myNewFilePath);
					$myCurrentLink.relink($myNewFile);
					var $myStatus = 'true' ;
					$mySuccessCount++ ;
				} else {
					$myFailureCount++ ;
				}
			} catch (error) {
				var $myStatus = 'false' ;
				$myFailureCount++ ;
			}
loicaigon
Advanced member
Posts: 560
Joined: Wed Jul 10, 2013 10:22 am

Re: Help with InDesign JavaScript for Switch

Post by loicaigon »

Hey there,

In InDesign Javascript, the message "Object is invalid" mostly means that the object you are trying to read a prop from or un a method on is not valid. A typical example is trying to access app.activeDocument if no documents are open.

In the snippet you pasted, it's hard to state if your problem is located here. Only candidate could be:
$myCurrentLink.relink($myNewFile);
Because $myCurrentLink might not be a valid Link Object.

If it works inside InDesign and not in Switch, it's likely that you are misusing a reference in Switch. For example, are you referencing the open doc using :
$doc

Something I used to do in the past was uisng a log function

function log(msg){
var f = new File (Folder.desktop+"/log.txt");
f.open('a);
f.writeln(msg);
f.close();
}

then use log("whatever") at regular steps and see what are your value and where the script stops working.

You can also look at the messages pane (debug), the path of the compiled script. Then open the folder, run the flow and copy the script for the brief moment it will be there. Then you can try to execute that script and see what typically goes wrong.

More info here:
https://www.enfocus.com/Manuals/UserGui ... tions.html
Loïc Aigon
Enfocus PitStop Manager
RichardRonnback
Newbie
Posts: 2
Joined: Mon Mar 10, 2025 3:18 pm

Re: Help with InDesign JavaScript for Switch

Post by RichardRonnback »

Thank you very much for your reply. I am quite experienced in scripting, so I understand that some object I try to reference is not available, but I can't figure out why.

I do have a log feature already implemented, as a reporting utility so I can use that as a debugging device as well.

A question about the $doc variable,

say that I try to create a reference to a doc in the form of "app.document[0]" would that return null / invalid / undefined? I was under the impression that $doc was only used for the communication between the script and Switch, not that otherwise valid references, lite the one above, would not be valid?

Again, thanks for your kind help!
loicaigon
Advanced member
Posts: 560
Joined: Wed Jul 10, 2013 10:22 am

Re: Help with InDesign JavaScript for Switch

Post by loicaigon »

You could log the following and see if the object is valid:

log ( app.documents[0].isValid )

But using $doc makes you sure that you will work on the document that Switch made InDesign to open. It could be as well that you have other InDesign documents open and index may not represent that document.

Also something that now pops tyo my mind. Don't you have an instruction to close the document in your script?

because it maybe that Switch tries to close the doc but as you may have closed it already, the object is no longer valid.
So it's not really your script failing but the consequences of your actions on the Script execution that Switch generated.

To ease my debugguing, I also use to use a context flag (i.e. executed in InDesign or from Switch). You can use an argument in Switch for ex:
Image

the your script can do:
var switchExec = false;
if( typeof $arg1 !== "undefined && $arg1==="switchExec"){
switchExec = true;
}

var doc;
if (switchExec) doc = $doc;
else doc = app.documents[0]; //then ensure this exists

same for closure
if(!switchExec) doc.close. (…)

Hope that helps.

Loic
Loïc Aigon
Enfocus PitStop Manager
loicaigon
Advanced member
Posts: 560
Joined: Wed Jul 10, 2013 10:22 am

Re: Help with InDesign JavaScript for Switch

Post by loicaigon »

Here is a script that works both in InDesign and Switch:

Code: Select all

log("-----------------------------------");

//Are we in Switch? (based on $arg1 being the string "switchExec")
var switchExec = false;
if( typeof $arg1 !== "undefined" && $arg1==="switchExec"){
    switchExec = true;
}

//Setting the doc object
var doc;
if (switchExec) doc = $doc;
else if(app.documents.length > 0){
    doc = app.documents[0]; //then ensure this exists
} 

//Running main routine
if (typeof doc !=="undefined"){
    doc.pages[0].textFrames.add({contents:"Hello", pointSize:120});
}

//Tracing function
function log(msg){
    var f = new File (Folder.desktop+"/log.txt");
    f.open("a");
    f.writeln(msg);
    f.close();
}
Loïc Aigon
Enfocus PitStop Manager
Post Reply