Creating an OS X Service to Insert the Current Date and Time
11:06AM — Saturday, November 10, 2012. Reading time ~1.8888888888888888min.
To create a new Jekyll post, you need to create a text file with meta data—called YAML Front Matter—at the top. At its simplest, it looks something like this:
--- layout: post date: 2012-11-10 11:06:47 title: Title of Post ---
The largest barrier to creating a new post is inserting the exact date and time. My brain thinks of dates as DD/MM/YYYY, and of time in 12hr format; I wanted to automate it.
Below is a very simple AppleScript that inserts the date, time and timezone in the format that Jekyll requires.
do shell script "date +%Y-%m-%d\\ %H:%M:%S\\ %z"
To make inserting it simple, it’s possible to assign a keyboard shortcut, by turning it into a Service in Automator.
- Open Automator.app
- Create a new Service
- Set it to receive selected text in any application
- Check the ‘Output replaces selected text’ tick-box
- Insert the ‘Run AppleScript’ action
- Copy and paste the above AppleScript
- Press run to make sure it works
- Save the
- Open the
Keyboard > Keyboard Shortcutssystem preference pane, select Services in the left column, then find the Service you just created.
- Assign a keyboard shortcut. I use
There is one pitfall: it only works on selected text.
When saving a post for Jekyll, the file-name also requires a date—but not a time. For this, you can follow the same process as above with a modified AppleScript:
do shell script "date +%Y-%m-%d"
For this, I use the keyboard shortcut
I would love it if the Service created the entire file name. So if the post title ‘This is a Post Title’ was on the clipboard, it would create a file called
2012-11-10-this-is-a-post-title.md; replacing spaces with hyphens and converting it to lowercase. However, that’s well beyond my very limited AppleScript knowledge.