Finding files with Google Desktop on Leopard (or, fun with AppleScript)

Friday, February 08, 2008 at 3:15 PM



Last spring I wrote a blog post about using AppleScript with Google Desktop. It described how you can combine the speed of Google Desktop with the power of AppleScript to search your Mac in a variety of ways.

Recently I was writing some scripts that use Google Desktop to search for files on Leopard and I ran into an issue. It appears that AppleScript on Leopard (Mac OS X 10.5.1) has some problems with the term "file", which is also a type in AppleScript. Google Desktop uses "file" as a type to search for, as in the following example:

search for "Hasselhoff" restrict to file

which should restrict my search to any file containing the term "Hasselhoff". This works great on Tiger, but on Leopard you get:

Google Desktop got an error: Can’t make file into type constant.

Well, according to the AppleScript manual, I should be able to turn the term "file" into its chevron-wrapped type and get the script to compile (you can get more info in Apple's documentation). For Google Desktop, that chevron-wrapped type would be «constant tyreFile».

So we get the ugly, but runnable:

search for "Hasselhoff" restrict to «constant tyreFile»

and when this is compiled Script Editor happily turns this back into:

search for "Hasselhoff" restrict to file

which appears great. The problem is that the next time you compile it you get back to:

Google Desktop got an error: Can’t make file into type constant.

Sigh. Even more depressing is that according to the AppleScript documentation, there is no way to coerce anything useful into a constant, so we can't store «constant tyreFile» as a string and coerce it at runtime. So what is a desperate AppleScripter to do? Luckily we can run another script at runtime, in the same namespace as our tell block, using the "run script" scripting addition from AppleScript's "Standard Additions". Now we can do something like this:

set restrictType to run script "«constant tyreFile»"
search for "Hasselhoff" restrict to
restrictType

It's not super fast, and it certainly isn't pretty, but it will compile and run consistently on both Tiger and Leopard. Here's hoping this will be fixed in the future, but until then, this technique should come in handy for other edgy AppleScript situations.