Tyilo
Tyilo

Reputation: 30102

AppleScript: on error (try) line number

Is it possible to get the line number, where the script threw an error?

Example:

try
    set a to "abc" + "123"
 on error line number num
    display dialog "Error on line number " & num
end try

Upvotes: 20

Views: 37958

Answers (4)

Zitoun
Zitoun

Reputation: 710

Satimage's Smile is of great help when it comes to debugging an applescript.

And it's free. Plus it's French (hehe).

Definitely a great tool !

Upvotes: 2

mcgrailm
mcgrailm

Reputation: 17640

i don't think so try statements look like this

try
    set a to "abc" + "123"
 on error errMsg
    display dialog "ERROR: " & errMsg
end try

but you could look at script debugger which will show you what line your error occurred on

another alternative is to get textmate which goes for $52 when it errors it gives you the line number and is also useful for writing code in many languages

Upvotes: 30

Dan
Dan

Reputation: 527

Late to the party here, but in respect to Script Debugger, here is a perhaps useful response from Mark Alldritt:

Yes, turn on Break On Exceptions. This cause the debugger to break at the point where an exception is thrown. The debugger also shows the sate of all known variable at the time the exception is thrown. You can then step into the 'on error' block.

Cheers -Mark

On 2013-01-24, at 8:43 AM, Dan wrote:

When a script throws an error in a Try block, is there any reasonable way to display the line where the error occurred?

Upvotes: 0

geowar
geowar

Reputation: 4447

Actually the on error syntax include the error number also (but no line number):

try
    set a to "abc" + "123"
on error errorMessage number errorNumber
    log ("errorMessage: " & errorMessage & ", errorNumber: " & errorNumber)
end try

You can use semaphores to mark your progress:

try
    ... your code here ...

    set lineNumber to "17"

    ... more code here

    set lineNumber to "18"

    ... more code here

on error errorMessage number errorNumber
    log ("(line #" & lineNumber & ") errorMessage: " & errorMessage & ", errorNumber: " & errorNumber)
end try

And I'll 2nd mcgrailm's recommendation for Script Debugger!

Upvotes: 3

Related Questions