Check for a file and delete it if it exists

Hi,

I work in tech support/training at a local university, and we’re currently converting the entire campus to OS X. Unfortunately, we have encountered a problem in importing IE bookmarks into Safari. I figured out how to do it fine – manually. But I’d like to write a little AppleScript to do it automatically (for the professors who are not OS X-savvy – the entire thing will be automated.)

Basically, I want to check and see if a file exists, and if it does, delete it. The file is located at ~/Library/Preferences/com.Apple.Safari.plist. What would be the proper way to check for the existence of this file, and it if exists, delete it?

By the way, I am writing this application with an interface using AppleScript Studio. I’ve used AppleScript Studio before and written some small scripts (so I’m not a total newbie at this!) but I’m not completely “proficient” either.

Thanks for all your help! My boss will be very happy if I can get this script written, and a happy boss == good things for me. :slight_smile:

Something like this might work.

set plist_ to ((path to "pref" as text) & "com.Apple.Safari.plist")
tell application "Finder"
	if exists file plist_ then
		display dialog "It exists."
		--delete file plist_ -- moves it to the trash
	end if
end tell

wrap that in a try block to catch any errors.


try

tell application "Finder"
	if exists file plist_ then
		display dialog "It exists."
		--delete file plist_ -- moves it to the trash
	end if
end tell


on error
display dialog "there was an error"
end try


Or, as long as you have yet the posix path:

try
     do shell script "rm ~/Library/Preferences/com.Apple.Safari.plist"
end try