This Week's Sponsor:

Kolide

Ensures that if a device isn’t secure it can’t access your apps.  It’s Device Trust for Okta.


Enhancing Reminders with AppleScript and Macros

As The Omni Group keeps working on OmniFocus 2 for Mac and Apple continues seeding new betas of iOS 7 and OS X Mavericks to developers, I have been reconsidering Reminders’ simplicity and enjoying the built-in iCloud sync, which, unlike other types of iCloud, is working fine for me. However, two things I miss from OmniFocus are the possibility to integrate the app with a web browser through bookmarklets and the system-wide Quick Entry panel; I use both tools on a daily basis to easily save a browser’s tab into OmniFocus’ Inbox, or to bring up a text field where I can jot down an idea and know that, no matter the app I’m using, it’ll be saved into OmniFocus. Luckily for me, Apple’s Reminders app comes with a good AppleScript Dictionary, which is likely something that Reminders’ core mainstream audience won’t ever care about, but that we can leverage to extend the app’s capabilities and input areas beyond Mountain Lion’s leather-and-paper window.

Reminders’ AppleScript Dictionary (viewable in AppleScript Editor -> ⌘⇧O or through File > Open Dictionary…) allows us to fetch the app’s configured accounts, list names, and, more importantly, to create new reminders with specific parameters. Through AppleScript, we can programmatically create reminders that have specific names, attached notes, priorities, and dates. For this tutorial, I’m only going to focus on creating reminders with names and notes set by AppleScript, leaving other parameters to your discretion (and imagination for script personalizations).

My first requirement was to create a script that would send the current browser tab to Reminders using a webpage’s title as name and URL as note. With AppleScript and Google Chrome, that’s extremely easy to accomplish:

tell application "Google Chrome"
	set tabname to get title of active tab of window 1
	set taburl to get URL of active tab of window 1
	tell application "Reminders"
		set newremin to make new reminder
		set name of newremin to tabname
		set body of newremin to taburl
	end tell
end tell

For Safari users, the basic idea is the same, but note how the AppleScript Dictionary changes title to name and active to current for tabs. I have no idea why Google went for this minor difference when building AppleScript support into Chrome.

tell application "Safari"
	set tabname to get name of current tab of window 1
	set taburl to get URL of current tab of window 1
	tell application "Reminders"
		set newremin to make new reminder
		set name of newremin to tabname
		set body of newremin to taburl
	end tell
end tell

Because this is an AppleScript, it can be executed by a variety of modern OS X launchers that have the advantage of letting you set your own keyboard shortcuts for system-wide actions. For instance, here’s how you would create a workflow for Alfred to launch the script in Chrome with ⌘2 and post a notification upon completion:

As MacStories readers know, though, my preferred solution for this kind of OS X automation is Keyboard Maestro, which I believe has a clearer interface for workflows (I prefer the top-to-bottom approach to visualizing actions than Alfred’s “canvas” metaphor) and that comes with handy if/then blocks to check for active apps and frontmost windows. So here’s how the same AppleScript looks when converted to a Keyboard Maestro macro:

As you can see, the macro is only activated after I hit the ⌘1 shortcut and if Google Chrome is at the front (the macro is also assigned to a Chrome-only macro group); I’m using the same AppleScript, but I’ve added a native notification to be displayed after a reminder has been successfully added. Thanks to Keyboard Maestro 6, we can customize the text that will be shown by Notification Center using a mix of plain text and tokens dynamically generated by %ChromeTitle% and %ChromeURL%.

Replicating OmniFocus’ Quick Entry panel is a bit trickier, but still doable thanks to AppleScript. I have put together a script that, when launched, will tell OS X to display a dialog with an empty text field where we can type the name of the reminder we want to add. This dialog can be activated at any time, much like OmniFocus’ Quick Entry.

The empty text field is generated by the blank default answer and the dialog is automatically cancelled after 20 seconds thanks to giving up after. I think the nicest touch is the custom dialog icon, which is Reminders’ own icon fetched through with icon path to resource – the key is to tell AppleScript to go look in bundle.

tell application "System Events"
	display dialog "Create a new reminder" default answer "" cancel button "Cancel" giving up after 20 with icon path to resource "Reminders.icns" in bundle (path to application "Reminders")
	set reminTitle to text returned of result
	tell application "Reminders"
		set newremin to make new reminder
		set name of newremin to reminTitle
	end tell
end tell

The result is straightforward and barebones, but better than seeing a generic application icon:

The downside of interacting with dialogs generated by OS X is that you can’t have the fancy custom interface of OmniFocus’ panel; you’re limited in the number of buttons you can add; and it’s moderately complex to customize the dialog window with additional options for things like notes or dates (which I have purposefully advoided to keep the script as simple as possible). If you’re looking for more advanced AppleScript solutions that work around natural language parsing for date and time, I’m sure you can find some online.

You can download the Keyboard Maestro macros here.

Unlock More with Club MacStories

Founded in 2015, Club MacStories has delivered exclusive content every week for over six years.

In that time, members have enjoyed nearly 400 weekly and monthly newsletters packed with more of your favorite MacStories writing as well as Club-only podcasts, eBooks, discounts on apps, icons, and services. Join today, and you’ll get everything new that we publish every week, plus access to our entire archive of back issues and downloadable perks.

The Club expanded in 2021 with Club MacStories+ and Club Premier. Club MacStories+ members enjoy even more exclusive stories, a vibrant Discord community, a rotating roster of app discounts, and more. And, with Club Premier, you get everything we offer at every Club level plus an extended, ad-free version of our podcast AppStories that is delivered early each week in high-bitrate audio.

Choose the Club plan that’s right for you:

  • Club MacStories: Weekly and monthly newsletters via email and the web that are brimming with app collections, tips, automation workflows, longform writing, a Club-only podcast, periodic giveaways, and more;
  • Club MacStories+: Everything that Club MacStories offers, plus exclusive content like Federico’s Automation Academy and John’s Macintosh Desktop Experience, a powerful web app for searching and exploring over 6 years of content and creating custom RSS feeds of Club content, an active Discord community, and a rotating collection of discounts, and more;
  • Club Premier: Everything in from our other plans and AppStories+, an extended version of our flagship podcast that’s delivered early, ad-free, and in high-bitrate audio.