Re: Finder's "exists" command ignores leading zero's in filename
Re: Finder's "exists" command ignores leading zero's in filename
- Subject: Re: Finder's "exists" command ignores leading zero's in filename
- From: Nigel Garvey <email@hidden>
- Date: Sun, 25 May 2014 15:20:11 +0100
Stan Cleveland wrote on Fri, 23 May 2014 13:03:33 -0700:
>On May 23, 2014, at 10:07 AM, Nigel Garvey wrote:
>
>> ...the way the Finder compared names meant my workaround was one of
the
>>fastest.
>
>Hi Nigel,
>
>I noticed a 3x speedup when I adjusted your code to work with a
variable,
>rather than a hard-coded path.
>
>Times below are from Script Debugger's built-in timer (and were rounded
>after averaging several runs):
>
> -- hard-coded (4.50 seconds)
> tell application "Finder"
> repeat 1000 times
> tell file "Test_00001.txt" of folder "Test" of desktop to
set
>fileExists to ((it exists) and (its name begins with "Test_00001.txt"))
> end repeat
> end tell
>
> -- variable (1.50 seconds)
> set filePath to "MacHD:Users:stanc:Desktop:Test:Test_00001.txt"
> tell application "Finder"
> set theName to name of file filePath
> repeat 1000 times
> set fileExists to ((file filePath exists) and (name of file
>filePath begins with theName))
> end repeat
> end tell
>
>Rather interesting. Does this mean that AS processes variable data more
>efficiently than hard-coded data? It seems logical that parsing data
once
>would be faster than parsing it 1000 times.
Hi Stan.
As Shane pointed out, it shows that the Finder works faster (or less
slowly) when given a single specifier containing a full path than it
does with one of its own cumbersome references (whose resolution in this
case also includes the determination of the path to the desktop folder).
Hard-coding "MacHD:Users:stanc:Desktop:Test:Test_00001.txt" instead of
your variable 'filePath' makes very little difference to the timing.
While this information is useful, your tests aren't fair in the context
of one-off checks for files in the desktop hierarchy. In the first test
(which times my script), the location of the desktop folder is worked
out on every iteration of the repeat. In the second test, the full path
to the file — including the path to the desktop — is simply given, once,
before the repeat. So a major component of the task isn't properly
timed. The second test should look like this:
repeat 1000 times
set filePath to (path to desktop as text) & "Test:Test_00001.txt"
-- Or: tell application "Finder" to set filePath to (desktop as text) & "Test:Test_00001.txt"
tell application "Finder"
set theName to name of file filePath
set fileExists to ((file filePath exists) and (name of file filePath begins with theName))
end tell
end repeat
On my system, this gives a definite speed advantage to the first test.
There are other issues with the second test too, of course: it tries to
get the name from the file before the check for the file's existence and
it ignores the leading-zero problem the tested code's supposed to solve.
But these are probably excusable in a quick-test situation.
In your reply to Shane, you wrote:
> -- takes 13.50 seconds
> tell application "Finder"
> set fileRef to file "Test_00001.txt" of folder "Test" of desktop
> repeat 1000 times
> tell fileRef to set fileExists to ((it exists) and (its name
>begins with "Test_00001.txt"))
> end repeat
> end tell
This similarly tries to set 'fileRef' to a file whose existence is not
yet known and whose name could be confused with one having a different
number of leading zeros. An alternative, instead of having the Finder
try to access the file and return a full reference for it, would be to
set 'fileRef' to the reference written into the script:
tell application "Finder"
set fileRef to a reference to file "Test_00001.txt" of folder "Test" of desktop
repeat 1000 times
tell fileRef to set fileExists to ((it exists) and (its name begins with "Test_00001.txt"))
end repeat
end tell
This not only works when the file _doesn't_ exist, but also reduces the
test's running time considerably. But using 'a reference to' doesn't
speed things up as such: it merely prevents them from being slowed down
by the longer, full reference to the file. Using 'tell' with the
scripted reference, as in my original script, has the same effect as
long as you don't set a variable directly to 'it':
tell application "Finder"
set fileRef to file "Test_0001.txt" of folder "Test" of desktop
--> document file "Test_00001.txt" of folder "Test" of folder "Desktop" of folder "stanc" of folder "Users" of startup disk of application "Finder" (if it exists)
--> document file "Test_001.txt" of folder "Test" of folder "Desktop" of folder "stanc" of folder "Users" of startup disk of application "Finder" (if this exists instead and has the most leading zeros of any confusable matches)
--> error (if no match)
set fileRef to a reference to file "Test_00001.txt" of folder "Test" of desktop
--> file "Test_00001.txt" of folder "Test" of desktop of application "Finder" (in all cases)
tell file "Test_00001.txt" of folder "Test" of desktop to get it -- No variable set
--> file "Test_00001.txt" of folder "Test" of desktop of application "Finder"
tell file "Test_00001.txt" of folder "Test" of desktop to set fileRef to it
--> Same results as with 'set fileRef to file "Test_00001.txt" of folder "Test" of desktop'
tell file "Test_00001.txt" of folder "Test" of desktop to set fileRef to a reference to it
--> file "Test_00001.txt" of folder "Test" of desktop of application "Finder"
end tell
Another thing your tests show is that the Finder becomes slow and
unresponsive in manual use after they're run. ;)
NG
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden