Daryl Spitzer
Daryl Spitzer

Reputation: 149364

How do I debug AppleScript?

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?

Upvotes: 57

Views: 61713

Answers (6)

norq
norq

Reputation: 1434

Use the log command. Example:

log "Hello world!"

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

Reference

Upvotes: 1

tuomassalo
tuomassalo

Reputation: 9101

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

Upvotes: 70

Klas Mellbourn
Klas Mellbourn

Reputation: 44377

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.

Upvotes: 0

Hex Bob-omb
Hex Bob-omb

Reputation: 314

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

Upvotes: 6

McUsr
McUsr

Reputation: 1409

  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
    

Upvotes: 24

Chuck
Chuck

Reputation: 4892

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.

Upvotes: 10

Related Questions