Debugging Hazel AppleScript

I would like to debug an AppleScript that will be called from Hazel, but when I enable debugging, I can’t select the AppleScript within Hazel.

How do I get this to work? Should it work?

Hazel, is a prefpane which does not have an AppleScript Dictionary. As such, the prefpane itself is not directly scriptable. However in Hazel’s rules section there are multiple mentions of AppleScript so you may be able to construct a rule which triggers an external (to Hazel) AppleScript you’ve written, debugged and stored somewhere on your system.

Since I am unable to locate a Hazel User Manual I’m not able to tell you how this might be accomplished except by using trial and error.

Here’s the docs for Writing AppleScript in Hazel Hazel won’t recognize the External AppleScript if Script Debugger is set to Debug mode. Here is my Hazel Rule that invokes the external AppleScript highlighted in red. The AppleScript runs and returns control to Hazel, but as far as being able to see what the objects passed in are or debug the script I haven’t been able to use Script Debugger at all.

Here’s my script:

<code>on hazelProcessFile(theFile, inputAttributes)
	set DLOG_TARGETS to {"~/Desktop/AppleScript.log"}
	dlog(front window of application (path to frontmost application as text))
	dlog({"theFile", theFile})
	log (toStrint(inputAttributes))
	tell application id "DNtp"
		set theTag to item 1 of inputAttributes
		set theTagList to {text of theTag}
		set theRecords to lookup records with tags theTagList
		set theGroup to item 1 of theRecords
	end tell
		set thePath to theFile as text
		if thePath does not end with ".download:" and thePath does not end with ".crdownload:" then
			tell application id "DNtp" to import thePath to theGroup
			display dialog theGroup
		end if
	end try
end hazelProcessFile</code>

There does seem to be an issue with debugging like that – we’re looking into it.

Sadly, this is a Hazel limitation and there is little we can do about it within Script Debugger.

At a technical level, Hazel appears to be loading scripts using the AppleScript OSA scripting system (component) which limits them to opening and running only AppleScript scripts. Were they to use the Generic OSA scripting system then they could support any OSA scripting system. This is an issue for Script Debugger because when you enable debugging, Script Debugger employs its own OSA scripting system to facilitate the debugging externally.

Developer of Hazel here; I was asked to pop in. Hazel uses the regular OSAKit API. Unless you are referring to something else, this one does support other languages (for instance JavaScript which is also supported by Hazel). Could you elaborate a bit more on this?

Thanks for joining us. I apologize for taking a while to reply, but I’ve been ill.

Rereading @mtennes’s responses to the OP, it appears that Hazel is not permitting users to select documents saved by Script Debugger when debugging is enabled.

As for OSAKit, it appears to be doing the right thing in terms of loading and executing scripts. Here is a sample Xcode project that loads and executes a simple script (Sample.scpt) with Script Debugger’s debugging enabled: (276.8 KB)

And here’s a video showing the results I see here when I run this sample application:

NOTE: In testing this, I did find a Script Debugger bug. If the Sample.scpt script is open in Script Debugger when the OSAKitTest app runs, then debugging proceeds as it is supposed to. However, if Script Debugger’s new document choose panel is the top-most window, then external debugging fails silently. I have a fix for this issue in hand which will become part of the next Script Debugger maintenance release.

I’ve downloaded the Sample Xcode project you provided, but I am not seeing the same behavior. Script Debugger NEVER comes to the front or has control at anytime. I have set breakpoints in Xcode at the script?.execute line. I have tried each of the files below, the first being the one in the Xcode project you provided (thank you) and the second being the scriptURL object (file) in the “ViewController.swift” file when it’s executed. Neither works. I obviously am missing something and if this simple example doesn’t work, it’s no wonder that the more complex Hazel case doesn’t.


I should point out that I don’t think the AppleScript is running correctly wherever it is, because I don’t get the correct return result either. As shown below the returned value is always nil, not 600. Is there some Xcode property that I need to set?

       resuleDescriptor:  nil

I’m not really sure what to suggest. You can make a recording of your actions and the responses you are seeing and I’ll see if I notice something important.

Please ensure that Sample.scpt is open, and current, in Script Debugger before you run the OSAKitTest app from Xcode (this requirement will go away with the next Script Debugger 6 maintenance release).

Here’s what I see.

I do see that the script is failing to load with this error on my end:

OSAScriptErrorNumberKey = "-2147450879”

Doing a quick search doesn’t bring up anything. Does that error code ring any bells on your end?

In any case, I do have the sample script exhibiting the problem so I’ll look into that aspect of it here.


Doesn’t ring my bell which just goes ding dong, remember I’m a newbie to Script Debugger.

Looking at your recording, it appears that the script is failing to load. Because the script object is nil, nothing will happen when the OSAKitTest app goes to execute the script. Why this is happening is not clear. You can check your console log to see of anything useful was logged by the OSAKitTest application.

When Script Debugger launches, it installs a file called Script Debugger.component in the ~/Library/Components folder which facilitates external debugging. I can so no reason why this file would be missing since Script Debugger is running in your video, but it might be worth checking to make sure its there.

That is not an error number I recognize. Generally OSA error codes are small 16-bit values. The only 32-bit errors are these, but they don’t match:

/* ComponentError codes*/
enum {
  badComponentInstance          = (int)0x80008001, /* when cast to an OSErr this is -32767*/
  badComponentSelector          = (int)0x80008002 /* when cast to an OSErr this is -32766*/

Please check your console log and see if the OSAKitTest application logged anything. There may be a clue to the cause of the failure there.

Sorry, wasn’t clear before. It’s fine in the sample project but in Hazel, I get that error. Nothing logged so I’ll have to play with it more when I have some time.

Well, well, well… this might be a problem!```
Michael@Zadok:/Users/Michael>>ps -ax | grep cript
3310 ?? 0:31.56 /Applications/Script Debugger
3357 ttys000 0:00.00 grep cript
Michael@Zadok:/Users/Michael>>ls -la ~/Library/Comp*
total 0
drwx------+ 3 Michael staff 102 Mar 27 2012 .
drwx------@ 87 Michael staff 2958 Oct 28 09:08 …
-rw------- 1 Michael staff 0 Mar 27 2012 .localized
Michael@Zadok:/Users/Michael>>ls -la /Library/Components/
total 0
drwxr-xr-x 2 root wheel 68 Jul 30 17:59 .
drwxr-xr-x+ 67 root wheel 2278 Sep 8 18:14 …

When I do as you have done I get these results:

mark-2:~ mall$ ls -la ~/Library/Comp*
total 16
drwx------   6 mall  staff   204 26 Oct 22:32 .
drwx------  92 mall  staff  3128 28 Oct 12:31 ..
-rw-r--r--@  1 mall  staff  6148 19 Jun 08:25 .DS_Store
drwxr-xr-x   3 mall  staff   102 26 Oct 22:24 Script Debugger.component

total 0
drwx------   3 mall  staff   102 16 Aug  2010 .
drwx------  92 mall  staff  3128 28 Oct 12:31 ..
-rw-------   1 mall  staff     0 16 Aug  2010 .localized
mark-2:~ mall$ 

If you lack a ~/Library/Components folder, then Script Debugger will be unable to install its Script Debugger.component file and external debugging will be inoperative.

Creating a Components directory where one was lacking solved all my problems. Including debugging scripts in Hazel. FEATURE REQUEST: Prompt users to create directory or do it automatically when required directory does not exist.

Fair enough, but this issue has never arisen before. The ~/Library/Components directory has, so far, always been there. I’m curious to know if you removed it at some point or if your system never had this directory.

FWIW, I just checked two test volumes. One I had started with a clean install of an early Sierra beta and updated from then on until 10.12 release, and it has a Components folder. The other was a clean install with either the Sierra GM or build before (I can’t remember exactly) and updated to 10.12 release, and it doesn’t have one.

That suggests a recent change…