How-to: Start and stop Leech on a schedule

Note: This article applies to Leech 2; Leech 3 has built-in scheduling support.

One feature that Leech, our simple download assistant, doesn’t offer is scheduling. For many users, this isn’t an issue, as they can use their internet connection whenever they wish. There is a subset of users, though, who have internet connections that may offer more speed at night, or not have capacity limits at night, or may allow unlimited downloading only at night.

A future version of Leech may offer scheduling, but until that comes to be, you can use AppleScript and a scheduling application to handle the task. It’s not overly complicated, but does require a bit of work in Leech and AppleScript.

The first step is to have Leech queue up all download requests, so you can just copy and paste URLs into it during the day, then let it run at night. To put Leech in queued mode, just make sure there’s not a checkmark by the Queue > Start Downloads Automatically menu item, as seen in the image at right.

Once that’s done, you can add URLs to Leech throughout the day, but they won’t start downloading. Next, you’ll need to create two AppleScripts, one to start those queued downloads, and the other to pause them again.

The scripts

Open AppleScript Editor, in Applications > Utilities, then copy and paste this code:

tell application "System Events"
    if (exists (processes where name is "Leech")) then
        tell application "Leech" to activate
        key code 125 using {command down}
    end if
end tell

This is the “start” script, which will tell Leech to start downloading your queued URLs. Note that if you don’t have any queued downloads, the script won’t do anything (as it can’t start a queue that doesn’t exist.)

Save this script somewhere (such as your user’s Documents folder), with a reasonable filename (Start Leech). Do not close this window, though, as we’ll be revisiting it.

Next, open a new AppleScript Editor window (via Command-N, or File > New). In the new window, paste the following code:

tell application "System Events"
    if (exists (processes where name is "Leech")) then
        tell application "Leech" to activate
        keystroke "P" using {shift down,command down}
    end if
end tell

This is the “stop” script, which will put Leech back into pause mode. Save this script (Stop Leech) into the same location as your other script—and as before, leave the window open.

Scheduling the scripts

Now that you’ve got two scripts, all that’s left to do is to have them run on your schedule. I’ll explain how to do this via OS X’s Calendar application; the directions will differ if you choose to use cron or launchd or some other solution.

In order to use Calendar, the first thing you need to do is convert your scripts to applications, because Calendar can’t run raw AppleScripts (iCal could, but it no longer exists).

In one open editor window, hold down the Option key, then select File > Save As. In the window that appears, change the File Format pop-up to Application, choose a name for your app (it can be the same as the raw script, if you want, as they have different extensions), and save the program.

Do the same for the other window, so that you’ve now saved two application versions of your scripts. Here’s how my setup looks, with the applications (left) and original scripts (right):

Once the application versions have been saved, launch Calendar and add a new event; the basics don’t matter, as you’ll be editing it. Might as well name it Start Leech, though, to help you visually see each task on your schedule.

When the editing window pops up, set the event start time at or after the time when your internet goes to unlimited mode. You don’t need a long-duration event, so set it for five minutes or similar. Then click on the date to open the scheduling details for the event. Set the “repeat” to whatever matches your schedule—daily, weekends only, whatever.

Next, click on None next to “alert” to set the alert for this event. Pick Custom from the pop-up menu; this will open another window (this is progress, really). In that new window, set the first pop-up to “Open file,” then pick “Other” in the second pop-up.

This will then show the standard Open File dialog; navigate to the folder where you saved the Start Leech (or whatever you named it) application. Select the application (not the script), then click the Select button to close the Open File dialog.

Finally, change the third pop-up menu (the one to set the time of the alert) to “At time of event,” then click OK. The screenshot at right shows how my window looked, correctly configured, just before I clicked the OK button.

You’ve now created a recurring event to start Leech’s downloads queue at your preferred time on your preferred schedule. Repeat the process with a new event called Stop Leech. Set this one to be triggered sometime before your unlimited internet usage ends, and point it at the Stop Leech application.

Wrapping it all up

With the scripts, application, and scheduled events created, you’re basically done. Go ahead and launch Leech and start queueing your downloads. When your defined start time arrives, Leech should automatically start your downloads, and then pause again near the end of your free usage period. (You may want to test things first by changing the start/stop times on your scheduled events to a time when you’ll be at the computer, just so you can see that they do work.)

We realize this is somewhat of a stopgap measure for true scheduling in Leech. However, in testing, it works well, and we hope those of you with time-based internet connectivity find it useful.

No Responses to “How-to: Start and stop Leech on a schedule”

  1. PlatinumBob says:

    Super handy tip, and very easy to follow. Trying it out tonight. I lost faith in Leech last week, but this blog post got me back onto it.

    Hopefully this works, and no more feature-bloat apps like Folx or iGetter will be necessary.
    Will report back.

  2. PlatinumBob says:

    Fantastic. Worked a treat, I watched it start to be sure, only realised this morning I could have tested it at any time yesterday. Oh well, at least Leech is back on top!

    Suggestion: while the calendar stuff is straightforward, there are plenty of non-technical folks out there who may be intimidated by copy and pasting code, saving as application etc.,

    So why not provide the start-stop applications to download?
    Here’s a Dropbox link to a zip containing the apps I made last night.
    https://db.tt/QVp8I5r6

  3. Rob Griffiths says:

    We’re a bit leery of posting compiled AppleScripts for users to blindly download and install. Yes, they can open them and look at the code, but if they’re going to do that, it’s not that much harder to copy and paste from here into AppleScript Editor.

    I appreciate the Dropbox link, and if anyone wants the compiled versions, they can grab them from there.

    thanks;
    -rob.