0

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...

Lithodora
  • 338
  • 1
  • 13

4 Answers4

1

Glad you have something that works, but if you're passing text to ExtendScript, why not format it on the AS side to be ExtendScript-friendly, like ['firstvalue', 'secondvalue', 'thirdvalue"'] --but this would be a string in AS, like

--in AS:
"['firstvalue', 'secondvalue', 'thirdvalue"']"

Then, in ExtendScript, if that's in a variable, like, myData, you can do (as I just did in ExtendScript Toolkit):

//in JS:
myArray = eval(myData);

I know using eval() is evil in web work, but for ExtendScript stuff, it can be very useful.

CRGreen
  • 3,406
  • 1
  • 14
  • 24
  • I had tried this, handing off a string. Are you telling me that eval would have done what I needed? – Lithodora Jul 25 '15 at 18:27
  • Aren't you just needing to get values from AS into JS (ES)? Since the values are text, if the text is formatted to be valid JS code, like a simple array, eval() will work on the JS sde to give you the array as an array. Unless I'm misunderstanding what you're after. – CRGreen Jul 25 '15 at 18:56
0

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

Community
  • 1
  • 1
Lithodora
  • 338
  • 1
  • 13
0

doScript can pass script args to one language to another. Here is a snippet inspired from the doc:

var aps = "tell application \"Adobe InDesign CC 2014\"\
 tell script args\
  set user to item 1 of {\"John\", \"Mike\", \"Brenda\"}\
  set value name \"user\" value user\
  \"This is the firest AppleScript script argument value.\"\
 end tell\
end tell"
    
app.doScript(aps, ScriptLanguage.applescriptLanguage);

var user = app.scriptArgs.getValue("user");
alert( user+ "from JS" );
Loic
  • 2,173
  • 10
  • 13
0

I don't think script args would return anything else than strings even if those could represent any kind of value. However a string can be easily turned into an array with a split method like this :

var aps = "set ls to {\"john\", \"mark\"}\
set n to count of items of ls\
set str to \"\"\
repeat with i from 1 to n\
 set str to str & item i of ls\
 if i < n then\
  set str to str & \",\"\
 end if\
end repeat\
tell application \"Adobe InDesign CC 2014\"\
 tell script args\
  set value name \"str\" value str\
 end tell\
end tell";

app.doScript(aps, ScriptLanguage.applescriptLanguage);

var str = app.scriptArgs.getValue("str");
var arr = str.split(",");
alert( "Item 1 of APS list is \""+arr[0]+ "\" in the JS context" );

The idea is to flatten the APS list into a comma separated string that will be later splitted in the javascript context to turn it into an array.

Loic
  • 2,173
  • 10
  • 13
  • The problem is the values themselves may contain a comma. Splitting the string may result in a bad run of the script. – Lithodora Jul 24 '15 at 15:20
  • Well you can use whatever value of your own like $$$$$$$$ or ###### and use that same value for the splitting process : set str to str & \"######\"\ on the APS side and str.split("######") on the js side ;) – Loic Jul 24 '15 at 19:45