How to get going with Hazel script?

Hi,

This is probably a most basic thing, but please bear with me.
If there’s a post explaining this, please point me to it. I found lots of references to Hazel, but the ones I looked at all contain code snippets but not an explanation on how or where to get this code into Hazel.

Suppose I want to use the script from this post: [url]Hazel rule script for importing/indexing to a specific group]

Then how do I get this into Hazel?
I’ve tried copying and pasting into textedit, save then import with Hazel, but this throws an error “The rules are either corrupt or not a valid rules file”, next I added a hazelrules extension, but this didn’t help either.

Thanks for helping out!

P.S. I’ve edited the file to look like this:

tell application "Finder"
   set _path to /Volumes/pt1/Users/erwin/DT/2014.dtBase2/
   set {_name, _extension} to {name, name extension} of theFile
   
   -- optional: hide extension in Finder
   -- set extension hidden of theFile to true
   
   -- optional: remove extension for DEVONthink displays
   -- set _name to text 1 thru -((count _extension) + 2) of _name
   -- set _extension to "." & _extension
end tell

-- Launch DEVONthink if it isn't already open.
tell application "System Events"
   if not (exists process "DEVONthink Pro Office") then
      tell application id "com.devon-technologies.thinkpro2" to activate
   end if
end tell

-- Import the file.

tell application id "com.devon-technologies.thinkpro2"
   set theGroup to get record with uuid "44F5A879-DF80-46B8-8045-7E4C644C036B"
   
   -- choose one option, Index or Import, comment out the other
   -- set theImport to import _path name _name to theGroup
   set theIndex to indicate _path to theGroup
end tell

Scripts are not imported into Hazel.

To use AppleScript with Hazel, you need to know how to configure Hazel to monitor a folder and then create rules to tell Hazel what actions to do when the contents of the monitored folder change. Among the actions Hazel can run in a rule is “Run AppleScript”. The AppleScript can either be “embedded” (you paste the script into a box that Hazel displays in the rule editor) or you can point Hazel to a file containing a compiled copy of the script.

This isn’t the best place to learn Hazel. You’ll find in-app help for Hazel (press the (?) on the Hazel panel in System Preferences) and a very helpful Hazel community (and Paul, the developer) at the Noodlesoft forum. There are numerous examples there where you can see how people use AppleScript with Hazel, get familiar with the concepts (and the manual), and then with that background follow along with the original posting you linked above to set up the script mentioned there.

Korm, thanks for helping out!

I got it working -
I’ll need to do some learning here.
Is there any way to kind of step through the script?

First I’ll need to learn to distinguish between various scripts, e.g. I wasn’t completely sure but suspected that ‘–’ serves to comment out a line.
Do you have any healthy pointers on that?

Thanks a ton!

What are you thinking of as a “step thorugh”?

Yes, anything that follows a “–” is a comment, either at the beginning of a line or later (i.e., you can put a comment after executable code in the same line)

Can’t help you there. You’ll need to debug using AppleScript editor or some other mechanism.

If you want to index then use these lines in the script

-- choose one option, Index or Import, comment out the other
   -- set theImport to import _path name _name to theGroup
   set theIndex to indicate _path to theGroup

If you want to import then use these lines

-- choose one option, Index or Import, comment out the other
   set theImport to import _path name _name to theGroup
   --- set theIndex to indicate _path to theGroup

Not sure what you’ve configured, but it’s sounds like you are having Hazel monitor the folder twice, or maybe run the rule twice, or some other mechanical error.

Debugging is the joy in the process :laughing:

Korm, thanks again for your patience and help :slight_smile:

Don’t know what happened, but I edited the message because I was able to debug my situatio, and consequently removed a couple of lines.

But somehow you still had them and answered to them - thanks a lot any way.

Stepping through:

  • I’ve done some VBscripting and remember from the VB script editor that I could step through a script, one line at a time.
    I’m not sure, but I also seem to remember I sometimes needed to put some ‘code’ to cause a pop-up or something to know where the script was or such.

I was wondering if some kind of similar method exists for Applesrcipt?

Ah, I suspected there was a VB issue in the background.

To do an effective debug with trace, breakpoints, and step-through you’d could look at a product Script Debugger from Late Night Software. It’s very pricey. With Yosemite, Apple includes Script Editor in the Utilities folder (pre-Yosemite it is called AppleScript Editor). Not much debugging support there. There are other scripting products, from Smile Software or Shane Stanley that support step debugging. Or perhaps you could work something out with Xcode.

I use Script Debugger, mainly, and AppleScript Objective C Explorer from Shane. I avoid Script Editor.

Thanks for sharing!

AppleScript is a quirky language to say the least so I would also definitely recommend
‘Learn AppleScript: The Comprehensive Guide to Scripting and Automation on Mac OS X’ by
Hamish Sanderson , Hanaan Rosenthal, third edition (the book or pdf, not the kindle edition).

Script Editor is fine for basic scripts, while it doesn’t have stepped debugging it does have quite nice logging

Frederiko

Hi Federiko,

Any specific reason why not to use the Kindle edition?
Also: Korm referred to some list of resources for learning Apple Script, however I searched but did not find it. Could you point me to it please?

Thanks a lot!
Erwin

I am not Jim.

Sorry - mixup with Bluefrog.