Re: Scripting Sherlock
Re: Scripting Sherlock
- Subject: Re: Scripting Sherlock
- From: Chris Nebel <email@hidden>
- Date: Thu, 07 Dec 2000 12:48:34 -0800
- Organization: Apple Computer, Inc.
Simon Forster wrote:
>
Is it possible to script Sherlock to search a sub-folder for every file
>
beginning with a tilde (~). This is easy to do in the Finder (see code
>
snippet below) but is damn slow. Sherlock should be quicker if it's possible
>
to script it to do this (but I believe it's not possible).
>
>
--Code snippet
>
repeat with i in (every folder of the entire contents of folder ((name of
>
startup disk) & ":WebSTAR") whose name begins with "~")
>
set end of folderList to (i as string)
>
end repeat
>
--End code snippet
Whenever you think a script is too slow, open up the event log and look at what
it's doing. In this case, there's an oddity in how AppleScript evaluates
repeat loops. The "in" part gets evaluated every time through the loop, so if
there are n matching items, there are n "every folder..." events sent to the
Finder. We'd like to only evaluate "every folder..." once, so instead try
something like this:
set x to (every folder of the entire contents of folder "WebSTAR" ,
of the startup disk whose name begins with "~")
repeat with i in x
set end of folderList to i as string
end
This is better, but still not that great. We only evaluate "every folder..."
once, but we get back a list of Finder object specifiers. To convert those to
strings, we have to send an event to the Finder for each one. Those events
take less time to process than "every folder...", but it's still not exactly
snappy. To really speed things up, add "as alias list" to the end of the first
"set" command. Then we get a list of aliases, which AppleScript can convert to
strings on its own.
--Chris Nebel
AppleScript Engineering