Fixing a corrupted AppleScript file

Recently I had a problem with some AppleScript .scpt files being somehow corrupted such that opening them in a script editor (Apple’s or Late Night Software’s Script Debugger) rendered them as ‘garbage’ instead of a readable script. Actually, it appeared to be the compiled code being rendered as plain text: button labels, text properties, etc. were readable, but the commands, keywords, etc were not.

The key to identifying this situation is that the characters “FasdUAS ” appear near the top of the text.

Using Spotlight to view the file showed the script correctly, so I knew the information to recover it was ‘in there’ somewhere. But the Spotlight window provides no way to copy the data (jn 10.10 at least, which is where I am).

The solution to recovering these files is a bit bizarre: use Spotlight to ‘find’ the file, and copy the compiled script from the preview in that window. Paste that into a new script, recompile, and save.

Note: my first thought was to paste the text back into the corrupted script and compile that, so I wouldn’t have multiple files floating around. That seemed to work except that reopening the file opened as text, requiring a recompile each time. Clearly there’s something about the file that thinks it is plain text (probably .applescript) and finding that might be the key to repairing the original file.

But that’s a task for another day.

google_pluslinkedingoogle_pluslinkedinby feather

Fixing the eppc protocol

When Apple released Security Update 2015-002 (https://support.apple.com/en-us/HT204413) they included a fix for the “FREAK” exploit. Unfortunately, that fix broke the eppc protocol. Fortunately, they fixed it in a subsequent patch for Mavericks. Unfortunately, for Lion/Mountain Lion you have to download a separate patch at https://support.apple.com/en-us/HT201393. Fortunately, it’s an easy patch. Unfortunately, Apple never released a similar patch for Snow Leopard. Fortunately, somebody else did. Unfortunately, I can’t remember who. Fortunately, I still have it and you can get it here.

See also this MacScripter Discussion.

google_pluslinkedingoogle_pluslinkedinby feather

Another AppleScript Tidbit

When intentionally generating an error in AppleScript you can supply the error message and number (and probably the other parameters, if you care about them). If this error is not trapped by a try handler, it is passed back to the caller.

What I discovered today is this: if you generate an error and give it error number 0, Script Debugger does not report the error in its standard error sheet. (AppleScript Editor does report the error.)

I’m not sure if this is an intentional bit of niceness — it gives you a way of stopping the script without being bothered by a dialog — or a mistake based on the fact that, in Cocoa, et. al. “error 0” means noErr.

google_pluslinkedingoogle_pluslinkedinby feather

AppleScript Text Sort via Unix

Sometimes you need to sort a list before presenting it to the user via “choose from list.” This is particularly important when working with Helix RADE, as the list of icons is sorted by selection order, not by name.

While various methods have been put forward, none rival the speed of the Unix ‘sort’ command. The following code is modified from a hint found at http://hints.macworld.com/article.php?story=20040513173003941.

on sortItems(itemList)
      -- modded from http://hints.macworld.com/article.php?story=20040513173003941
   local listString, newString, newList
   set oldDelims to AppleScript's text item delimiters
   set AppleScript's text item delimiters to {ASCII character 10} -- always a linefeed
   set listString to (itemList as string)
   set newString to do shell script "echo " & ¬
      quoted form of listString & " | sort -g" -- add -r for reverse
   set newList to (paragraphs of newString)
   set AppleScript's text item delimiters to oldDelims
   return newList
end sortItems
google_pluslinkedingoogle_pluslinkedinby feather