Skip to content

[Enhancement] Applescript API #4418

Open

Description

@jacob-greenfield

Is it possible to have an Applescript API for Mac apps made with Electron?

Activity

  1. bengotow commented on Feb 9, 2016

    @bengotow
    Contributor

    I think this is probably best done through a native node module like the one above rather than being built into the core Electron framework.

  2. kevinsawicki commented on Feb 9, 2016

    @kevinsawicki
    Contributor

    I think this is probably best done through a native node module like the one above rather than being built into the core Electron framework.

    Agreed, going to close this out, thanks for sharing that link @dkfiresky

  3. nimish commented on Feb 10, 2016

    @nimish

    This is for support for exposing functionality inside the electron app to applescript, not calling applescript via the node app, so that things like beardedspice can work

  4. jacob-greenfield commented on Feb 10, 2016

    @jacob-greenfield
    Author

    What I meant was I wanted BeardedSpice to be able to execute Javascript in Electron apps. I was using AppleScript as a potential medium for doing this. On a Mac, open Script Editor and open File > Open Dictionary... and select basically any web browser. Choose its own suite on the left and you will se an execute or executeJavascript or similar function which lets you run Javascript inside the browser from another application using Applescript.

  5. anaisbetts commented on Feb 10, 2016

    @anaisbetts
    Contributor

    It's probably not reasonable to implement AppleScript support in Electron apps, because the format for Script Dictionaries is essentially a dictionary of "Script command => Objective C selector" mappings.

    Theoretically, you could map all of these to a Proxy object then marshal the call to a JS event (probably on app), but my Objective C-fu is lacking in this department. Maybe @joshaber has an idea?

  6. joshaber commented on Feb 10, 2016

    @joshaber
    Contributor

    Oof, yeah, you could dynamically implement selectors using the Objective-C runtime to call through to JS.

  7. jacob-greenfield commented on Feb 10, 2016

    @jacob-greenfield
    Author

    @joshaber Seems interesting and complicated. How should I begin approaching this?

  8. aricwilmunder commented on Dec 2, 2016

    @aricwilmunder

    I'm trying to click menu items in an Electron App using AppleScript but I'm not seeing any elements when I open the apps dictionary. Anyone with success in this area?

  9. capripot commented on May 31, 2017

    @capripot

    Out of curiosity, how come touchbar support is built-in but AppleScript api should be a node module?

  10. 9 remaining items

  11. ThomasSzabo commented on Jul 26, 2022

    @ThomasSzabo

    @nornagon this is nice. I also tried a native module and liked it a lot. But I am not getting how I can send an incoming AppleEvent (as defined in the sdef file) to the native node module binary that is hidden deep inside the app folder.

    Is the conclusion of this thread that I have to build my own custom electron with AppleScript in order to receive AppleScript events?
    Or are there any other ways? Has anyone accomplished anything?

  12. github-actions commented on Oct 25, 2022

    @github-actions
    Contributor

    This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. If you have any new additional information—in particular, if this is still reproducible in the latest version of Electron or in the beta—please include it with your comment!

  13. gdsmith commented on Oct 31, 2022

    @gdsmith

    bump

  14. codebykevin commented on Dec 20, 2022

    @codebykevin

    Implementing a full AppleScript API with the ability to specify objects and properties on an app-specific basis may be too complex.

    However, it appears that, whether intended or not, the core Apple Events and AppleScript commands -- launching the app, opening files, display dialogs, quitting the app -- are already present. This AppleScript runs without error:

    tell application "/Applications/Visual Studio Code.app" launch open POSIX file "/Users/kevin/Desktop/anigif.tcl" display dialog "quitting now..." delay 3 quit end tell

    This script launches VS Code, opens the named file, pops up a little dialog announcing it's quitting, and then after sleeping for three seconds, the app quits.

    Further interactive testing shows that VS Code responds to the "reopen," "activate," and "show preferences" Apple Events as well.

    It's likely that Electron is picking up this functionality, even though It's not apparently documented, as part of being built on the Cocoa API's - these core events are all baked into the Cocoa framework. In other words, VS Code, and more fundamentally Electron, gets this for free.

    If this functionality is NOT documented, may I suggest a documentation enhancement? And if it is, can someone post a link?

    One additional suggestion. While implementing a FULL AppleScript API is a huge undertaking, would it be too much to implement an AppleScript bridge to an existing Electron scripting API? Tcl/Tk implements a "do script" AppleScript command that lets external apps call into a Tcl/Tk application via the AppleScript bridge, and the script command is then executed by the Tcl interpreter:

    tell app "Wish" do script "puts \"hello world\" end tell

    The output "hello world" is returned to the calling application.

    Implementing similar functionality in Electron that hooks into one or more of the "executeJavaScript" API's could potentially add a rich IPC mechanism to Electron apps on macOS and improve their ability to interact with other Mac apps.

  15. jacob-greenfield commented on Dec 20, 2022

    @jacob-greenfield
    Author

    The built-in commands you're referencing are documented in the AppleScript Language Guide; most of them are also part of the Standard Suite you can access using Script Editor from File > Open Dictionary.... Most browsers including Chrome and Safari already have an "execute JavaScript" action but now most browsers require it to be enabled from the developer menu, due to obvious security issues (any unsandboxed local app can use AppleScript, and thus could access all your data from that browser). For that reason I think it's best to follow suit and not enable full JavaScript control through AppleScript by default.

    Considering it's been nearly 7 years with no progress since I opened this issue, at this point I'm just going to try and implement full, correct scripting dictionary support. Not totally sure if it's feasible but it's worth a shot.

  16. andrewschreiber commented on Jul 12, 2023

    @andrewschreiber

    Curious what you found on scripting electron @Coder-256!

  17. colinjcahill commented on Aug 17, 2023

    @colinjcahill

    If @Coder-256 or any other brave soul is interested in building the bridge discussed here I'd love to be the first to donate to the cause. This is becoming an increasingly vexing programming gap.

  18. jacob-greenfield commented on Aug 18, 2023

    @jacob-greenfield
    Author

    It should be possible, but I just don't have the time to do something this large; it's a much more difficult project than it seems. The main challenge is the fact that you can't really use Cocoa script commands without writing and loading your own application-specific Swift/Objective-C code (bit of a no-no for Electron), so it would be best to handle the raw Apple Events themselves using a JS API. The hardest parts of that will likely be making a good JS wrapper around either the Cocoa/Objective-C APIs (e.g. NSAppleEventDescriptor) and/or the C API (e.g. AEGetParamDesc, etc.), and figuring out how to actually read the Apple Events that AppleScript generates.

    The starting point for getting these events would probably be -[NSAppleEventManager setEventHandler:andSelector:forEventClass:andEventID:] (as suggested here).

    Good resources for this can be a bit difficult to find, partly because Apple Events are a bit old and not very commonly used. Some good resources are:

    Best wishes to whoever takes this on! Let me know if you have any questions and I'll do my best to answer.

  19. andrewschreiber commented on Sep 14, 2023

    @andrewschreiber

    Perhaps there is an opportunity here for a "bolt-on" solution in the form of an Electron package. AppleScript is one way to enable two-directional communication between local applications, but not the only way. Maybe it'd be better to have a cross-platform approach here as well.

    Lets call the package idea electron-scriptable, which would basically be a local PubSub folder.

    • Looks for a folder at ~/.electron-scriptable, creating it if it doesn't exist
    • Makes a folder for the Electron app with a path like ~/.electron-scriptable/com.bundle.id/
    • Register to watch for updates in main.js (uses fs.watch at a customizable interval)
    • Command to create a schema_v1.json based on your watchers (probably manual at first)
    • For other applications that want to run scripts on your application, they would create a new JSON file in that folder based on the latest schema.
    • When a new file is detected in the folder, gets parsed by the function. Perhaps an array of new files since last check.
    • Your application can run commands, ignore, etc
    • Updates file with response / errors.
    • Other application also could have a watcher which detects your update of the JSON file, then run a function

    Pros

    • Cross-platform
    • Could work on older versions of Electron
    • Quick to setup
    • Could pass arbitrary bi-directionally information as base64
    • Other applications could watch as well for composability
    • Your application wouldn't have to be open. Could have a message backlog
    • AppleScript can read and write JSON files as well

    Cons

    • Security. For sensitive data would need to do pinned encryption, which could be reverse engineered
    • Spoofable. Other applications might be able to create a fake schema for your application
    • Perhaps small battery life / responsiveness trade-off

    Overall I think the security concerns are fairly manageable given we're operating on a local machine. That's my sketch, curious what fellow people looking for scriptable Electron apps think!

  20. andrewschreiber commented on Sep 15, 2023

    @andrewschreiber

    As an interim solution, using Deep Links / protocols is probably our best bet for at least one-directional communication

    https://www.electronjs.org/docs/latest/tutorial/launch-app-from-url-in-another-app

    Here's how Raycast extension silently triggers an effect in Things, for example

    export async function silentlyOpenThingsURL(url: string) {
      const asyncExec = promisify(exec);
      await asyncExec(`open -g "${url}"`);
    }
    
    export async function updateTodo(id: string, todoParams: UpdateTodoParams) {
      const { authToken } = getPreferenceValues<Preferences>();
    
      if (!authToken) throw new Error('unauthorized');
    
      await silentlyOpenThingsURL(
        `things:///update?${qs.stringify({
          'auth-token': authToken,
          id,
          ...todoParams,
        })}`,
      );
    }
    
  21. andrewschreiber commented on Sep 21, 2023

    @andrewschreiber

    Had a sketch of an idea on turning URL scheme 1-directional triggered communication into a secure callback experience

    Let's say VSCode integrates this package and exposes a scripting interface that lets you, say, trigger code compilation

    • vscode://compile_myproject

    Now imagine one of the arguments is a freshly generated private key

    • vscode://compile_myproject-key_wbdhsk834

    Now VSCode, using electron-scriptable, would trigger the command upon receiving the properly formatted scheme. Then it would publish the result of that command in the ~/.electron-scriptable folder (as detailed in comment above). The result - compile success - would be encrypted with the private key, so only the original triggering application could parse it.

  22. iamEvanYT commented on May 30, 2025

    @iamEvanYT
    Contributor

    If anyone is still looking for a solution for this, I made a native module for handling it.
    https://www.npmjs.com/package/electron-osa-bridge

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions