set loc_old to "H:OldFolder:"
set filename_old to "OldFile.txt"
try
set a to loc_old
log a
set b to filename_old
log b
set c to a & b
-- Applescript file path
log c
set d to POSIX path of file c
--POSIX file path
log d
if c exists then
try
do shell script "rm " & d
end try
end if
on error error_message number error_number
log error_message & " " & error_number
end try
Terminal result, before running script:
If this works, and it does, it deletes the OldFile. Log is below.
Terminal result after running script:
Unfortunately, if I run it again, after the file has already been deleted, it still runs as if the file was still there! Unfortunately, the shell script return an error (of course).
Why does “if c exists” still evaluate as true?
I know I can force the shell script with -f, or just leave the error out of the try trap. I’m rather more interested in the evaluation, since I use this as a subroutine in many places.
Thanks in advanced.
Paul
What you get is perfectly normal. You are just using a foolish instruction.
exists c
is in fact :
exists “H:OldFolder:OldFile.txt”
which doesn’t check the existence of a file, it checks the existence of a string and so it’s ALWAYS returning correctly the value : true
Next time, double check your code before complaining.
Yvan KOENIG (VALLAURIS, France) mercredi 7 mai 2014 19:25:22
The exists command totally depends on the target application. Many applications override the exists class so exists doesn’t always behave the same in every context. In the context of current application (application “script editor” when run in the editor) the exists command will check if the given value equals missing value object. If the given value is missing value it will return false, otherwise it will return true. When you have targeted the Finder or System Events it will lookup if the given path does exists. Because the Finder is so tremendously slow I prefer to use an do shell script on this one with inside an handler.
pathExists(path to desktop folder)
on pathExists(HFSPath)
return (do shell script "[[ -e " & quoted form of POSIX path of (HFSPath as string) & " ]] && echo yes || echo false") as boolean
end pathExists