Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Debuging in AppleScript

Hi,


I'm looking for a simple way to debug in AppleScript.


I often code AppleScripts in Automator to interact with other actions but I have some problems to debug.


What is the best way to do that ?


Thx.

Posted on May 2, 2015 10:07 AM

Reply
Question marked as Best reply

Posted on May 4, 2015 10:47 AM

Don't know the best way.


I debug the old fashion way. I know it's 2015, but it's still the "print" statement or was it the write statement in Fortran.


Here is an example of the Applescript log statement. I think you will have to run the app separately from automator. I do no automate.


I wrote up a write to file "logging" function in applescript. I'll post later when I have more time to search for it 😊.


(*


It is easier to diagnose problems with debug information. I suggest adding log statements to your script to see what is going on. Here is an example.



Author: rccharles

For testing, run in the Script Editor.

1) Click on the Event Log tab to see the output from the log statement

2) Click on Run


*)



on run

-- Write a message into the event log.

log " --- Starting on " & ((current date) as string) & " --- "

display dialog "beginning..." giving up after 5 -- time in seconds

-- debug lines

set desktopPath to (path to desktop) as string

log "desktopPath = " & desktopPath

display dialog "all done..."

end run

6 replies
Question marked as Best reply

May 4, 2015 10:47 AM in response to Combo

Don't know the best way.


I debug the old fashion way. I know it's 2015, but it's still the "print" statement or was it the write statement in Fortran.


Here is an example of the Applescript log statement. I think you will have to run the app separately from automator. I do no automate.


I wrote up a write to file "logging" function in applescript. I'll post later when I have more time to search for it 😊.


(*


It is easier to diagnose problems with debug information. I suggest adding log statements to your script to see what is going on. Here is an example.



Author: rccharles

For testing, run in the Script Editor.

1) Click on the Event Log tab to see the output from the log statement

2) Click on Run


*)



on run

-- Write a message into the event log.

log " --- Starting on " & ((current date) as string) & " --- "

display dialog "beginning..." giving up after 5 -- time in seconds

-- debug lines

set desktopPath to (path to desktop) as string

log "desktopPath = " & desktopPath

display dialog "all done..."

end run

May 4, 2015 10:47 AM in response to Combo

Automator is a vehicle without steering, not to mention an event data recorder.


A simple way to debug AppleScript script is to run it in (Apple)Script Editor with some test code implemented in the script and investigate the value returned in result pane and/or event log pane as well as possible errors reported in (Apple)Script Editor.


Good luck,

H



PS. A typical AppleScript script in Run AppleScript action in Automator with the following construct:



on run {input, parameters} -- code to process input end run



can be translated to stand-alone script with the following construct:



on run set input to {} -- {} is to be replaced with list of objects received from previous action in Automator workflow -- code to process input end run



and be run in (Apple)Script Editor.

May 3, 2015 7:45 AM in response to Combo

Don't write a large AppleScript program and then try to debug it. Layout what you want to achieve on notepad paper so that you have a general road map, and then tackle small chunks of functionality until you are satisfied that the results are correct in the event/log panel at the bottom. Then move on to the next block of code. I have tried the first sentence, and am still in therapy. 😉 Use the previous suggestions for log and display dialog.


Have browser bookmarks to the AppleScript Language Guide, and MacScripter, so you can refer to syntax, and observe how certain problems are solved using AppleScript. Understand that some of the older MacScripter examples may need tweaks depending on Apple's recent changes to AppleScript.


I use MacScripter as a browser search argument:


MacScripter debug applescript

MacScripter how to debug applescript

MacScripter how to read a file into a variable


If you happen to have Xcode installed, you can look up AppleScript errors in the Terminal using this Bash script which will work on Mavericks and Yosemite:


./MacErrors.sh 1728


#!/bin/bash
#
# chmod +x MacErrors.sh
# Error values are provided as non-negative integers
# must supply a value to the script


usage () {
    echo "Usage: $0 error"
}


[[ $# -eq 0 ]] && usage && exit 1


MACERRORS="/Applications/Xcode.app/Contents/Developer/Platforms/\
MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/System/Library/\
Frameworks/CoreServices.framework/Versions/A/Frameworks/\
CarbonCore.framework/Versions/A/Headers/MacErrors.h"


egrep $1 "$MACERRORS"
exit 0

May 3, 2015 11:20 AM in response to Hiroto

Addenda.


Use log statement with due caution because it performs implicit coercions on many values.


Try the following and see what it yields:


E.g. 1.


set {n, m} to {0, "0"} log n log m return {n, m}



E.g. 2.


repeat with a in {1, 2, 3} log a log a's contents log a = a's contents return {a, a's contents} end repeat



E.g. 3.


set aa to {1, {2, 3}, 4} log aa return aa



I solely use return statement to check the value for this reason.



Regards,

H


EDIT: added E.g. 3.

May 3, 2015 6:08 PM in response to rccharles

Hello Robert,


What I wanted to say is that by log statement we cannot distinguish integer 1 from string "1", a reference to item 1 of {1, 2, 3} from item 1 of {1, 2, 3}, nested list such as {1, {2, 3}, 4} from flat list {1, 2, 3, 4} etc.


Why log statement has been flawed to such an extent for the entire history of AppleScript is totally beyond me. 😝



E.g. 1.


set {n, m} to {0, "0"} log n log m return {n, m}


-> (*0*) (*0*) Result: {0, "0"}




E.g. 2.


repeat with a in {1, 2, 3} log a log a's contents log a = a's contents return {a, a's contents} end repeat


-> (*1*) (*1*) (*false*) Result: {item 1 of {1, 2, 3}, 1}




E.g. 3.


set aa to {1, {2, 3}, 4} log aa return aa


-> (*1, 2, 3, 4*) Result: {1, {2, 3}, 4}




Event logs and results above are with AppleScript Editor 2.3 (118) and AppleScript 2.1.2 under OS X 10.6.8.


Later versions may (hopefully) vary...


Cheers,

H

Debuging in AppleScript

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.