20

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
Tyilo
  • 28,998
  • 40
  • 113
  • 198

4 Answers4

29

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

mcgrailm
  • 17,469
  • 22
  • 83
  • 129
  • 5
    $199 for an apple script debugger, no thanks. +1 for pointing it out, though. – Adam Eberlin Apr 10 '13 at 00:49
  • 1
    You can now download and use if free for 20 days, and after that some features are disabled but you can still use the "lite" version. – Vivi Feb 18 '21 at 05:22
2

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 !

Zitoun
  • 446
  • 3
  • 13
2

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!

Tyilo
  • 28,998
  • 40
  • 113
  • 198
geowar
  • 4,397
  • 1
  • 28
  • 24
  • it might be silly question, but where can we see that errormessage and error number log – MacDeveloper Aug 06 '15 at 09:17
  • Script Editor & Script Debugger both have console logs where that output will appear. When running stand-along scripts the logs go to the system console. – geowar Aug 06 '15 at 17:19
0

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?

Dan
  • 527
  • 1
  • 7
  • 25