description |
---|
Commands that run scripts on your computer. |
Local commands are just scripts that run on your computer. Slapdash knows how to run scripts in the most popular languages.
Even if Slapdash doesn't support your language out of the box, you can just use shebang syntax, or create a bash wrapper to call into your program.
To create a local command, run Create New Command in the Command Bar, choose Local Script as a type, select the script file on your computer, give your command a name and press Create Command.
We try to support as many languages as possible. Below you'll find a list of languages you can use to build Slapdash commands.
In practice, you can use a shebang directive to point to an interpreter of your choice.
- AppleScript
- bash / zsh
- JavaScript
- Perl
- PowerShell
- Python
- Ruby
- PHP
- TypeScript
Support for AppleScript is baked into macOS. Just create a command.applescript
file anywhere and start experimenting!
Here's an example of an AppleScript command which toggles the Mac OS system dark mode.
#!/usr/bin/osascript
tell application "System Events"
tell appearance preferences
set dark mode to not dark mode
end tell
end tell
Slapdash runs JavaScript commands using Node.
Here is an example of a JavaScript command that displays a list with two options:
// command.js
const response = {
view: {
type: "list",
options: [
{
title: "Open Slapdash",
action: {
type: "open-url",
url: "https://slapdash.com"
}
},
{
title: "Copy Heart Emoji",
action: {
type: "copy",
value: "❤️"
}
}
]
}
};
console.log(JSON.stringify(response));
When you run the command in the Command Bar, Slapdash will execute the underlying script:
node command.js
Slapdash runs TypeScript commands using NodeJS and ts-node.
If you are building a command inside some package, Slapdash will also look inside its node_modules
for the ts-node
executable. If it can't find ts-node
there it will look for ts-node
in the global node modules location on your computer.
// command.ts
// Optionally, import our npm package to get TS types for Command Response.
import { CommandResponse } from "@slapdash/command-response-types";
const response: CommandResponse = {
view: {
type: "list",
options: [
{
title: "Open Slapdash",
action: {
type: "open-url",
url: "https://slapdash.com",
},
},
{
title: "Copy Heart Emoji",
action: {
type: "copy",
value: "❤️",
},
},
],
},
};
console.log(JSON.stringify(response));
When you run the command in the Command Bar, Slapdash will execute the underlying script:
ts-node command.ts
You already have bash (or zsh) on your Mac. No need to install anything. Just create e.g. command.sh
file anywhere and start bashing:
#!/bin/bash
echo '{
"view": {
"type": "list",
"options": [
{
"title": "Open Slapdash",
"action": {
"type": "open-url",
"url": "https://slapdash.com"
}
},
{
"title": "Copy Heart Emoji",
"action": {
"type": "copy",
"value": "❤️"
}
}
]
}
}'
macOS has both Python v2 and Python v3 pre-installed, so you can just use it by default. Put the following to e.g. command.py
:
#!python3
# -*- coding: utf-8 -*-
import json
print(json.dumps({
"view": {
"type": "list",
"options": [
{
"title": "Open Slapdash",
"action": {
"type": "open-url",
"url": "https://slapdash.com"
}
},
{
"title": "Copy Heart Emoji",
"action": {
"type": "copy",
"value": "❤️"
}
}
]
}
}))
You can use Python v2 too: just change the shebang line to #!python2
as usual.
macOS should have Ruby pre-installed (or you can install it with brew install ruby
). Put the following to e.g. command.rb
:
#!/usr/bin/env ruby
require 'json'
print JSON.generate({
"view": {
"type": "list",
"options": [
{
"title": "Open Slapdash",
"action": {
"type": "open-url",
"url": "https://slapdash.com"
}
},
{
"title": "Copy Heart Emoji",
"action": {
"type": "copy",
"value": "❤️"
}
}
]
}
})
Perl is conveniently pre-installed on macOS. If you still remember how to write in Perl put the following to command.pl
:
#!/usr/bin/perl -w
use utf8;
use JSON;
print encode_json({
"view" => {
"type" => "list",
"options" => [
{
"title" => "Open Slapdash",
"action" => {
"type" => "open-url",
"url" => "https://slapdash.com"
}
},
{
"title" => "Copy Heart Emoji",
"action" => {
"type" => "copy",
"value" => "❤️"
}
}
]
}
})
Some version of PHP is pre-installed on macOS, so it should work out of the box. Put the following to e.g. command.php
:
#!php
<?php
echo json_encode([
"view" => [
"type" => "list",
"options" => [
[
"title" => "Open Slapdash",
"action" => [
"type" => "open-url",
"url" => "https://slapdash.com"
]
],
[
"title" => "Copy Heart Emoji",
"action" => [
"type" => "paste",
"value" => "❤️"
]
]
]
]
]);
PowerShell is already pre-installed on Windows. Just create a file with a .ps1
extension. Here's an example that will empty your recycling bin.
Clear-RecycleBin -Force
When searching for a programming language binary to execute the command script, we use the following approach:
- We check the first line of the script, the same way it's done in Unix-like systems by looking for the shebang directive. If the first line looks like
#!/path/to/binary
or just#!binary
, Slapdash calls the referenced interpreter to execute the command. - If there is no shebang directive, Slapdash will try to infer the language binary from the file's extension. For example, if it's a
*.rb
file, Slapdash will look for the installed Ruby binary. - If the script is within an npm package (JavaScript/TypeScript), Slapdash will check the current directory's
node_modules/.bin
folder to look for tools likets-node
. - Finally, Slapdash will use the OS's
PATH
environment variable to resolve the binary.