this blog is girtby.net

Posted
05 May 2005

Categories
nerd factor x provocation

Tags

2 Comments

Automated Bug Generation

AppleScript is the language for beginners. At least that's what Apple was telling us back in those heady System 7 days when it was introduced. But every time I attempt to use it, I struggle.

Let me show you what I mean. Open up the Script Editor and type the following:

POSIX file "foo"

In AppleScript terms, this is creating a reference to an as-yet-nonexistent file, specified using a POSIX path. More specifically, it's a special kind of reference called a "fileURL" (not to be confused with a "file:/" URL). If you believe Apple Tech Note 2022, fileURLs are to be preferred over the old skool (HFS) file specification, originally used in System 7 and possibly earlier. If this doesn't mean anything to you, don't worry about it.

The point is this. Look what happens when you compile this script.

Compiled Applescript #1

Amazing. This must be the only computer language in existence where the act of compiling actually changes your source code!

In this case the change is relatively benign. The code still runs and gives the same result. However it's pretty easy to find breakage. Let's try the following:

class of POSIX file "foo"

Here I'm attempting to verify my hypothesis about the type of file reference created. Again the code is changed upon compilation, but it does produce the desired result when run:

Compiled Applescript #2

Now here's the truly amazing thing. Let's try making a trivial change to the source — like adding a space and then removing it — then recompile and rerun:

AppleScript Runtime error

This is just incredible. Not only does the compiler change your source code, it actually breaks it so that it no longer runs!

I must say this really is a time-saver. Thanks to AppleScript I no longer need to spend hours introducing bugs into my code; now the compiler can do it for me!

2 Comments

Posted by
marxy
2005-05-05 00:14:35 -0500
#

Ahh but why not raise the amusing topic of simple string handling. Have you seen the recommended way to replace a substring?

on searchReplace(theText, SearchString, ReplaceString) set OldDelims to AppleScript's text item delimiters set AppleScript's text item delimiters to SearchString set newText to text items of theText set AppleScript's text item delimiters to ReplaceString set newText to newText as text set AppleScript's text item delimiters to OldDelims return newText end searchReplace

Now, of course, I could get BBEdit to do that replace for me, but that does seem like overkill.


Posted by
alastair
2005-05-05 00:14:35 -0500
#

Pretty crazy, for sure.

BTW I did come across the following little trick which does simplify your subroutine a little:

set {theOldDelimieter, text item delimiters} to {text item delimiters, SearchString}

Like python, kinda.