0

I'm calling an Applescript scpt file and when I opened the AppleScript editor it had an option to use JavaScript.

I would like to convert my AppleScript to JavaScript but can't find any documentation on it (announcements and such and redirects on apple).

Here is my AppleScript:

#!/usr/bin/osascript

on run argv

    set output to "{\"1st Parameter\":\"" & first item of argv & "\"}"
    return output

end run

More context:

I'm trying to loop through a directory and export from Excel. I only need to know about how to run JavaScript in a SCPT file but this is the background.

#!/usr/bin/osascript

on run argv

    #goal is to export multiple files to csv
    #the plan is to
    #pass in folder and loop through files or 
    #pass in array of files (paths as a comma separated string)
    #pass in single file and call script multiple times
    set processReportsScript to "excel -e '" & first item of argv & "'  '" -o '" & second item of argv & "'.csv'"

    do shell script processReportsScript

end run

UPDATE:

While in Script Editor I'm able to run this JavaScript:

function run(args) {
  var x = false;
  var y = Application("Mail");
  var running = y.running();
  var id = y.id();

  debugger;

  if (x) { 
    console.log("Why isn't this being called?")
  }

  return "hello";
}

Based in part on this guide.

However when I save the JavaScript into the SCPT file and call it from an external application it gives the following error:

script error: Expected end of line, etc. but found “(”. (-2741)

It does not give any errors when using AppleScript.

FYI the external application is passing the script to osascript.

UPDATE 2:

I changed the extension from scpt to js and now it's running. I read that it's possible to pass in the language type using -l but when I do I get numerous variations of the error:

no such component " 'JavaScript'" no such component " JavaScript"

As long as it works by changing the extension I think this works.

1.21 gigawatts
  • 16,517
  • 32
  • 123
  • 231
  • The `function` runs fine for me as `.scpt` when passed directly to `osascript`. What is the external app that's passing it to `osascript`? BTW From the man page "-l language Override the language for any plain text files. Normally, plain text files are compiled as AppleScript." – user3439894 Jan 11 '18 at 03:46
  • That's interesting. I might be passing argument incorrectly but if it works for you without changing anything then I'm not sure. It's an Adobe AIR app using a native process call. I passed the argument `-l JavaScript` and `-l "JavaScript"` and `-l "javascript"` and variations of it. – 1.21 gigawatts Jan 11 '18 at 04:12
  • I don't have Adobe AIR, so I can test, sorry. The `-l` option is only for plain text files, not `.scpt` when the language is other then AppleScript. (Read the man page for osascript.) – user3439894 Jan 11 '18 at 04:22
  • That's OK. I think I have it mostly working. – 1.21 gigawatts Jan 11 '18 at 04:31

1 Answers1

1

Both AppleScript and JavaScript for Automation (JXA) use the same file extension: ".scpt". When you are in the Script Editor, there is a language popup where you can select either:

enter image description here

The is no tool for auto-conversion from AppleScript to JXA.

For more info about JXA, see
JXA Resources

JMichaelTX
  • 1,659
  • 14
  • 19