Wednesday, January 17, 2018

#1 2003-06-20 09:38:28 am

mipadi
Member
From:: Pennsylvania, USA
Registered: 2003-06-20
Posts: 7

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. smile

Offline

 

#2 2003-06-20 09:52:56 am

Rob
Member
From:: Ohio, USA
Registered: 2002-11-20
Posts: 1886

Re: Check for a file and delete it if it exists

Something like this might work.

Applescript:

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


Filed under: Finder

Offline

 

#3 2003-06-20 03:57:08 pm

andy_boretto
Member
Registered: 2003-01-02
Posts: 28

Re: Check for a file and delete it if it exists

Something like this might work.

Applescript:

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.

Applescript:


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


I am not speaking for my employer.  These views are my own


Filed under: Finder

Offline

 

#4 2003-06-21 02:15:41 am

julifos
Administrator
From:: Malasaña, Madrid
Registered: 2002-11-20
Posts: 2014

Re: Check for a file and delete it if it exists

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

Applescript:

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

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)