Drafts: Slash Command

Slash commands are something that I think rose to prominence through the Slack collaboration platform. They are a way of invoking commands by entering a forward slash character followed by a string of text into. Effectively a quick entry method. I figured this could be useful for me in Drafts, and so wrote something to do it.

The ThoughtAsylum Action Group and TADpoLe

Before I get into the details, the first thing I first should mention that the slash command functionality I created is available in the ThoughtAsylum Action Group, which in turn utilises the Thought Asylum Drafts Library (TADpoLe). Download links and additional information about these are available on the TADpoLe page on this site.

Slash Commands Invocation

There are two actions in the ThoughtAsylum Action Group that can trigger evaluation of a slash command.

  • TAD-Slash Command Prompt - pops up a prompt in which to enter the slash command.
  • TAD-Slash Command In-Situ - evaluates the slash command in the draft based on current cursor position.

I would recommend creating actions that you assign your own keyboard shortcuts to and that include these shortcuts (as described in the Introducing TADpoLe post).

Prompt

For the prompt version, when the TAD-Slash Command Prompt action is invoked a pop-up input window is displayed and the command can be entered without the forward slash.

In-Situ

The equivalent in-situ version is where a forward slash is entered on a line, and then the TAD-Slash Command In-Situ is invoked. That’s the opposite order to the command prompt version.

As the action is executed, the action takes the content from the last forward slash on the line and reads the content from that point forwards. That text is removed and the command executed.

Default Commands

There are a number of pre-built commands available that come as part of the standard ThoughtAsylum Action Group.

Open Action Directory

Keyword: actdir
Description: Opens the URL “https://actions.getdrafts.com” in the default browser.

Dictate to Clipboard

Keyword: cbdic
Description: Runs the “TAD-Dictate-to-clipboard” to capture a dictation to the system clipboard.

Run Action by Name

Keyword: act
Description: Runs an action by name.
Example: act TAD-Copy Title

Load Workspace by Name

Keyword: ws
Description: Opens a workspace by name.
Example: ws Writing

Choose Workspace to Load

Keyword: wsc
Description: Runs the function Workspace.TA_selectLoadWorkspaceByButton() to display a list of workspaces to choose from and then load.

Add Tags to Draft

Keyword: tag
Description: Runs the function draft.TA_tagAddCSV to add a comma separated list of tags to the current draft.
Example: tag foo,bar

Open URL

Keyword: url
Description: Opens a URL in the default browser.
Example: url https://www.thoughtasylum.com

This last one you may notice has an issue. It has forward slashes as standard in the parameter. That means that if you enter this command as an in-situ command it will fail as the action will try and run a command based on part of the URL rather than the URL command and the URL. Running it in the command prompt however will work fine. I’ll probably resolve this at some point (it’s on the list), but my own personal use has been around opening commonly accessed URLs, and it’s pretty easy to keyboard shortcut a scripted action to toggle link mode (editor.linkModeEnabled = !editor.linkModeEnabled;), or tap the link mode button if visible; hence it has not yet made it to my priority list.

Defining Commands

You can of course define your own commands. This is done through the configuration file for the TADpoLe library, which is a JSON file. The commands are defined as key value pairs under the commands key. The key is the activation command and the value defines what the command triggers.

For the default commands described above, here is the structure of commands.

"commands" : 
{
  "url" : "OPEN_URL|<1>",
  "actdir" : "OPEN_URL|https:\/\/actions.getdrafts.com",
  "cbdic" : "RUN_ACTION|TAD-Dictate-to-clipboard",
  "act" : "RUN_ACTION|<1>",
  "ws" : "RUN_FUNCTION|app.TA_applyWorkspaceByName",
  "wsc" : "RUN_FUNCTION|Workspace.TA_selectLoadWorkspaceByButton",
  "tag" : "RUN_FUNCTION|draft.TA_tagAddCSV"
}

If you look in the configuration file, you may, or may not see these settings in there. It simply depends on whether a full save has been triggered at any point, or if it is using the defaults that are set within the code on launch. If it isn't there, you can always create it yourself, and copy in any of the commands from above that you may wish to keep. You can of course also modify them however you see fit.

From the examples, you can probably pick out that there are effectively three types of command:

  1. OPEN_URL - Opens a URL
  2. RUN_ACTION - Runs a Drafts Action by name.
  3. RUN_FUNCTION - Runs an available function by name passing in any subsequent content as parameters.

The command is followed by a pipe (vertical bar, |), and then the specifics of the instruction. It is notable that OPEN_URL and RUN_ACTION also allow you to use <1> as a placeholder for any following text.

Summary

That’s all there is to it. It is primarily an alternative to memorising keyboard shortcuts. You can use more meaningful names and even create a naming convention to help. Overall, I don’t use it a huge amount, but for my use, there are a few quick access things where this approach is invaluable. Hopefully, you’ll find some uses for it too.

Author: Stephen Millard
Tags: | drafts | tadpole |

Buy me a coffeeBuy me a coffee



Related posts that you may also like to read