General applescript file watcher

This script is from macworld, (osx only) but the way they posted is not only horribly un-user-friendly, it also wouldn’t compile without a lot of text massaging. I decided to put it up here to save you the trouble and explain 3 ways it can be used. What it does is to look at a file and see if it is the same size as last time it checked. You can change the interval based on the application, and you can tell it what to do once it has detected the file is “finished”

Here are three (of many) ways it can be used. These are all based on the script below, but they would require you to add code in the section below commented with “–this is where you put your “do when finished” code” The script as it is written will beep 3 times once the file is finished and 5 times if an error occurs. If you end up writing something useful to go with this post it here for others to see!

Firefox download monitor
Set this script to alert you when a big download finishes. A small interval ought to work out provided your connection doesn’t timeout.

Compressor secondary action
If for some reason you don’t want to run an applescript through the default compressor interface you can do it through here. This could be because you are watching it from another computer on the network, or maybe you have other specific needs. This will also allow you to open the exported file directly with an application, which compressor doesn’t let you do.

Export from fcp / after effects secondary action / alert
I guess AE dings when an export is done, but FCP doesn’t do a damn thing. If you run this on your export then it will do your bidding once the exported file is finished.

Here is the code!

on adding folder items to this_folder after receiving these_items
-- this is the standard intro for a folder action
repeat with f in theItems
-- wait for the item to be all there
set Was to 0
set isNow to 1
repeat while isNow ≠ Was
-- the basic idea is that the script loops until the file size is the same for more than 30 seconds. That means the file has finished copying.
set Was to size of (info for f)
-- this section is getting the file size of the video
delay 30 -- modify this for delay time
set isNow to size of (info for f)
-- this section is sampling the file size 30 seconds later
end repeat
--this is where you put your "do when finished" code
tell application "Finder"
beep 3
end tell
end repeat -- get next item f in thisFolder
on error error_message number error_number
if the error_number is not -128 then
tell application "Finder"
beep 5 --if the script errors out, beep 5x
end tell
end if
end try
end adding folder items to

Copy the code and paste it into Script Editor. Then you can save it, apply it to a folder as a folder action and you are on your way!

PS> as a side note, isn’t it weird that userfriendly doesn’t have a good antonym? anti-user? Userunfriendly?

~ by ross on February 1, 2010.