Adding Useful Text Shortcuts to the Clipboard With Alfred

Comments

Alfred Worflows can be useful in creating strings and pushing them immediately to the clipboard, so that they are available without moving the hands from the keyboard.

Pushing formatted date into the clipboard

For example, I often use dates in the format [yyyy-mm-dd] in various documents and homemade apps. Since it is something I do quite regularly, an Alfred workflow may be a good idea. Something simple, something that works out a date and puts it in the clipboard, and pastes it in the frontmost app if it can.

Creating the a workflow

In Alfred, in the workflows tab, I clicked on the + button at the bottom of the LHS panel (1), and selected blank workflow. I filled in some details and clicked save

Then I clicked on the plus button at the top of the RHS panel (2), and selected from ‘inputs’ a ‘keyword’, from ‘actions’ a ‘run script’, and from ‘outputs’ a ‘copy to clipboard’. I dragged from the kink in ‘keyword’ to ‘run script’ to connect them, then did the same to connect ‘copy to clipboard’. Then I double clicked on each in turn to edit.

The Keyword object

The keyword argument is the actual keyword that you type in Alfred to run the command, I used “today”. I also ticked “with space” and selected “argument optional” from the dropdown next to it. This means that I can call the argument as “today +2” or “today y” for example, to get the date two days from now or yesterday. “With space” ensures the space between “today” and the next work is considered part of the keyword and not the argument. The argument itself can be used in the script as the special string “{query}”.

The title and subtext appear in the Alfred dropdown when you are typing the keyword.

The Run Script object

I selected PHP as the language, left all the checkboxes as they were, and entered the following script. Not the most sophisticated script, but before I invest time to make it slick I want to make sure I actually need it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
switch ("{query}") {
  case "tomorrow":
  case "1":
  case "+1":
  case "t":
    $when = mktime(0, 0, 0, date("m")  , date("d")+1, date("Y"));
    break;

  case "2":
  case "+2":
    $when = mktime(0, 0, 0, date("m")  , date("d")+2, date("Y"));
    break;

  case "3":
  case "+3":
    $when = mktime(0, 0, 0, date("m")  , date("d")+3, date("Y"));
    break;

  case "-2":
    $when = mktime(0, 0, 0, date("m")  , date("d")+2, date("Y"));
    break;

  case "-3":
    $when = mktime(0, 0, 0, date("m")  , date("d")+3, date("Y"));
    break;

  case "yesterday":
  case "-1":
  case "y":
    $when = mktime(0, 0, 0, date("m")  , date("d")-1, date("Y"));
    break;

  case "next\\ week":
  case "7":
  case "+7":
  case "n":
    $when = mktime(0, 0, 0, date("m")  , date("d")+7, date("Y"));
    break;

  case "last\\ week":
  case "l":
  case "-7":
    $when = mktime(0, 0, 0, date("m")  , date("d")-7, date("Y"));
    break;

  default:
    $when = mktime(0, 0, 0, date("m")  , date("d"), date("Y"));
    break;

}
print date("[Y-m-d]", $when);

The Copy To Clipboard object

For this I only ticked the ‘Automatically paste to front most app’ tickbox.

Job done

Creating the workflow was quite quick, and since I store the Alfred preferences in my Dropbox folder, I can easily share them with my work computer. So now I can easily type “ALT-Space today RETURN” to insert a date in the document I am writing, or any of the more complicated ones like “today -3” or “today n” (although I probably won’t need those).

Comments