Eric Willis

Established circa ∞

Archive for April 2008

Return value from InDesign Server Script via SOAP

with 2 comments

Returning values from InDesign Server scripts can be tricky. If you’re using the SOAP interface (I am, through .NET) and you need to get information back from the IDS script you’ve executed through RunScript(), there’s a bit of code reorganization to be done.

Let’s say that you need to know the number of pages in your InDesign Document back in .NET land after the script has completed. This will not work:

var doc = app.open(File('test.indd'));
doStuff();
doc.exportFile(ExportFormat.PDF_TYPE, File('test.pdf'));
return doc.pages.length;

function doStuff()
{
// do some stuff
return doc;
}

The above script doesn’t work because the return doc.pages.length line isn’t in the right spot (even though it seems like the last line of code to run in the script).

If you wrap all of the code in a function (let’s say main) and move the call to main() to the last line of the script then everything works as expected. IDS will return the number of pages in the document through SOAP to the initiating code in the InDesignServer.Data.data object property.

The code below does work:

function main()
{
var doc = app.open(File('test.indd'));
doc = doStuff(doc);
doc.exportFile(ExportFormat.PDF_TYPE, File('test.pdf'));
return doc.pages.length;
}

function doStuff(doc)
{
// do some stuff
return doc;
}

main(); // the return statement in this function actually gets returned

Here’s a snippet of C# that would receive the return value from the IDS script (assumes that you’ve already connected to the webservice):

InDesignServer.Service idServer = new InDesignServer.Service();
InDesignServer.RunScriptParameters idParams = new InDesignServer.RunScriptParameters();
InDesignServer.Data data = new InDesignServer.Data();
String error = String.Empty;
Int32 numberOfPages = 0;

idParams.scriptLanguage = "javascript";
idParams.scriptFile = @"c:\path\to\script.jsx";

idServer.RunScript(idParams, out error, out data);
numberOfPages = Convert.ToInt32(data.data);

This reorganization of the script seems to work every time for me. I have consistently gotten proper results from IDS.

Written by Eric Willis

April 18, 2008 at 9:54 pm