If you’re looking for a quick and convenient way to run a Node.js script from your macOS menu bar, there are several options available. You can either create your own macOS menu bar app or use third-party apps. However, macOS provides a straightforward approach to achieve this using AppleScript.

To get started, follow these simple steps:

  1. Open the Script Editor app on macOS.
  2. Go to the app’s settings and enable the Show Script menu in menu bar option.
  3. Click on File and select New from the menu to open the script editor.

To run your Node.js script using AppleScript, add the following line:

do shell script "cd /Users/myself/dev; /opt/homebrew/bin/node script.js"

Alternatively, you can use JavaScript for Automation (JXA) to achieve the same result. Here’s an example using JXA:

const app = Application.currentApplication()
app.includeStandardAdditions = true
app.doShellScript('cd /Users/myself/dev; /opt/homebrew/bin/node script.js')

As a JavaScript developer, you have the flexibility to choose between AppleScript and JXA. However, AppleScript tends to feel more native in this case.

The cd command is used to navigate to the directory containing your Node.js script, ensuring that the .env file in that folder is picked up by Node. After adding a suitable name to your script, save it into your user’s scripts folder, which can be found in ~/Library/Scripts.

Once you have completed these steps, you will find your script conveniently listed in the menu bar. Simply click on the script to run it, and a spinning gear will appear to indicate its progress. This feature allows you to be notified when the script has finished running.

Running a Node.js script from your macOS menu bar is a neat and efficient way to streamline your workflow. Give it a try and see how it elevates your productivity!

Tags: Node.js, macOS, AppleScript, JavaScript for Automation, menu bar, scripting