How do I debug AppleScript?

Solution 1:

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:

  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
    
  4. 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:

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:

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