I can never get the exists command to work properly...

I have this code:


if exists (jpgFilePath as alias) then
-->move jpegs
do shell script "cp -n " & quoted form of POSIX path of jpgFilePath & " " & quoted form of POSIX path of jpegFolderPath
end if

where jpgFilePath = SERVERNAME:DIRECTORY:JPGS:FILENAME.jpg

and jpegFolderPath = SERVERNAME:DIRECTORY:DIRECTORY:JPGS:

What I am trying to do is move some files from the JPGS directory (in jpgFilePath) to another JPG directory in a sub directory of the main directory (jpegFolderPath).

I keep getting this error:

File Cumulus:Merion Ad Archive 2:jpgs:AA-0536022.jpg wasn’t found. (-43)

but isn’t that the whole point of the exists command - to see if a file/object exists and if so then do something, if not move on?

Any help would be appreciated.

I should add this works if the file exists, it’ll move them. Just breaks if the file doesn’t exists.

Hi,

two working versions:
¢ 1) The Finder checks if the file exists (string path is sufficient)

tell application "Finder" to if exists jpgFilePath then
	-->move jpegs
	do shell script "cp -n " & quoted form of POSIX path of jpgFilePath & " " & quoted form of POSIX path of jpegFolderPath
end if

or
¢ 2) coercion to an alias throws an error, if the file doesn’t exist

try
	jpgFilePath as alias
	-->move jpegs
	do shell script "cp -n " & quoted form of POSIX path of jpgFilePath & " " & quoted form of POSIX path of jpegFolderPath
end try

You’re not getting that far; The error is at the alias coercion (see what StefanK said).

Edit: I think [url=http://developer.apple.com/documentation/AppleScript/Conceptual/AppleScriptLangGuide/AppleScript.6a.html#14115]exists[/url] has limited use on it’s own.

This works nicely for the examples provided, where there are multiple parts. However, an alias is a complete reference by itself; You either have one or you don’t.*

  • That said, I’m not sure what to make of this:
choose file with prompt "DELETE THIS FILE:" without invisibles
set test to result

tell application "Finder"
	delete test
	empty trash
end tell

return {exists test, test}

If you’re looking at this, also try using parentheses around test or exists test.

You don’t need the Finder to use “exists”.

if exists "Charm:Users:mikey:" then
	log "lol"
end if

--> (*lol*)

(You can also tell System Events directly.)

Edit: Hurr, this doesn’t work, see the post below and my response.

A string would always exist.

if exists "ghosts" then
	return "ghostbusters!"
else
	return "back to sleep"
end if

Also:

-- Look at the event log
exists ""
exists 123
exists false

Ignoring the fact that I, uh, forgot the errata on that for a second–this is what I get for posting right when I wake up–“exists” actually does work without the Finder:

tell application "System Events"
	if exists (folder "Charm:Users:mikey:") then
		log "LOL"
	end if
end tell

--> exists == true

tell application "System Events"
	if exists (folder "Charm:Users:this_user_does_not_exist") then
		log "LOL"
	end if
end tell

--> exists == false

BTW… Moving to OS X…

:slight_smile: → (lol)

Hi, Bruce.

As it says, ‘exists’ is an application command. When you run a script in Script Editor, and it has an ‘exists’ that’s not in a ‘tell’ block, it’s Script Editor that executes the ‘exists’ command. Script Editor has ‘exists’ in its dictionary and can determine the existence of things it knows about. If you save the same script as an application and try to run it independently, you get an error saying that the item being tested doesn’t understand the ‘exists’ command.

I think what’s happening here is to do with the timing. Either Script Editor’s responding to the ‘exists’ command before the Finder’s finished emptying the trash, or it simply hasn’t had time to notice that the file’s gone. If you put in a short delay before the final line, you get the expected error.