Unable to ask applescript 'if file exists' 'else'

Hello everybody!


I've been googling for hours on end for this. Can't find anything.

I thought it would have been simple enough: get applescript to do a check to see if a file exists, and do something. If it does not exist, do something else.

My script is (more or less) as follows:

set file_yes to (("~/") & "FILE.txt")

tell application "Finder"

if exists file file_yes then

do shell script " SHELL SCRIPT " with administrator privileges

tell application "Finder" to display alert "Shell Script complete!"

try

do shell script "rm ~/FILE.txt" with administrator privileges

end try

else

tell application "Finder" to display alert "No file!"

end if

end tell

But the program constantly skips to 'No file!'. All I get is that alert. I cannot get the 'shell script complete' message, nor delete the file. What am I doing wrong?


FYI: I am not an applescript expert (as you can tell...). I know a bit of it and make many programs for myself, but I can't get this one going. I know the basics. Not everything.


Thanks in advance!

MacBook Pro, Mac OS X (10.7.5)

Posted on Dec 21, 2015 1:51 PM

Reply
15 replies

Dec 21, 2015 2:21 PM in response to Niel

Niel wrote:


You need to use a Mac OS X-style path when addressing the Finder:


if exists file "FILE.txt" of home then


(137423)

Thanks, Niel.

Unfortunately, I am now receiving an error: the variable 'home' is not defined.

I did a 'set home to ("~/")' to try fix it, and now I'm getting 'Can't make FILE.txt into type integer'.

What is wrong now?

Dec 21, 2015 2:44 PM in response to Sharkey101

Since you're using the Finder, don't bother with either 'POSIX path' or trying to set home at all. Run the following:


tell application "Finder"

if exists file "FILE.txt" of home then

do shell script " SHELL SCRIPT " with administrator privileges

tell application "Finder" to display alert "Shell Script complete!"

try

do shell script "rm ~/FILE.txt" with administrator privileges

end try

else

tell application "Finder" to display alert "No file!"

end if

end tell


(137429)

Dec 21, 2015 2:43 PM in response to stevejobsfan0123

However, I have been playing around with it, and this script worked for me:


set home to POSIX path of (path to home folder) as text

set filePath to home & "File.txt"

if exists filePath then


display dialog "Found!"

else

display dialog "Not found!"

end if

Hmm... tried that. Now I'm getting the opposite of what I was before. Now instead of what I said earlier,

"the program constantly skips to 'No file!'. All I get is that alert. I cannot get the 'shell script complete' message, nor delete the file.",

I am now getting constant 'shell script complete'.

Gaah! Frustrating!

Dec 21, 2015 2:48 PM in response to Niel

tell application "Finder"

if exists file "FILE.txt" of home then

do shell script " SHELL SCRIPT " with administrator privileges

tell application "Finder" to display alert "Shell Script complete!"

try

do shell script "rm ~/FILE.txt" with administrator privileges

end try

else

tell application "Finder" to display alert "No file!"

end if

end tell

Now applescript wants an 'else' at the end (after the final 'end tell').

😟


EDIT: Fixed that. Now I've got the same error as I started with. A constant 'No File!'

Dec 21, 2015 3:09 PM in response to Niel

If simply adding the 'else' doesn't work, post your entire code.

I didn't simply add 'else', but here you are, anyway:

set question to display dialog "YES or NO?" buttons {"YES", "NO", "Cancel"}

set answer to button returned of question


if answer is equal to "YES" then

#Check if FILE.txt exists. If yes, Error! Already YES! If no, do as follows:

do shell script "SHELL SCRIPT" with administrator privileges

set ipaddress to IPv4 address of (get system info)

tell application "Finder" to display alert "ALERT" & ipaddress

do shell script "echo 'FILE' >~/FILE.txt" with administrator privileges

end if


if answer is equal to "NO" then

#tell application "Finder"

# set home to POSIX path of ("~/")

#end tell

#if exists file "FILE.txt" of home then


#set home to POSIX path of (path to home folder) as text

#set filePath to home & "FILE.txt"

#if exists filePath then


tell application "Finder"

if exists file "FILE.txt" of home then

do shell script "SHELL SCRIPT" with administrator privileges

tell application "Finder" to display alert "SHELL script complete"

try

do shell script "rm ~/FILE.txt"

end try

else

tell application "Finder" to display alert "Error! No file!"

end if

end tell

end if

Dec 21, 2015 8:42 PM in response to VikingOSX

settheFileto ((path tohome folder) astext) & "FILE.txt"

set status to false as boolean


tell application "Finder" to if exists theFile then set status to true

if status is true then

display alert "shell script complete"

else

display alert "No file."

end if

That is perfect. Thanks, VikingOSX, Niel and stevejobsfan0123. You've all been a big help! Thanks for replying fast, too 🙂

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Unable to ask applescript 'if file exists' 'else'

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.