• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Check if a file exists fails
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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
References: 
 >Re: Check if a file exists fails (From: Ed Stockly <email@hidden>)

  • Prev by Date: Re: "a reference to"
  • Next by Date: Is Leopard Finder returning wrong Unicode for åÅ?
  • Previous by thread: Re: Check if a file exists fails
  • Next by thread: Re: Check if a file exists fails
  • Index(es):
    • Date
    • Thread