Giter Club home page Giter Club logo

Comments (6)

johnelm avatar johnelm commented on June 12, 2024 2

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.

from node-jxa.

JMichaelTX avatar JMichaelTX commented on June 12, 2024 1

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.

from node-jxa.

JMichaelTX avatar JMichaelTX commented on June 12, 2024

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)

from node-jxa.

JMichaelTX avatar JMichaelTX commented on June 12, 2024

@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:

image

from node-jxa.

johnelm avatar johnelm commented on June 12, 2024

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

from node-jxa.

johnelm avatar johnelm commented on June 12, 2024

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.

from node-jxa.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.