Search code examples
javascriptjsonapplescriptadobe-indesignextendscript

Passing Applescript list to javascript ExtendScript as array for Indesign


Background

I have a load of Applescripts(AS) which designers use with InDesign that help process the workflow for production. There is a great deal of OS interaction that the AS does that the JavaScript can not, so moving away from AS is not possible.

Due restrictions I am unable to install pretty much anything. I am unable to update anything. Script Editor and ExtendScript Tool Kit are what I have to work with.

Operating Environment: OS X 10.8.5 & Adobe CS6

How it works

User preferences are saved as Properties in local Applescripts saved in the user's documents folder.

    ###property grabber.scpt
    set mypath to path to documents folder
    set mypropertiesfile to ((mypath & "myproperties.scpt") as string)
    set thePropertyScript to load script file mypropertiesfile
    set designerinitials to (designerinitials of thePropertyScript)  ETC...

Some of the properties are AS lists.

Why I need JS?

I'm making palettes and would prefer to use the ScriptUI rather than do it all in AS like this:

set dlgRef to make dialog with properties {name:"User Settings", can cancel:true, label:"Dialog Label"} 

The string the AS hands off to the JS is this:

{"myname",{firstvalue:"test", secondvalue:"val2", thirdvalue: "val3"},{firstvalue:"test2", secondvalue:"val2", thirdvalue: "val3"}}

These are not lists, but text...

The JS

myAppleScript = new File("valid_path_to/property grabber.scpt");
var myreturn = app.doScript(myAppleScript, ScriptLanguage.applescriptLanguage);
var myname = myreturn[0];
var firstlist = myreturn[1];
var secondlist = myreturn[2];

ExtendScript data browser shows:

 firstlist = {firstvalue:"test", secondvalue:"val2", thirdvalue: "val3"}

It is not an array...

I have tried using https://github.com/KAYLukas/applescript-json to json encode the lists, but the same result.

firstlist = [{firstvalue:"test", secondvalue:"val2", thirdvalue: "val3"}]

I have also made it much simpler with just

  firstlist = {"test","val2","val3"}

Still the JS treats it as a string and not an array.

Any ideas what I need to do or am doing wrong? I hope it simple and I feel stupid if I get an answer...


Solution

  • I hate finding an answer after I take the time to post an elaborate question.

    https://stackoverflow.com/a/14689556/1204387

    var path = ((File($.fileName)).path); // this is the path of the script
    // now build a path to another js file
    // e.g. json lib https://github.com/douglascrockford/JSON-js
    var libfile = File(path +'/_libs/json2.js');
    if(libfile.exists)
      $.evalFile(libfile);
    

    Like Neo learning Kung Fu, it suddenly went, "Whoa, I know JSON!"

    var firstlist = JSON.parse(myresult[1]);
    

    Gives me workable objects