How to debug AppleScript error: "Expected “*)” but found end of script."
I am going mad trying to fix the following error in my AppleScript code:
What this error means is that I have opened a comment somewhere in the code with a (*
but I have forgot to close it with a *)
.
The code contains over 4000 lines and hundreds of comments. I have looked through the code for hours trying to find this missing closure but I cannot find it.
Is there some method or application that can inform me of the exact line of the code that is responsible for the error?
Solution 1:
There's a syntax highlighting package for AppleScript for the text editor Atom. It works better than the one provided with Script Editor, as it will correctly identify multiline comments, even if the script itself doesn't compile:
From there, it should be easy to identify the missing *)
.
Solution 2:
Update:
The reason why I could not find an instance of a (*
without a corresponding *)
is because every instance of (*
does in fact have a corresponding *)
. This wasn't the cause of the error, despite the misleading error message that claimed that it was.
The following comment was responsible for the error:
(*
"This is the text that I want to display
*)
You cannot put an odd number of double quotation marks in an AppleScript comment, apparently. That is to say, if you open a quote, you must close the quote.
I have no idea why this is. It's a comment, for Pete's sake. Why does AppleScript care if I forget to close a double quotation mark in a comment?
Also, this requirement is not mentioned in the AppleScript Language Guide - Comments.
If you are curious, I just tested it, and you are allowed to put a single quotation mark in a comment without closing it. For example,
(* 'This is the text that I want to display *)
produces no error.
Anyway, to answer my original question:
Is there some method or application that can inform me of the exact line of the code that is responsible for the error?
I managed to think of a method heuristically. It's crazy basic, and to many people it is probably obvious, but it never occurred to me as I was reading line after line for hours, looking for the missing *)
.
Using the following method, I found the exact line of code responsible for the error in under 5 minutes:
Create a new, blank AppleScript file.
-
Copy one half of your code to the new file.
-
If this code produces an error, then delete one half of this code. Compile the code.
If this code still produces an error, then delete one half of this code. Compile the code.
If this code no longer produces an error, then hit the Undo button and delete the other half of the code. Compile the code.
If this code does not produce an error, then hit the Undo button. This time, copy the other half of the original code (that is, copy the half that you did not copy the first time).
-
You get the idea. You are basically halving the code (by trial and error) until you can isolate the error down to a single line of code. There must be a technical term for this method, but I don't know the term.