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: Shane Stanley <email@hidden>
- Date: Sat, 24 May 2014 12:46:06 +1000
On 24 May 2014, at 11:55 am, Stan Cleveland <email@hidden> wrote:
> The first skips having the Finder get and store the file name in a variable, and just uses a string instead to test the name. It's nearly all strings and is the fastest version yet:
>
> -- takes 0.9 seconds
> set filePath to "MacHD:Users:stanc:Desktop:Test:Test_00001.txt"
> tell application "Finder"
> repeat 1000 times
> tell file filePath to set fileExists to ((it exists) and (its name begins with "Test_00001.txt"))
> end repeat
> end tell
>
> The second variation has the Finder store a file reference in a variable for testing existence, but also uses a string to test the file name. This is the slowest by far:
>
> -- 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
>
> Apparently, the "tell fileRef" in every iteration of the loop forces the Finder to re-resolve the reference stored in the variable.
The thing is, if you're actually planning to check if a file exists in a real script, you'll generally only do it once at a time anyway, so for a more indicative timing comparison the resolution should probably always happen within the repeat loop. (The exception perhaps is in a loop where you're looking to see if a file has appeared, and timing is pretty pointless in that situation anyway, given that you're probably using a delay statement.)
Nonetheless, your code suggests that the Finder is woefully slow at resolving its own file references. That's why, for example, getting the selection "as alias list" is so much faster than getting a list of Finder references.
At this stage the Finder has so many black marks against its name that I don't really understand why people persist with it. Beginners, yes, but not those who have been around for a while. (And even beginners keep getting caught because they fail to grok the difference between paths and file objects.) It's not as if there aren't better and quicker alternatives for most of the things it does. (Getting the selection being the obvious -- and, yes, also buggy -- exception.)
--
Shane Stanley <email@hidden>
<www.macosxautomation.com/applescript/apps/>
_______________________________________________
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