Scripts and Hacks Grab Bag

The origin of the term “Life hacks” was all about engineers who would build tools to help them in their daily life. As I’ve gotten more familiar with languages like Applescript, PHP, and Python, I’ve been creating more of these hacks to help out with things I do on my Mac.

Here’s a grab bag of some of those I’ve created recently. They are all in different states of stability but I use most of them so they’re at least not completely broken.


Set Start Day for Selected Tasks: OmniFocus has great support for entering dates with quick shortcuts like Mon and 2w. But they don’t have a quick way to get to the start date field from the keyboard.

I’ll often have a series of tasks that have to push back for some reason. The way I work is often based on days, like “I can’t do this now but I should have a chance on Thursday.” This applescript prompts for you to pick from a list of days. You can type in th and hit enter and the selected tasks will be scheduled for a Thursday start date.

This does all the date math on its own, where it will always choose the following occurrence of that day. I’m writing this on Saturday and if I choose Saturday from the list it will set the start day for next Saturday.

It’s a fast and logical way to schedule tasks so you don’t have to think about them until the time comes.

Download Set Start Day Applescript

Using an Applescript .app to trigger tasks from a recurring OmniFocus action: I have repeating tasks in OmniFocus to create things like weekly log files or emails for status reports that I want to automate as much as possible. Automating it is not only faster but maintains consistant naming schemes if you’re particular about that like I am.

It’s simple to create something in applescript to make a file:

set today to do shell script "date +%Y-%m-%d"
set filepath to "/Users/nickwynja/Desktop/"
set filename to "Test " & today & ".txt"

do shell script "touch " & (quoted form of (filepath & filename))
do shell script "open -a BBEdit " & (quoted form of (filepath & filename))

That will create and open a file named Test 2012-11-10.txt on my desktop in the app I specified in the open -a command. Most of that is actually shell because it’s easier to do one-liners and often more compatible between different apps.

If you save this as an application in Applescript Editor, you’ll be able to attach the .app to a OmniFocus task, and with one click, it will create and open the file you need.

Keyboard Controls

Media Control Keys with Rdio and iTunes: Most of the time I use iTunes set today to do shell script “date +%Y-%m-%d” which is well bound to to the Since getting a [clicky keyboard](http://wwstandard Previous, Play/Pause, set filepath to “/Users/nickwynja/Desktop/” If yoript Editor, you’ll be able to attach the .app to a set filename to “Test” & today & set filename to “Test” & today & “.txt”That will create and open a file named Test 2012-11-10.txt on my desktop in the app I specified in the open -a command. Most of that is actually shell because it’s easier to do one-liners and often more do shell script “touch "” & filepath & filename & “"”compatible between different apps.”.txt”OmniFocus task, and with one click, it will create and open the file you need.Next controls on a Mac. Keyboard Maestro’s built in macros for these controls behave the same way as a do shell script “open -a BBEdit "” & filepath & filename & “"”standard Apple keyboard. Sometimes when I’m feeling frisky, I’ll open Rdio to listen to some new tunes. This means I have both Rdio and iTunes open and all of a sudden these media controls become a mess. I needed something that would Play/Pause the right app when it was playing.

Since I only sometimes have Rdio open, this script would check for that first. If Rdio is running, the script sends the specified control to Rdio, or else it sends it to iTunes. This logic only fails when I have Rdio open but not playing and iTunes (which is always open) playing a song. So I just get in the habit of quitting Rdio when I’m not listening to it. This will probably work with other apps too as long as it has a similar Applescript dictionary.

This is such a great solution and has gotten my using Rdio way more. Before I’d have both open and instinctively hit the Play/Pause key and then iTunes would start. It would be a train wreck of two songs playing over each other. Connect these scripts to Keyboard Maestro and enjoy seamless control over your audio apps.

Download Rdio/iTunes Media Control Applescripts

Open and Clear Notification Center: Notification Center is a really nice way to get an overview of what you need to know so you can process it or get back to work. Apple gives a way to open Notification Center using the keyboard by setting a shortcut in System Preferences > Keyboard > Keyboard Shortcuts > Mission Control. The most often action I have in Notification Center is to open it, check the preview of new emails that have come in, and then clear out the notifications so I can ignore and come back to them later. There’s no keyboard binding for this but you can set something up in Keyboard Maestro to imitate clicking on the little x. The “Move or Click Mouse” macro is perfect and gives you options to click at a certain location on the screen and then restore the mouse location to where it was. This is what my macro command looks like.

I have F12 set up to open up Notification Center, glance at what’s in there, then hit control+F12. This imitates a click for the topmost x button. If there’s notifications from different apps, you’ll have to clear a few times. When I’ve sufficiently ignored everything, I hit F12 again to close it.

That’s efficient first-level processing without ever leaving my keyboard.


BBEdit is so wonderfully scriptable and that makes me happy inside.

Autosave Scratchpad Files: I love my scratchpads. I dump random strings of text in there, list to-dos, and leave notes to myself. I’ve always wanted to be able to pipe text into it from scripts and Alfred extensions but the problem was the file wouldn’t autosave like it would if I was using nvALT. If I would try to append text to the scratchpad and it wasn’t saved I would have conflicts.

I found this gist that ties into a hook when you switch focus away from BBEdit and saves open documents. I didn’t want it to save all open documents though, just my scratchpad.

All I needed to do was change this line which gets all open documents:

set docList to every text document in theApp

To this which matches the document name of my open scratchpad:

set docList to every text document in theApp where name of it contains "Scratchpad_"

Follow the documentation in the script to set it up. If you bought BBEdit in the Mac App Store, you’ll also have to install a script to get around sandboxing and authenticate this save for you. You can find instructions to do that here.

Now I can assume that in most use cases my scratchpad is saved and I can append text without conflicts. I use F2 as a keyboard shortcut triggering the script that opens my scratchpad and now have ⌘-F2 to append my clipboard to the end of the saved scratchpad using a hacky keyboard maestro macro.

Text Filters: I’ve discovered that you can run scripts on text selections and if you only intend to do that action inside of BBEdit, it runs faster than using Services.

I previously created a service that would mark may plain-text todos as completed. This worked but wasn’t as fast as Text Filters are. The service was written in python so all I had to do was take it and add it into /Text Filters in BBEdit’s Application Support directory. All it is is a simple find-and-replace for the [ ] Task string I use for a to-do and replace it with [X] Task. But this helped me figure out that I could manipulate other strings with more powerful scripts.

I haven’t gotten too deep in this yet but one that I did write helps me format markdown headers. Often I’ll just write and then go back and format. BBEdit’s clippings can work well for markdown formatting but you can’t put much logic in them.

What I wanted to do was select a string and wrap it with a # to make a heading like this:

# Heading 1 #

The problem with clippings is that since I wanted the spaces, wrapping a selection with a clipping and then repeating would add extra spaces in between the # like this:

# # Heading 2 # #

That’s neither attractive or valid markdown. This python script (which probably could be simplified) does the job perfectly and works for multiple lines too.

import sys
import re

for a in sys.stdin:
  b = re.sub(r'^(.*?)$', r'# \1 #', a)
  c = b.strip("\n")
  d = c.replace('# #', '##')
  print d 

With this, I can select a heading or several lines of headings and it will wrap them in #s. Tap the keystroke again and it will add another one before and after without the space. It’s the little things that make me happy.

Download Markdown Heading Text Filter

Copy Slug to Clipboard: The way Second Crack (the blogging engine I use) works is it takes the file name of the post as the URL slug. I usually have the title of the post decided and written at the top before actually saving the file. This service which Brett Terpstra original shared with me will take the selected text—in this case my title—and turn it into a slug-ready string on your clipboard.

I’ll copy my title “Scripts and Hacks Grab Bag” and run the service on it. Hit Save and the save prompt will open then paste and in goes the slug scripts-and-hacks-grab-bag. Boom. Off it syncs with Dropbox and is ready to publish.

Download Copy Slug to Clipboard Service

That’s it for now but I’ll probably do another one of these grab bags when I’ve built up a bunch more interesting hacks to share. I hope that these are useful or at least inspire you to find ways to improve the efficiency of your work.

Update: Cleaned up the Applescript to make and open a file. Thanks to reader Jan-Yves for the quoted form of tip.