DEV Community

Adam Lombard
Adam Lombard

Posted on • Updated on

macOS: Run a script from any app via custom hotkey

In MacOS, it's possible to run a script -- from any application -- via custom hotkeys we set ourselves.

I use modified ISO 8601 datetime strings (in a YYYYMMDD_HHMMSS format) several times a day, for all sorts of things, because they are relatively unique (you get a new one every second), they keep items sorted by creation time, and they're fairly easy to read and/or remember if you have to. Let's set up our Mac to generate these strings on the fly!

1 - In our Applications, let's open a program called Automator.

Automator app

2 - Select "Quick Action" and click "Choose". (We may need to go "File -> New" in order to see the "Quick Action" option.)

3 - In the setup group box for our Quick Action, set "Workflow receives current" to "no input" in "any application".

4 - In the menu tree on the left-hand side, select "Library -> Utilities".

5 - In the "Utilities" sub-tree, double-click "Run Shell Script". This will place a shell script interface in the work area on the right of the application.

6 - In the script interface, enter this shell script:

date '+%Y%m%d_%H%M%S'
Enter fullscreen mode Exit fullscreen mode

7 - At the bottom of the script interface, click "Results" to expand the results panel.

8 - In the upper right corner of Automator, click "Run (►)". We should see the formatted datetime string appear in the "Results" panel of the script interface.

At this point, our finished service should look like this in Automator:

Finalized service in Automator

9 - Save our new service! Name it something nice, like... like... FancyDateStamper or... Louise!

10 - Go to "System Preferences -> Keyboard -> Shortcuts".

11 - Select "Services" from the left-hand menu.

12 - In the right-hand menu, find our new service, make sure it has a checkmark in front of it, and click "Add Shortcut".

13 - Now things get hard, because it's really tough to find a shortcut that isn't already used by an app. I went with ctrl+alt+command+D, but I'm almost certain to regret that someday. 😐

And that's it! At this point, we should now be able to open almost any app, and, anywhere we can enter text via the keyboard, we should be able to instead run our hotkey combo and get our datetime stamp. If not, we may have some permissions issues...

14 - Go "System Preferences -> Security & Privacy -> Privacy"

15 - Select "Accessibility" from the left-hand menu.

16 - Unlock to make changes, if necessary, then click the "+" sign at the bottom of the right-hand menu.

17 - Add "Automator" as a trusted app.

While in Automator, you may have noticed the option to create a service in JavaScript as well! Let me know what you automate! 😊


Credit to this StackExchange article, where I first learned the general steps involved.


Was this helpful? Did I save you some time?

🫖 Buy Me A Tea! ☕️


Top comments (0)