Folder actions have been a very useful tool in many scripters’ toolbox, but they do have a few drawbacks. Not the least of which is some tendency for the script to act upon files which haven’t yet finished copying to the hot folder. If you have run into this or some other problem inherent with folder actions, there is another way to achieve the hot folder concept.
This time out, I’m going to show you how to build a stay-open script with an on-idle handler. A script like this acts much like a full blown application in that it stays running and includes the ability to run commands on a regular interval. As you might imagine, this is extremely useful in many scenarios, not just watched folders. For example, wouldn’t it be cool to create your own System Event-like actions? With a stay-open script, you can have the script watch for the presence of your removable hard drive and act upon it. Sound exciting? It is. Let’s get started.
In the first screenshot, you’ll see that I’ve added what’s called an on-idle handler. This construct is unique in that it actually has two parts. The first being the on/end on statements.
The second being a line containing a ‘return’ command followed by an integer. This line is what makes an idle handler run its commands at an interval. The integer represents the interval in seconds. In this case, we’ll have our commands run every 5 seconds. (See below.)
Now that we’ve set up the most crucial portion of our code, we’re ready to make our script actually do something at each 5-second interval. Since I introduced this edition with a suggested alternative for creating watched folders, that’s what we’ll do here. In the next screenshot, the first few lines set up the folder we’d like to watch. In this case, I’m going on the pre-created folder called some_folder located at the root of my startup hard disk. If you create a folder there at your own hard drive called some_folder, this script will work as-is since we’re getting the name of the startup disk dynamically at the beginning of our script.
Next, we’ll add a final line directed at the Finder to set up the current number of items in some_folder. This number (itemCount) will be used to determine whether or not new items have been added to the folder the next time our 5-second interval rolls around.
Next, we’ll add an if/then conditional statement to instruct our script on what to do if it finds that itemCount is suddenly not zero. In this case, we’re just using a display dialog.
Finally, it’s time to save our handy dandy watched folder script. Pay close attention to the dialog options and set them as illustrated in our next screenshot.
Note we’ve chosen “stay open” and enabled “never show startup screen”.
The screenshot below shows how your new script applet will appear in the dock just like any other application. For those of you who would prefer your stay open script not appear in the dock, check out Dockless by Humongous Elephants and Tigers .
A couple of additional notes on idle handlers and stay-open scripts:
- When our idle/stay-open script’s icon is double-clicked from within the Finder, our idle code is executed for the first time. Then the 5-second interval takes over. However, if we want something specific to happen upon first launch, we need to add commands outside the idle handler. To keep things clear and easy to understand, I recommend adding an explicit on-run handler for this sort of thing. (Although, the run handler is implied when adding commands outside the idle handler.) An instance where on-run code might be useful in this case might be to have the user choose the watched folder, instead of hard coding it in like we’ve done in the above example.
- Take care not to make your idle interval too frequent, if your idle code is complex or lengthy. The more frequent the idle interval, the more CPU cycles your script will eat while running. This is not such a big deal with one watched folder, but if you have one script watching several folders, the overhead can get rather heavy and bog things down. Particularly if the script is running on a workstation and not an unattended server.
I hope you find this tutorial and the techniques presented here to be useful devices in your scripting adventures. Thanks for reading and we’ll see you next month!