How to Test Hazel AppleScripts in AppleScript Editor

Tags: , ,

Bottom Line: You can use AppleScript Editor to test your AppleScripts destined for Hazel with this little trick.

I frequently post about Hazel and the hundreds of menial, repetitive tasks I can get it to take care of for me (see my posts tagged “automation”). Although it has a great GUI interface for getting some serious work done, I often end up embedding a little AppleScript or shell script.

Because Hazel’s job is to act when it notices a new file in a folder it’s set to monitor, it can be a little frustrating testing and debugging these AppleScripts as I write them. It’s smart enough to not run multiple times on the same file (unless the file changes), but this ends up being a problem when it comes to testing. You drop the file in the folder, the script doesn’t run correctly, then you have to drag it out of the folder and back in again for the next round of testing. Lame, especially or a keyboard navigation junkie like me.

However, I’ve found a little trick that really speeds things up. You might have noticed that Hazel likes to refer to the file being processed as “theFile.” Well, you can just add an alias “theFile” to the top of the script and it will run in AppleScript Editor just like in Hazel, which means you can just command + R to run it, tweak more, and repeat, no mousing around or GUI moving files necessary.

As I’ve tried to indicate, you can just use the “property theFile” part for testing a script that will be embedded in Hazel (delete or comment the rest), or include the whole hazelprocessfile jazz for testing external scripts that Hazel will reference. Just uncomment the relevant parts and test away. The fully commented script, as you see it now, is how it should look after testing, when it’s ready for prime-time. (I recommend you keep the parts commented out instead of erasing them in case you need to test more in the future.)

Enjoy!

  • Joe

    When testing an embedded script in Hazel, I tried to use

    property theFile : alias “Users:joe:Documents:SnapScan:2013_05_13_17_04_00.pdf”

    Clicking command+R causes an error ‘bonk’ sound. Saving the script, then clicking OK to save the whole rule including the script with that lines causes “Rule Error: At least one of the actions has an empty field or an error. Please fill in the field, correct the error or remove the action.” to appear. Removing the line removes this error.

    Is there an error in how I’m doing this, or has Hazel changed so your method no longer works?

    • http://n8henrie.com/ Nathan Henrie

      It sounds like you’re doing it wrong. Sorry if my comments in the script above aren’t totally clear — the “property theFile” part should be commented out for the script to function in Hazel. Their purpose, when not commented, is to allow testing a rule in AppleScript Editor (which gives more useful debugging information). Once the script is running properly in AppleScript Editor, I think re-comment that line and have Hazel run it as an external script.

      Let me know if you need further clarification.