Skip to main content

Command Line Script

Runs a command-line script and inserts the output into the snippet.

This placeholder is very powerful if you want to perform complex operations or interact with external tools.

iOS Compatibility

⚠️ This feature is supported only on macOS.

iOS does not allow to access the command line.

Limitations

App Store applications are sandboxed and have limited access to the system. By default, you should be able to access tools from /usr/bin, like ls, cat, grep, awk, etc.

Sandboxed apps can access only ~/Downloads folder and its subfolders. Therefore, if you or the script you are running needs to access other folders, the operation will fail.

Because of these limitations, you won't be able to run tools installed with Homebrew or other package managers.

Permissions

If you encounter permission issues, you may need to grant Full Disk Access to Snippety in System Preferences -> Security & Privacy -> Full Disk Access.

Snippety Helper

To overcome the limitations, you can use the Snippety Helper. It is a command-line tool that can run scripts for you and return the output to Snippety.

It works like a proxy between Snippety and the shell. You can run any script with it, and it will return the output to Snippety.

However, make sure that the tool is not running with superuser privileges. It would allow any app or script to run potentially harmful commands on your system.

Parameters

1️⃣ Input mode (default: Selection)

You can use an optional input passed to the script. This parameter defines where the input comes from:

  • Selection: Selected text
  • Clipboard: Current Clipboard content
  • TextField: Manual text input
  • None: No input required

To use the input value in your script use %@.

2️⃣ Script

Here you can enter the script that you want to run. You can use the input value in the script by using %@. You can also pipe commands using |.

Please note that | is a special character that Snippety uses to separate parameters. If you want to use it in the script, you need to escape it with a backslash \|. However, if you use wizard to create the script, Snippety will automatically escape it for you.

3️⃣ Escape input for script usage (default: YES)

This value defines if the input (%@) should be wrapped with single quotes.

If escaping is on the input text will be wrapped with single quotes, so that even a multiline input works.

echo %@ will produce: echo 'Selected Text'.

If escaping is off your input text must be ready for command line usage. It might be useful in some cases like when your input should be seen by a script as separate arguments.

echo %@ will produce: echo Selected Text.

Syntax

{@script|TextField|ls "$HOME/Downloads" \| grep %@|YES@}

This script will list all files in the Downloads folder and filter the output by the input text. The input text will be wrapped with single quotes, because escaping is set to YES.

Notes

If you want to see what environment variables are available to the script, you can run the following placeholder:

{@script|None|env|YES@}

Usually, scripts produce extra newline at the end of the output. If you want to remove it, you can use something like this:

{@script|None|echo 'line\nline2' \| perl -p -e 'chomp if eof'|YES@}

Running Script File

You can also run a file with a script. It can be useful if you have a long script that you want to keep in a separate file.

First, create a file with a script. Remember, that it must be accessible by Snippety. You can use ~/Downloads folder for that.

Sample script file ~/Downloads/test.sh:

#!/bin/bash

curl -s "https://gist.githubusercontent.com/wojciech-kulik/a74ddbf64b9fab2f995b8ce7e9cde9af/raw/dd9d6ec9a95a962c9f77674844493093fe38ef62/index.html"

Make sure that the script is executable by running:

chmod +x ~/Downloads/test.sh

Sample placeholder:

{@script|None|$HOME/Downloads/test.sh|YES@}

Use Cases

This placeholder can be helpful in many situations. Especially, when you want to perform advanced operations or interact with external tools.

Some ideas:

  • Filter selected text with grep
  • Count words with wc
  • Add selected text to a file
  • Download or upload files
  • Sort selected text

Example 1

A script that downloads a code from GitHub Gist and inserts it into the snippet:

{@script|None|curl -s "https://gist.githubusercontent.com/wojciech-kulik/a74ddbf64b9fab2f995b8ce7e9cde9af/raw/dd9d6ec9a95a962c9f77674844493093fe38ef62/index.html"|NO@}

Example 2

A script that formats selected JSON using jq tool:

{@script|Selection|echo %@ \| jq .|YES@}

Example 3

A script that uses Apple Script to show a notification:

{@script|None|osascript -e 'display notification "This is a test notification" with title "Test Title"'|YES@}