Script won't run when saved as app!?

Last night I wrote a script to calculate UPC check digits. It works great when run from Applescript Editor, but doesn’t run at all as a stand alone app. I even tried creating an app with Platypus, and that didn’t work either.

I assume it’s some sort of “Tell” problem. If I put the following at the beginning of the script…

tell app "System Events"

… it will run as an app. Unfortunately, it gives me a spinning cursor that won’t go away until I click on the dialog window.

Any help would be appreciated. The entire script is below:

-- Check Digit calculator
-- calculates check digits for 12 and 13 digit UPCs

-- Enter UPC (must be 11 or 12 digits)
try
	repeat
		set anumber to 0
		repeat until (count of anumber) is equal to 11 or (count of anumber) is equal to 12
			display dialog "CHECK DIGIT CALCULATOR - Enter UPC" default answer "" buttons {"OK", "Cancel"} default button 1
			set anumber to text returned of result
			if (count of anumber) is not equal to 11 and (count of anumber) is not equal to 12 then beep
		end repeat
		
		set numberlist to every character of anumber as list
		set my_count to count of numberlist
		set my_digits to {}
		set digitsum to 0
		
		-- calculates check digit
		if my_count is 12 then
			set multiplylist to {1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3}
		else
			set multiplylist to {3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3}
		end if
		
		repeat with n from 1 to my_count
			set my_digits to my_digits & ((item n of numberlist) * (item n of multiplylist))
			set digitsum to (digitsum + (item n of my_digits))
		end repeat
		
		set roundup to (round (digitsum / 10) rounding up) * 10
		set checkdigit to roundup - digitsum
		
		-- displays result
		if my_count = 11 then
			display dialog "Check digit is " & checkdigit & "." & "

" & item 1 of numberlist & "-" & items 2 thru 6 of numberlist & "-" & items 7 thru 11 of numberlist & "-" & checkdigit buttons {"OK"} default button 1
			
		else
			display dialog "Check digit is " & checkdigit & "." & "

" & items 1 thru 3 of numberlist & "-" & item 4 of numberlist & "-" & items 5 thru 12 of numberlist & "-" & checkdigit buttons {"OK"} default button 1
			
		end if
	end repeat
on error -- do nothing, end program
end try

If you put a dialog in the ‘on error’ of the try it says: 0 doesn’t understand the count message, so the first repeat is failing to count anumber.

It works if you change the third line to: set anumber to “0”

Thanks Adam, that fixed it! Such a little thing, too…

Coercions that work “sometimes” are the most difficult kind to find, Torajima; I got it from the error message, but have spent many hours trying to find them when it was my own script.

Turns out this is caused by a semi-bug (in my view). Cocoa apps like the Script Editor and Script Debugger 4 will coerce “count 1234” to 0. An AppleScript aplet will not do that so it fails.