Skip to main content
Inspiring
May 21, 2018
Answered

Opening a file in InDesign from jsx

  • May 21, 2018
  • 2 replies
  • 3777 views

This seems trivial, but I can't seem to get InDesign to open a file.

I'm can pass a file path to the index.jsx using an evalScript:

// pasteFile is '/Users/me/Desktop/Blah/05_21_2018_PRODUCT/Blah_05_21_2018_PRODUCT.indd'

//csInterface.evalScript("openFile('" + JSON.stringify(pasteFile) + "')");

    csInterface.evalScript("openFile('" + pasteFile + "')");

Then in the .jsx, I simply try to open it:

#include "json2.js"

function openFile(someFile) {

     //var aFile = JSON.parse(someFile);

     alert(someFile);

     var docRef = app.open(someFile);

}

I read elsewhere that you need to JSON.stringify then JSON.parse an argument being passed to the jsx. Regardless, alert(someFile); works in both cases, and it alerts with the proper file path. app.open(someFile); appears to do nothing, however.

Any ideas?

This topic has been closed for replies.
Correct answer toddm72

I double checked that InDesign CC is updated, and that the file I'm opening is for the correct version.

I have a hunch, though I haven't been able to prove it yet.

The script is copying a file to a new folder, then trying to open that file. I'm using node's fs to do the copying. Perhaps it's trying to open the file before the pipe finishes?

Here's what it's doing:

   // node

    var fs = require('fs');

    fs.createReadStream(copyFile).pipe(fs.createWriteStream(pasteFile));

   

    csInterface.evalScript("openFile('" + pasteFile + "')");

I'm newish to JavaScript and even newer to node, so I'm still trying to work out a way to force the script to wait for the createWriteStream to finish before running the evalScript.

If that even is the issue...


I worked it out, my hunch was right.

It was trying to open the file before the createWriteStream finished.

Changed that code to:

    // node

    var fs = require('fs');

    var ws = fs.createWriteStream(pasteFile);

    fs.createReadStream(copyFile).pipe(ws);

   

    ws.on('finish', function(){

        csInterface.evalScript("openFile('" + pasteFile + "')");

    });

This waits for the stream to 'finish', then runs the evalScript. Working now.

2 replies

Loic.Aigon
Legend
May 21, 2018

That's a peculiarity of ExtendScript and I find it reliable enough. However, the FileSystem Class of CEP has some advantages like checking right managements which ExtendScript can't. And node has probably some interests if you do pipe operations and monitor async processes. But for minor operations, I am ok with ExtendScript File class.

Trevor:
Legend
May 21, 2018

You don't need JSON.stringify or json2.js

Most likely your file path is not valid and thus not opening.

Put a check in to see if the path exists.

new File(someFile).exits

toddm72Author
Inspiring
May 21, 2018

I thought something was funny about that stringify business. Glad to know I can remove that.

I added that check, which returns 'true':

function openFile(someFile) {

    var checkFile = new File(someFile).exists;

    alert(checkFile);

    var docRef = app.open(someFile);

}

So the file does exist. InDesign still won't open it.

Loic.Aigon
Legend
May 21, 2018

May be a version issue. You may try to open a newer version of a file with an older InDesign version (ex: CC > CS )