File exists before deleting

Hi all,

I’ve got a script here that allows a user to try a couple of things to fix an application problem. Their first option is to delete some prefs files or they have the option of trashing the contents of an application directory (the apps download at next login). Works pretty well (although the code is messy).

My problem is if the user tries to trash the prefs file and they don’t exists, the script throws an error. I can’t figger out how to check if the files exist, to avoid the applescript error. I’m posting the whole thing (don’t be harsh - I’m not a scripter lol). Any help would be appreciated. Thanks.


----------------------------------------------------
--
-- set some variables
--
----------------------------------------------------

set x to path to trash
set myApps to path to applications folder as string
set myApps to myApps & "DTApps:" as string
set keepfiles to {"LoginApp.app", "DTLogin:"}
set keepfiles2 to {}

----------------------------------------------------
--
-- Let the user know they just pressed an important button and give them a way out.
--
----------------------------------------------------

display dialog "You've just started the DTI Fixer script!

If you didn't mean to do this, please press Cancel at once!

If really want to try and fix DTI, press OK." with icon 2 default button 1


tell application "System Events"
	if application process "InCopy" exists then
		tell application "InCopy"
			quit
		end tell
	end if
end tell

----------------------------------------------------
--
-- user dialog
--
----------------------------------------------------

tell application "Finder"
	activate
	set tempVar to display dialog "Please select an option below to try and fix DTI.  Try the Trash DTApps option first." buttons {"Trash Saved Data", "Trash DTApps folder", "Cancel"} default button 1 with icon 2
	
	set theButtonPressed to button returned of tempVar
	if theButtonPressed is "Trash Saved Data" then
		
		----------------------------------------------------
		--
		-- we are throwing out the saved data file in indesign
		--
		----------------------------------------------------
		
		set myHome to path to home folder as string
		set myfolder to (myHome & "Library:Preferences:Adobe InDesign:Version 2.0") as string
		set myFolder1 to (myHome & "Library:Preferences:Adobe InDesign:Version 2.0:InDesign Recovery") as string
		set myfolder2 to (myHome & "Library:Preferences:Adobe InCopy:Version 2.0") as string
		set myFolder3 to (myHome & "Library:Preferences:Adobe InCopy:Version 2.0:InCopy Recovery") as string
		set myFolder4 to (myHome & "Library:Preferences") as string

		
		tell application "Finder"
			delete (files of folder myfolder whose name contains "InDesign SavedData")
			delete myFolder1
			delete (files of folder myfolder2 whose name contains "InCopy SavedData")
			delete myFolder3
			delete (files of folder myFolder4 whose name contains "com.dtint.newsspeed.plist")
		end tell
		

		
		display dialog "Please log into DTI again.  If deleting the Saved Data files did not work, please run the script again and click Trash the DT Apps button." with icon 2
		
	end if
	----------------------------------------------------
	--
	-- we just trashed the saved data file.  does it work now?
	-- script ends
	--
	----------------------------------------------------
	
end tell

if theButtonPressed is "Trash DTApps folder" then
	
	----------------------------------------------------
	--
	-- trash DTApps files for starters
	--
	----------------------------------------------------
	
	
	tell application "Finder"
		repeat with i in keepfiles
			set temp to myApps & i as string
			set keepfiles2 to keepfiles2 & temp
		end repeat
		set mylist to every file of folder myApps
		repeat with i in mylist
			set i to i as string
			if i is in keepfiles2 then
				set nothing to ""
			else if i is not in keepfiles2 then
				set mybadfile to i as string
				move file mybadfile to x
			end if
		end repeat
		
		----------------------------------------------------
		--
		-- trash DTApps folders
		--
		----------------------------------------------------
		
		set mylist to every folder of folder myApps
		repeat with i in mylist
			set i to i as string
			if i is in keepfiles2 then
				set nothing to ""
			else if i is not in keepfiles2 then
				set mybadfolder to i as string
				move folder mybadfolder to x
			end if
		end repeat
		display dialog "Click OK to start the LoginApp.
		
If trashing the DTApps directory did not work, you need help from a higher power." buttons {"Start LoginApp", "Cancel"} with icon 1 default button 1
	end tell
end if

tell application "Finder"
	activate application "LoginApp"
end tell

Wouldn’t the easiest way around the problem be a “try” loop?


		try
			--code for deleting a file
		on error
			--file doesn't exist, do something else
		end try

You can leave-out the “on error” if you don’t really care if the try fails (similar to leaving the “else” out of an “if” statement).

–Kevin

That works as advertised, so thanks Kevin. And I’m using the on error to let the user know that the file’s weren’t found.

Many thanks!