Schedule your AppleScripts with Lingon

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:

feed smartgroup screenshot.png

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

A good tip and excellent real world examples.
Another app for launchd creation is Soma Zone’s LaunchControl.

Yep I saw that one too, but the interface scared me away :slight_smile: It’s supposed to be a lot more powerful… so maybe something I’ll look into at some point. Thanks for sharing it!

Even better is rolling your own launchd stuff without a utility… lots more fun (and challenging!) :mrgreen:

Haha seriously. I have no problem with cron, but launchd is a mystery to me. I’m so glad I found Lingon… I will be automating the hell out of DT and DA.

A future release will simplify this a lot, neither external tools nor scripts will be necessary in this case.