You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is it possible to have an Applescript API for Mac apps made with Electron?
Reacted by Zeyi (Rice) Fan, kaue, Shmully Wolfson, Ronan, Lucas Bento, Qays H. Poonawala, Miguel Sánchez Villafán, Vlad Sitalo, Andrew Kwon, Michael Klement and 19 more
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
Reacted by Qays H. Poonawala, Kevin Doveton, Leo sun, Jacob Greenfield, Paolo Castro and Starry
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.
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?
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?
@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?
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!
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.
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.
Reacted by Stephen Kaplan, Andrew Schreiber, Victor Hallberg, Colin Cahill and Ryan Hamel
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.
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:
The Cocoa Scripting Guide: discusses scriptable applications. This is helpful, but some sections don't apply if you aren't using the Cocoa "script command" mechanism which requires custom classes.
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!
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.
Activity
bengotow commentedon Feb 9, 2016
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.
kevinsawicki commentedon Feb 9, 2016
Agreed, going to close this out, thanks for sharing that link @dkfiresky
jacob-greenfield commentedon Feb 10, 2016
I was talking about something more like this: https://developer.apple.com/library/mac/documentation/AppleScript/Conceptual/AppleScriptX/Concepts/scriptable_apps.html
nimish commentedon Feb 10, 2016
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
jacob-greenfield commentedon Feb 10, 2016
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 anexecuteorexecuteJavascriptor similar function which lets you run Javascript inside the browser from another application using Applescript.anaisbetts commentedon Feb 10, 2016
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?joshaber commentedon Feb 10, 2016
Oof, yeah, you could dynamically implement selectors using the Objective-C runtime to call through to JS.
jacob-greenfield commentedon Feb 10, 2016
@joshaber Seems interesting and complicated. How should I begin approaching this?
joshaber commentedon Feb 10, 2016
https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtDynamicResolution.html is probably a good starting point.
aricwilmunder commentedon Dec 2, 2016
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?
capripot commentedon May 31, 2017
Out of curiosity, how come touchbar support is built-in but AppleScript api should be a node module?
9 remaining items
ThomasSzabo commentedon Jul 26, 2022
@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?
github-actions commentedon Oct 25, 2022
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!
gdsmith commentedon Oct 31, 2022
bump
codebykevin commentedon Dec 20, 2022
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 tellThis 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 tellThe 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.
jacob-greenfield commentedon Dec 20, 2022
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.
andrewschreiber commentedon Jul 12, 2023
Curious what you found on scripting electron @Coder-256!
colinjcahill commentedon Aug 17, 2023
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.
jacob-greenfield commentedon Aug 18, 2023
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.
andrewschreiber commentedon Sep 14, 2023
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.~/.electron-scriptable, creating it if it doesn't exist~/.electron-scriptable/com.bundle.id/main.js(uses fs.watch at a customizable interval)schema_v1.jsonbased on your watchers (probably manual at first)Pros
Cons
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!
andrewschreiber commentedon Sep 15, 2023
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
andrewschreiber commentedon Sep 21, 2023
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
Now imagine one of the arguments is a freshly generated private key
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.
iamEvanYT commentedon May 30, 2025
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