Re: Check if a file exists fails
Re: Check if a file exists fails
- Subject: Re: Check if a file exists fails
- From: Christopher Nebel <email@hidden>
- Date: Sun, 6 Jan 2008 21:02:47 -0800
On Jan 6, 2008, at 12:17 PM, Ed Stockly wrote:
if (the file ":Index" exists) then
:Index
Is not a valid file reference, which may be why you're getting that
particular error.
AppleScript needs the full path:
file "hdName:dir:filename"
works, but
file ":filename"
won't work.
No, it's valid, but it's not particularly well-defined. HFS paths
with a leading colon are relative to the current working directory.
Problem is, where the current working directory is is not really
specified, it's not controllable from most applications, and it has
been known to change from release to release. Practical upshot: use
the full path.
Also, the most reliable way to determine if a file exists in
appleScript is to attempt a coersion to alias:
try
set myFile to "hdName:dir:filename" as alias
set fileExists to true
on error
set fileExists to fale
end try
For some definition of "reliable", yes. It works, but I consider this
one of the worst bits of AppleScript trickery ever -- its actual
meaning is completely obscure, it relies on some undocumented
semantics of "alias", it uses an exception to report a boolean
condition, and it's a false economy because most of the time you're
going to need to talk to some application to use the file anyway. I
encourage people to ask the question of Finder or System Events:
tell app "System Events"
exists file "whatever" --> true or false.
end
Added bonus if you use System Events: it understands POSIX paths;
useful if you're into that sort of thing.
--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