Sometimes I want to run AppleScripts automatically on a certain schedule – either at a specific time, or at an interval. I’m getting the hang of writing AppleScripts, but I had no idea how to run it on a schedule… until I found Lingon.
Lingon has a simple interface. You enter a command to run, and then set a schedule. That’s it. For AppleScripts, you can use the file browser to choose a script file to run. Lingon will then prefix the script command with /usr/bin/osascript
to ensure it runs as a script. Hit “Save & Load” and you’re done.
How I use scheduled AppleScripts
I use DEVONthink as an information manager. It has excellent AppleScript support, so I’m using Lingon to schedule my scripts. Specifically right now I’m using it for RSS. I have all my RSS feeds in a single DEVONthink database. To view RSS items, I have a smart group that looks for documents in the feeds folder:
I intended to set up this smart group of feed items so that I could process them on the go – simply going through the list and deleting any that I’m not interested in. The mobile app DEVONthink To Go currently has a limitation on smart groups – it only supports pre-defined smart groups, you can’t add your own. Scripting to the rescue…
I created a simple script (“move RSS items” below) which examines the smart group, and moves any items to a “process” folder. It then syncs to my iPhone, where I can delete any unwanted items. I move any items I want to keep into a “file to links database” group, which gets picked up by another automated script (“file bookmarks to links database” below).
Now that I’m getting more comfortable with AppleScript, I have lots more ideas for how to automate my workflow by running scheduled AppleScripts with Lingon. Because of DEVONthink’s strong AppleScript support, I can gradually “teach” it to be an even more intelligent virtual assistant.
Hints
-
I like to reference UUIDs in my scripts, that way I can rename and move things around without breaking my scripts. The simplest way to grab an item’s UUID is to use the “copy item link” command and then paste it into your script (just be sure to remove the
x-devonthink://
prefix). -
Before saving your Lingon configuration, test it out at the command line. Copy the “What” field from Lingon’s configuration, including the
/usr/bin/osascript
prefix, and run it from Terminal. If it works, you can save your Lingon config and it will run automatically from now on.
“Move RSS items” script:
-- Note: You need to create a smart group that
-- finds RSS items. This is theGroup
-- You also need a destination group for the items
-- to be moved to. This is incomingGroup
tell application "DEVONthink Pro"
set incomingGroup to get record with uuid "8170252E-4446-4098-923A-5D7331BC33E2"
set theGroup to get record with uuid "E14BF768-E081-409E-8690-D0F25343B68C"
set theChildren to children of theGroup
repeat with theChild in theChildren
move record theChild to incomingGroup
set unread of theChild to false
end repeat
end tell
“File bookmarks to links database” script:
-- Note: You need a group that contains the
-- documents to file away (theGroup).
-- You need another group / database which is
-- the destination for the bookmarks (linksInbox)
--
-- This script creates a bookmark using the
-- original document's URL and name, files it
-- to the destination, and deletes the original
-- document.
tell application "DEVONthink Pro"
set linksInbox to get record with uuid "287E1736-517A-4038-BF97-B5418241349B"
set theGroup to get record with uuid "D6B2FB79-C280-41BA-B4B3-B7E9E07682CB"
set theRecords to children of theGroup
if (count of theRecords) is equal to 0 then
return
end if
repeat with theRecord in theRecords
set theUrl to URL of theRecord
if theUrl is not null then
set theParent to parent of theRecord
set theDatabase to database of theRecord
set theName to name of theRecord
create record with {name:theName, URL:theUrl, type:bookmark} in linksInbox
move record theRecord to trash group of theDatabase
end if
end repeat
end tell