What tips and tricks do you have for debugging AppleScript? Is there a debugger? If not, what is the best way to insert "prints" to display the value of variables? Is there a way to "pretty print" more complicated data structures?
-
Have you tried the `log` command? I don't know when it became available. – Feuermurmel May 07 '21 at 19:03
6 Answers
The latest versions of xcode will let you create an AppleScript application but breakpoints in applescript don't work since Apple has discontinued support for AppleScript debugging in xcode.
Fallback: for simple "printf style" debugging you could use:
display dialog "my variable: " & myVar

- 28,968
- 18
- 162
- 169

- 8,717
- 6
- 48
- 50
- Script Debugger
- XCode
Getting the properties of an object (see below) to understand why it fails, when run from script editor. You can also use the class word, to see what class a property is. The Dictionary for an app is a good starting point.
One technique that often would have helped me, (and that I still sometimes do) is to tell something to return their properties, like this:
tell application "TextEdit" get properties end tell
Log statements and Console.app, for debugging of runtime events. (further below). You can ofcourse turn debugging on and off by setting a property
Below is a techniuqe I use for tracking runtime errors, in applets, mail rules, and what have you. When it fails, the error number and message is logged into TestDrive.log, and can be found in the left margin of Console.app…
tell application "TextEdit" try set a to text 0 of its name on error e number n my logit("OOPs: " & e & " " & n, "TestDrive") end try end tell to logit(log_string, log_file) do shell script ¬ "echo `date '+%Y-%m-%d %T: '`\"" & log_string & ¬ "\" >> $HOME/Library/Logs/" & log_file & ".log" end logit

- 22,495
- 29
- 154
- 227

- 1,400
- 13
- 10
-
`get properties` fails for **Mail** (`AppleEvent handler failed." number -10000`) so it does not seem to be generic, is there a more generic variant to retrieve scripting capabilities of applications? – dualed Oct 27 '14 at 10:35
If you're building any amount of AppleScripts, ScriptDebugger is the best tool I can recommend. Having said that...
Xcode is a free option that can be used to develop AppleScripts and can step through code with the debugger. The ability is primarily included so you can build Cocoa applications with AppleScript Studio, but you could use it for any AppleScript development.
If you're looking for something simpler, you might check out Smile, which isn't really a debugger, but does offer features useful for debugging that aren't available in the standard Script Editor.

- 4,662
- 2
- 33
- 55
-
2It seems Xcode 4 dropped the script debugger - http://stackoverflow.com/a/5535604/255961 – studgeek Dec 03 '12 at 23:44
If a display dialog is too small you can use TextEdit to show big returns:
tell application "TextEdit"
activate
make new document
set text of document 1 to myResults
end tell

- 314
- 4
- 10
To get the names of windows and other GUI elements and properties, I've found UI Browser invaluable. You can use it to inspect whatever you want to control with AppleScript to find the designations of the elements you want to control
Not free, but easily worth it for a serious developer.

- 42,571
- 24
- 140
- 158