Re: Checking on the existence of a file
Re: Checking on the existence of a file
- Subject: Re: Checking on the existence of a file
- From: Christopher Nebel <email@hidden>
- Date: Thu, 01 Nov 2012 14:06:33 -0700
On Oct 28, 2012, at 8:01 AM, koenig.yvan <email@hidden> wrote:
> Le 28/10/2012 à 15:13, Luther Fuller <email@hidden> a écrit :
>
>> On Oct 28, 2012, at 8:18 AM, koenig.yvan wrote:
>>
>>> No.
>>> Chris Nebel explained that checking the existence of an alias was linked to some features of the operating system which were not guaranteed to be valid in future.
>>> My understanding is that it may be broken if Apple switch from Hfs+ to an other file system as it was rumored several time (and we may be sure that they will switch one day).
>>
>> Mmmmm. I use this ...
>>
>> tell application "Finder"
>> if (exists alias file linkName of mailboxFolder) then
>>
>> very often in a script. Should I be worried?
>
> Ask to Chris Nebel ;-)
>
> But honestly what's the need to test if an alias exists.
>
> tell application "Finder"
> if (exists file linkName of mailboxFolder) then
>
> will do the trick.
>
> If you want details about Chris Nebel explanations, search in the archives.
Oh, what the heck.
Using the Finder's "exists" command to test for existence of a file is perfectly fine; that's what it was designed to do. (Though I'd point out that asking it "exists alias file ...", since Finder knows about "alias files" as distinct from other kinds of files, will tell if you if that item exists *and* is an alias file. If you just wanted to know whether or not there was a file there of any type, you'd use "exists file ..."; for any sort of item at all, file or folder, use "exists item ...".) If for some reason you feel icky using Finder, System Events' Disk-Folder-File suite can substitute in many cases.
Using "as alias" inside a "try", or "the alias trick" as I call it, relies on the semantics of the Alias Manager. AppleScript's "alias" objects are fairly thin shims over Alias Manager alias records, and since the API doesn't allow creating an alias record for a file that doesn't exist, so you get an error which is visible in AppleScript. People discovered this a long time ago as a clever way to avoid sending commands to Finder, which could be a performance bottleneck if you were doing it often enough.
I don't like the alias trick much, because (a) it obscures the intent of the code, and (b) it relies on what is effectively an implementation detail. Basically, I consider it in bad taste. However, practically speaking, it's unlikely to break any time soon, though the Alias Manager is deprecated these days, and even if the Alias Manager did break, we'd try to preserve that particular behavior because so many people use it.
tl;dr: use the alias trick if you must, it probably won't hurt you (much), but prefer Finder or System Events' "exists".
--Chris Nebel
AppleScript Engineering
_______________________________________________
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