Re: Syntax for Finder 'exists' command?
Re: Syntax for Finder 'exists' command?
- Subject: Re: Syntax for Finder 'exists' command?
- From: kai <email@hidden>
- Date: Tue, 29 Mar 2005 23:39:25 +0100
On Tue, 29 Mar 2005 06:56:29 -0800, Dennis Jones wrote:
I am trying to determine if a file exists after a Finder 'move'
command prior to using the file.
In the code below, line 1 and 2 work as expected to get the info for
the file. Line 3 works as expected and selects the file in the finder.
However, line 4 always returns 'false', when I would expect it to
return true since I know the file exists.
What is wrong with my syntax?
-- code
set importFolder to "fooFolder"
set importFile to "fooFile"
tell application "Finder"
set this_item to (importFolder & importFile) as alias -- line 1
set this_info to info for this_item -- line 2
select (importFolder & importFile) -- line 3
exists (importFolder & importFile) as alias -- line 4
end tell
It seems there may be some confusion between object names, paths,
Finder objects and aliases here, Dennis. In addition, as already noted,
it seems very unlikely that even the earlier statements above could
actually work - at least, not based solely on the strings "fooFolder"
and "fooFile". To make complete sense, these would normally require
extra path information, either from more detailed text specifications
or through conversion to Finder references.
Assuming that the folder "fooFolder" is on the desktop, and that it
contains the file "fooFile", there are numerous syntactical forms for
checking their existence via the Finder. These include the following
variations:
----------------
set importFolder to "fooFolder"
set importFile to "fooFile"
tell application "Finder"
exists file importFile of folder importFolder
(folder importFolder as Unicode text) & importFile exists
exists file ((folder importFolder as Unicode text) & importFile)
exists file (importFolder & ":" & importFile)
item importFile of item importFolder exists
exists (item importFolder as Unicode text) & importFile
item ((item importFolder as Unicode text) & importFile) exists
exists item (importFolder & ":" & importFile)
end tell
----------------
Note that no path information is supplied in any of the above examples
- only object names and Finder object classes (the specific 'file' &
'folder' - and the more general 'item'). The reason they work is that,
where no path information is provided, the Finder assumes that the
objects can be found on the desktop.
If the objects resided elsewhere, or if they were referred to outside a
Finder tell block, additional path information would have to be
supplied.
Note also that the examples feature no coercion to alias. In fact, any
attempt to coerce to alias (which would either succeed or fail) should
constitute all the evidence needed of the existence (or not) of an
object. Clearly, this would render any 'exists' test redundant. For
example, the following snippet (which contains no explicit 'exists'
test) should return either true or false, depending on the existence of
the objects in question:
----------------
set importFolder to "fooFolder"
set importFile to "fooFile"
tell application "Finder" to try
file importFile of folder importFolder as alias
true
on error
false
end try
----------------
Finally, the 'coercion to alias' test could be used without a Finder
tell block at all. As already mentioned, this type of approach would
require specific path information:
----------------
set importFolder to "fooFolder"
set importFile to "fooFile"
try
(path to desktop as Unicode text) & importFolder & ":" & importFile as
alias
true
on error
false
end try
----------------
You'll see that this method concatenates the path to the desktop (as
Unicode text) with the names of the importFolder and importFile
(separated by a colon). The concatenation operation forms the full file
path to "fooFile". This is then coerced, if possible, to an alias.
I hope this helps to clarify things a little...
---
kai
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden