Wednesday, July 24, 2019

#1 2014-09-04 07:38:37 am

gostavs
Member
Registered: 2014-09-04
Posts: 1

Delete folders if exist and delete

Hi!
First off, Im almost completely new to this.
I want to make a script that deletes folders and files related to Java.
Ive googled and searched the forums. Ive come up with and tried different variations of these two scripts, and i dont understand why either dont work-

Applescript:


tell application "Finder"
   if exists folder "/System/Library/Java/JavaVirtualMachines/" then delete ({POSIX file "/System/Library/Java/JavaVirtualMachines"} as alias)
   if exists folder "/Library/Java/JavaVirtualMachines/" then delete ({POSIX file "/Library/Java/JavaVirtualMachines"} as alias)
   if exists folder "/Library/Application Support/Oracle" then delete ({POSIX file "/Library/Application Support/Oracle"} as alias)
   if exists file "/Library/Preferences/com.oracle*" then delete ({POSIX file "/Library/Preferences/com.oracle*"} as alias)
   if exists file "/var/db/receipts/com.oracle*" then delete ({POSIX file "/var/db/receipts/com.oracle*"} as alias)
   
end tell

Applescript:


tell application "Finder"
   if exists "/System/Library/Java/JavaVirtualMachines/" then delete "/System/Library/Java/JavaVirtualMachines"
   if exists "/Library/Java/JavaVirtualMachines/" then delete "/Library/Java/JavaVirtualMachines"
   if exists "/Library/Application Support/Oracle" then delete "/Library/Application Support/Oracle"
   if exists "/Library/Preferences/com.oracle*" then delete "/Library/Preferences/com.oracle*"
   if exists "/var/db/receipts/com.oracle*" then delete "/var/db/receipts/com.oracle*"
end tell

Both says that every folder dont exist and therefore it doesnt delete them. I almost got it working one time, but then I had to enter the admin password for every folder/file that was to be deleted.

I want to be able to run the script on my clients and enter the admin password once, and then be done with it. Any help is greatly appreciated!
Best regards,
Gustav

Last edited by gostavs (2014-09-04 07:40:25 am)


Filed under: applescript, delete, folders

Offline

 

#2 2014-09-04 08:56:16 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11564
Website

Re: Delete folders if exist and delete

Hi,

the main issue is the Finder works only with HFS paths (colon separated).
The HFS path equivalent is

Applescript:


set systemLibaryFolder to path to library folder from system domain
set localLibaryFolder to path to library folder -- local domain is the default value
set localApplicationSupportFolder to path to application support folder -- local domain is the default value
set localPreferencesFolder to path to preferences folder from local domain
tell application "Finder"
   if exists folder "Java:JavaVirtualMachines" of systemLibaryFolder then delete folder "Java:JavaVirtualMachines" of systemLibaryFolder
   if exists folder "Java:JavaVirtualMachines" of localLibaryFolder then delete folder "Java:JavaVirtualMachines" of localLibaryFolder
   if exists folder "oracle" of localApplicationSupportFolder then delete folder "oracle" of localApplicationSupportFolder
   delete (every item of localPreferencesFolder whose name starts with "com.oracle") -- exists is not necessary using a where clause
   delete (every item of folder "var:db:receipts" of startup disk whose name starts with "com.oracle")
end tell


regards

Stefan

Offline

 

#3 2014-09-04 11:50:49 am

kel1
Member
Registered: 2013-01-11
Posts: 2188

Re: Delete folders if exist and delete

Hi,

Also, if you try to delete System files you need to enter an admin password. You might try unix' rm with sudo. Then, I think you only need to enter it once and it lasts for a short time.

gl,
kel


Os 10.10.3
Mbp

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)