node-jxa
node-jxa copied to clipboard
Issue with the JXA Application.currentApplication() Object
I get an error in VSC using Code Runner with this script:
#!/usr/bin/env osascript -l JavaScript
var app = Application.currentApplication();
app.includeStandardAdditions = true;
console.log("Test Get app name");
var appProp = app.properties(); // <== fails on this line
appProp.name;
/*
ERROR MSG:
117:148: execution error: Error on line 6: Error: Message not understood. (-1708)
NOTE: Even though it says "line 6", the actual line is 7, as line 6 outputs correctly.
For completeness here's the entire Code Runner output:
[Running] /usr/bin/env osascript -l JavaScript "/Users/Shared/Dropbox/SW/DEV/JXA/VSC/Test Using CodeRunner Ext.js"
Test Get app name
/Users/Shared/Dropbox/SW/DEV/JXA/VSC/Test Using CodeRunner Ext.js:117:148: execution error: Error on line 6: Error: Message not understood. (-1708)
[Done] exited with code=1 in 0.11 seconds
*/
This same script, without the shebang, runs fine in Script Editor.
What am I doing wrong in VSC?
To provide more info, VSC with Code Runner seems to work fine otherwise using the currentApplication()
object:
#!/usr/bin/env osascript -l JavaScript
var app = Application.currentApplication();
app.includeStandardAdditions = true;
var msgStr = "Test using VSC Code Runner"
var titleStr = "Test Using CodeRunner"
app.beep()
var oAns = app.displayDialog(msgStr,
{
withTitle: titleStr
,withIcon: "caution"
,buttons: ["Cancel","OK"]
,defaultButton: "OK"
,cancelButton: "Cancel"
})
var ansStr = oAns.textReturned
var btnStr = oAns.buttonReturned
console.log("Btn: " + btnStr)
hey @JMichaelTX ,
I think what's happening there is that Application.currentApplication()
is returning an Application object when you run it from within vscode (and thus vscode is the active app), even though vscode is not an OSA scriptable app.
This is interesting.. it looks like the app object obtained for a non-scriptable app is still a valid object. It has a working id() and name(). And it can be decorated with Standard Additions like any other app object, so methods like displayDialog()
will work. But it has no properties()
etc.
#!/usr/bin/env osascript -l JavaScript
let app = Application.currentApplication();
// or
app = Application( "/Applications/Visual Studio Code.app" );
app.includeStandardAdditions = true; // app.displayDialog and so forth will work
// const props = app.properties(); // doesn't work
console.log( `app.name(): "${app.name()}"`) // "Visual Studio Code"
console.log( `app.id(): "${app.id()}"`) // "com.microsoft.VSCode"
On the other hand, Script Editor is a scriptable app, and so it provides properties()
and other app-scripting goodies.
@johnelm , thanks for the quick response. OK that solves my issue.
And now I have been able to install your node-jxa, so this works:
I think what's happening there is that Application.currentApplication() is returning an Application object when you run it from within vscode (and thus vscode is the active app), even though vscode is not an OSA scriptable app.
@johnelm, Thanks for your very quick replies and help.
one minor glitch. Your script:
console.log( `app.name(): "${app.name()}"`) // "Visual Studio Code"
console.log( `app.id(): "${app.id()}"`) // "com.microsoft.VSCode"
works for:
app = Application( "/Applications/Visual Studio Code.app" );
but returns "undefined" for:
let app = Application.currentApplication();
app.name(): "undefined" app.id(): "undefined"
Not a big deal. I very rarely use the properties of the currentApp, so I don't see an issue here.
@JMichaelTX
Hmm, interesting. When I ran the script from within vscode, Application.currentApplication()
returned the same app object as when I specified it explicitly.
I’ve actually never used Application.currentApplication()
; I’ve always been able to target a specific scriptable app in my scripts.
Cheers, John
PS: you’re a committer for the JXA-Cookbook, yeah? Can anyone make edits, or do you accept PRs on the wiki repo? I was thinking of suggesting node-jxa links in the couple spots where the browserify trick is suggested.
hey @JMichaelTX
I'll close these issues in a bit.
You’re a committer for the JXA-Cookbook, yeah? Can anyone make edits, or do you accept PRs on the wiki repo? I was thinking of suggesting node-jxa links in a couple spots where the browserify trick is suggested, in the Cookbook. Let me know what you think.