How do I debug AppleScript?

DebuggingApplescript

Debugging Problem Overview


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?

Debugging Solutions


Solution 1 - Debugging

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

Solution 2 - Debugging

  1. Script Debugger
  2. XCode
  3. 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
  1. 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

Solution 3 - Debugging

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.

Solution 4 - Debugging

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

Source http://forums.macrumors.com/showthread.php?t=446171

Solution 5 - Debugging

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.

Solution 6 - Debugging

Use the log command. Example:

log "Hello world!"

The output can then be seen in the "Messages" windows in the official editor.

Reference

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDaryl SpitzerView Question on Stackoverflow
Solution 1 - DebuggingtuomassaloView Answer on Stackoverflow
Solution 2 - DebuggingMcUsrView Answer on Stackoverflow
Solution 3 - DebuggingChuckView Answer on Stackoverflow
Solution 4 - DebuggingHex Bob-ombView Answer on Stackoverflow
Solution 5 - DebuggingKlas MellbournView Answer on Stackoverflow
Solution 6 - DebuggingihatetoregisterView Answer on Stackoverflow