I am looking to find a code that I can find out if a file exists or not. I found several solution but none had any specific location (the ones I found they always say ‘browse to the folder’ or something like that).
I do not want to use “do shell script”. I have a file in my documents called “About Stacks.pdf” I REALLY hope someone would be able to give me a solution.
tell application "Finder"
if exists file "Users:administrator:Documents:About Stacks.pdf" then
display dialog "found it"
else
display dialog "not there"
end if
end tell
Thanks in advance!
Riz
Model: Mac Pro
Browser: Internet Explorer 6.0
Operating System: Mac OS X (10.6)
what’s wrong with your code? It’s the normal way.
There is another without the Finder
set stackFile to ((path to documents folder as text) & "About Stacks.pdf")
try
stackFile as alias
display dialog "found it"
on error
display dialog "not there"
end try
Thanks for your reply. Sorry for the late follow up (I was on the road). I tried the script you sent me, but it is givng an error ("Expected end of line, etc. but found unknown token).
I was wondering, if we could troubleshoot with the initial script that I had posted. I am just not sure what’s wrong with my code.
I also tried the code below and it says Syntax error and that the file was not found. I know for sure that the file IS there.
tell application "Finder"
set myfile to "Users:administrator:Documents:About Stacks.pdf" as alias
if exists file myfile then
display dialog "found it"
else
display dialog "not there"
end if
end tell
set documentsFolder to path to documents folder
tell application "Finder"
if exists folder "Test Folder" of documentsFolder then
display dialog "found it"
else
display dialog "not there"
end if
end tell
or
set documentsFolder to path to documents folder
try
((documentsFolder as text) & "Test Folder") as alias
display dialog "found it"
on error
display dialog "not there"
end try
set testFolder to (path to documents folder as text) & "Test Folder:"
try
(testFolder & "About Stacks.pdf") as alias
display dialog "found it"
on error
display dialog "not there"
end try
Select the file/folder in Finder and run this script from your Script Editor.
It will copy the path to the clipboard so that you can paste it.
–I know the script is poorly written but i wrote it a long time ago.
set thepath to path to users folder as string
set AppleScript's text item delimiters to ":"
set thepath to first text item of thepath
tell application "Finder"
repeat with aitem in (get selection)
set r to POSIX path of (aitem as text)
set AppleScript's text item delimiters to "/"
set z to the text items of r
set AppleScript's text item delimiters to ":"
set z1 to text items of z as string
if second text item of z1 is "Volumes" then
set z2 to "\"" & text 10 thru -1 of z1 & "\""
set the clipboard to z2
else
set the clipboard to "\"" & thepath & ":" & (text 2 thru -1 of z1) & "\""
--if the file or folder is from users directory
end if
end repeat
end tell
I finally found something that works out pretty well for what I needed…
set Backup to "Backup1.bbb"
tell application "Finder"
if exists file Backup of folder "BlackBerry Backups" of folder "Documents" of home then
display dialog "found it"
else
display dialog "not there"
end if
end tell
I tried the same code on my MacBook Pro which is running 10.5.7 and that worked. I also tried the code on Mac Pro running 10.6.1. They both worked for me.
on checkFileExists(FileToCheckString)
try
set thePath to quoted form of (POSIX path of FileToCheckString)
do shell script "test -f " & thePath
return true
on error
return false
end try
end checkFileExists