Opens in a new tab

Help · Snippet content

Script Snippets

A script snippet runs a small program when it expands and types whatever the program outputs. Useful for things a fixed snippet can’t do, like the current song, a random password, or text from another app.

To make a script snippet

  1. Create a snippet as usual (how).

  2. In the Content: menu, choose AppleScript, Shell Script, or JavaScript.

  3. Type the script in the editor and give it an abbreviation.

  4. Make sure Run script snippets (AppleScript, shell, JavaScript) is on in Settings ▸ Expansion (it is by default).

Examples

AppleScript: the frontmost Safari page title

tell application "Safari" to return name of front document

The value the script returns is typed.

Shell script: a random password

LC_ALL=C tr -dc 'A-Za-z0-9' < /dev/urandom | head -c 16

Whatever the script prints is typed. Shell scripts run with zsh unless the first line says otherwise (for example #!/bin/bash or #!/usr/bin/python3).

JavaScript (JXA): today in ISO format

new Date().toISOString().slice(0, 10)

JavaScript snippets run as JavaScript for Automation; the value of the last line is typed.

How scripts behave

  • 10-second limit. A script still running after 10 seconds is stopped, and whatever it had printed by then (often nothing) is typed. Your typing never freezes while a script runs; keys you press are held and typed afterward.

  • Only normal output is used. Error messages are discarded, so a failing script types nothing. Test it in Terminal or Script Editor first.

  • One trailing line break is removed from AppleScript and JavaScript output.

  • Output is typed exactly as produced. % codes in the output are not processed.

  • A normal snippet can include a script snippet with %snippet:abbreviation%; the script runs and its output appears in that spot.

  • If Run script snippets is off, typing a script snippet’s abbreviation just beeps.

  • Scripts that control other apps may make macOS ask for permission the first time.

Scripts run with your own permissions and can do anything you can. Only use scripts you understand or trust, especially in imported libraries.

Related: Content types · Script troubleshooting · Privacy, Blocklist & Permissions