Re: How to detect if a file exists
Re: How to detect if a file exists
- Subject: Re: How to detect if a file exists
- From: "Mark J. Reed" <email@hidden>
- Date: Fri, 17 Jul 2009 20:03:38 -0400
On Fri, Jul 17, 2009 at 7:29 PM, Stockly, Ed<email@hidden> wrote:
> In your script AppleScript does not know that the string
> "/Users/Andrew/Desktop/Stencil.doc" is a reference to a file. You’re
> essentially asking if the string itself exists and, since it’s right there
> in the script, it does exist so the condition is true and the dialog
> displays.
Pretty much.
> What you want to do is enclose the command inside a finder tell, and
> indicate it’s a file reference, but AppleScript uses colons to delimit
> directories, so you’ll either want to coerce your path to an alias or use
> this format for your file reference.
You don't need an alias; just use "POSIX file" inside the Finder tell:
tell application "Finder"
if POSIX file "/Users/Andrew/Desktop/Stencil.doc" exists then
(display dialog "Hello world")
end tell
You *can* coerce to an alias if you prefer; that will fail if the file
doesn't actually exist, so you don't have to talk to the Finder or
System Events to find that out:
try
POSIX file "/Users/Andrew/Desktop/Stencil.doc" as alias
display dialog "Hello, world!" -- won't get here if the file doesn't exist.
end try
--
Mark J. Reed <email@hidden>
_______________________________________________
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