Thanks to Mark Hunte, I was able to successfully implement the script below to delete desktop files for a specific user.
set exception_disk_list to list disks
tell application "Finder"
try
delete every file of entire contents of desktop
end try
try
delete (every folder of entire contents of desktop whose name is not in exception_disk_list)
end try
empty trash
end tell
Now I wish to modify the script to delete the contents of the user’s Documents, Movies, Pictures, Public, & Sites folders respectively.
set exception_disk_list to list disks
tell application “Finder”
try
delete every file of entire contents of folder “Desktop” of folder “art” of folder “Users” of startup disk
delete every file of entire contents of folder “Documents” of folder “art” of folder “Users” of startup disk
delete every file of entire contents of folder “Music” of folder “art” of folder “Users” of startup disk
delete every file of entire contents of folder “Pictures” of folder “art” of folder “Users” of startup disk
delete every file of entire contents of folder “Public” of folder “art” of folder “Users” of startup disk
delete every file of entire contents of folder “Site” of folder “art” of folder “Users” of startup disk
end try
try
delete (every folder of entire contents of desktop whose name is not in exception_disk_list)
end try
empty trash
end tell
The only items that are deleted are the ones on the desktop. How can I fix this?
I don’t want to delete the items that are in my folder equivalents of your script so you will need to use this as an example.
set t to {(path to documents folder), (path to sites folder)}
repeat with i in t
tell application "Finder"
open i
end tell
end repeat
all the main user folders your after can be found by using the path to … applescript keywords
the rest of it you’ve got sussed so you just need to piece it together now!!
Hi Barry,
I am assuming you want to delete all items in the art folders found in the ‘user_art_folder’ list.
And still delete everything on the desktop. Apart from the ‘exception_disk_list’
try this
set exception_disk_list to list disks
set user_art to "art"
tell application "System Events"
set user_art_folder to {documents folder, pictures folder, public folder, sites folder, music folder of user domain}
repeat with i from 1 to number of items in user_art_folder
set this_folder to path of (item i of user_art_folder)
try
tell application "Finder"
set t to delete entire contents of alias (this_folder & user_art as string)
end tell
end try
end repeat
tell application "Finder"
try
delete every file of entire contents of desktop
end try
try
delete (every folder of entire contents of desktop whose name is not in exception_disk_list)
end try
end tell
empty trash
end tell
I Pidge, you can set up a NON ADMIN temporary user to test a script like this.
Important to make sure it is a none admin, and also have the exception_disk_list, otherwise your script will try to delete the disks on the desktop, which includes the Hard Disk.
I had managed in the meantime to write a script (below) that worked when running as a .scpt. It is hardly as elegant as your coding. I tried running the one you offered but it only deletes items from the desktop. Admittedly, too, I am a little unfamiliar w/ the syntax you applied. Is there a way to streamline the script I wrote?
— Mount the volume
tell application “Finder”
open location “afp://art:password@195.226.25.58/Art/Public”
end tell
— Delete Desktop items
set exception_disk_list to list disks
tell application “Finder”
try
delete every file of entire contents of desktop
end try
try
delete (every folder of entire contents of desktop whose name is not in exception_disk_list)
end try
empty trash
end tell
— Delete all items from art’s Documents, Movies, Music, Pictures, Public & Sites
tell application “Finder”
activate
select window of desktop
select window of desktop
make new Finder window to startup disk
select Finder window 1
select Finder window 1
set target of Finder window 1 to folder “Documents” of folder “art” of folder “Users” of startup disk
delete every item of Finder window 1
select window of desktop
select window of desktop
make new Finder window to startup disk
select Finder window 1
select Finder window 1
set target of Finder window 1 to folder "Movies" of folder "art" of folder "Users" of startup disk
delete every item of Finder window 1
select window of desktop
select window of desktop
make new Finder window to startup disk
select Finder window 1
select Finder window 1
set target of Finder window 1 to folder "Music" of folder "art" of folder "Users" of startup disk
delete every item of Finder window 1
select window of desktop
select window of desktop
make new Finder window to startup disk
select Finder window 1
select Finder window 1
set target of Finder window 1 to folder "Pictures" of folder "art" of folder "Users" of startup disk
delete every item of Finder window 1
select window of desktop
select window of desktop
make new Finder window to startup disk
select Finder window 1
select Finder window 1
set target of Finder window 1 to folder "Public" of folder "art" of folder "Users" of startup disk
delete every item of Finder window 1
select window of desktop
select window of desktop
make new Finder window to startup disk
select Finder window 1
select Finder window 1
set target of Finder window 1 to folder "Sites" of folder "art" of folder "Users" of startup disk
delete every item of Finder window 1
select window of desktop
select window of desktop
close every window
empty trash
end tell
— Tidying up
tell application “Finder”
activate
select window of desktop
select window of desktop
make new Finder window to disk “Art”
select Finder window 1
select Finder window 1
set target of Finder window 1 to folder “Public” of disk “Art”
select Finder window 1
set current view of Finder window 1 to list view
end tell
tell application “Finder”
activate
select Finder window 1
select Finder window 1
set bounds of Finder window 1 to {880, 75, 1275, 626}
select Finder window 1
select Finder window 1
set position of Finder window 1 to {1170, 51}
end tell
Here is a weird thing: After saving the .scpt as a run-only app & configuring it to run when ‘art’ logs in, sometimes I get the error "The operation cannot be completed because you do not have sufficient privileges for “Art.” “Art” is the server. And the script apparently trying to trash an item on the server. Should I move the afp mount to the end of the script?
First I think you need to be clear on what you want to do, as I misunderstood the first time?
Art is the current user.
There is also a server called Art.
You want to delete every file and folder on user Art’s desktop.
You then want to delete every file and folder from user Art’s Documents, Movies, Music, Pictures, Public & Sites folders.
And lastly you want to mount Art the server.
You are correct. “art” is a user. “Art” is the name of the server.
Only thing I would add is that since another dept manages the server, the “—Tidying up” part that I wrote is to make the Art/Public folder show up in list view.
set exception_disk_list to list disks
tell application "System Events"
set user_art_folder to {documents folder, pictures folder, public folder, sites folder, music folder} of user domain
repeat with i from 1 to number of items in user_art_folder
set this_folder to path of (item i of user_art_folder)
try
tell application "Finder"
set t to delete entire contents of alias this_folder
end tell
end try
end repeat
tell application "Finder"
try
delete every file of entire contents of desktop
end try
try
delete (every folder of entire contents of desktop whose name is not in exception_disk_list)
end try
empty trash
end tell
end tell
tell application "Finder"
activate
open location "afp://art:password@195.226.25.58/Art/Public"
delay 5
make new Finder window to folder "Public" of disk "Art"
set current view of Finder window "Public" to list view
set bounds of Finder window 1 to {880, 75, 1275, 626}
set position of Finder window 1 to {1170, 51}
end tell
Thanks, Mark! I am learning fast. Just one last thing I can’t quite figure out. I wanted to close all windows so that only the Art/Public window is open, so I inserted a ‘close every window’ before the last part of the script ran. But, when the script finishes, there are two windows open: the one that is in list view, & one that simply opens the same window, in the middle of the screen, in icon view. Darn, so close. What am I missing?
tell application “Finder”
activate
close every window
end tell
set exception_disk_list to list disks
tell application “System Events”
set user_art_folder to {documents folder, movies folder, pictures folder, public folder, sites folder, music folder} of user domain
repeat with i from 1 to number of items in user_art_folder
set this_folder to path of (item i of user_art_folder)
try
tell application “Finder”
set t to delete entire contents of alias this_folder
end tell
end try
end repeat
tell application “Finder”
try
delete every file of entire contents of desktop
end try
try
delete (every folder of entire contents of desktop whose name is not in exception_disk_list)
end try
empty trash
end tell
end tell
tell application “Finder”
activate
close every window
end tell
tell application “Finder”
activate
open location “afp://art:password@195.226.25.58/Art/Public”
delay 3
make new Finder window to folder “Public” of disk “Art”
set current view of Finder window “Public” to list view
set bounds of Finder window 1 to {880, 75, 1275, 626}
set position of Finder window 1 to {1170, 51}
end tell
-------------------------
tell application "Finder"
activate
close every window
end tell
-------------------------
set exception_disk_list to list disks
tell application "System Events" to set user_art_folder to {documents folder, movies folder, pictures folder, public folder, sites folder, music folder} of user domain
repeat with n in user_art_folder
set this_folder to path of n
try
tell application "Finder" to set t to delete entire contents of alias this_folder
end try
end repeat
tell application "Finder"
try
delete every file of entire contents of desktop
end try
try
delete (every folder of entire contents of desktop whose name is not in exception_disk_list)
end try
empty trash
delay 3
close every window
open location "afp://art:password@195.226.25.58/Art/Public"
delay 3
make new Finder window to folder "Public" of disk "Art"
set current view of Finder window "Public" to list view
set bounds of Finder window 1 to {880, 75, 1275, 626}
set position of Finder window 1 to {1170, 51}
end tell
Yvan KOENIG (from FRANCE samedi 20 octobre 2007 13:01:48)
But I did not see the need to include the whole script again.
But your deletion of the extra finder tell block wrapper, is ok, but you will need to change the line. close every window to close every finder window or the finder will not know whose window you want it to close.
Not sure why you have two delays, And I would also get rid of the first :
tell application “Finder”
activate
close every window
end tell
As it is redundant.
for easier reading
set exception_disk_list to list disks
tell application "System Events"
set user_art_folder to {documents folder, pictures folder, public folder, sites folder, music folder} of user domain
repeat with i from 1 to number of items in user_art_folder
set this_folder to path of (item i of user_art_folder)
try
tell application "Finder"
set t to delete entire contents of alias this_folder
end tell
end try
end repeat
tell application "Finder"
try
delete every file of entire contents of desktop
end try
try
delete (every folder of entire contents of desktop whose name is not in exception_disk_list)
end try
empty trash
end tell
end tell
tell application "Finder"
activate
open location "afp://art:password@195.226.25.58/Art/Public"
delay 3
close every Finder window
make new Finder window to folder "Public" of disk "Art"
set current view of Finder window "Public" to list view
set bounds of Finder window 1 to {880, 75, 1275, 626}
set position of Finder window 1 to {1170, 51}
end tell
This works fairly well, except that if an external HD is plugged in prior to login, I get the error message that the device “external HD…” is in use… & the user has to click OK for the script to continue. Is there a workaround? I tried inserting a delay before the empty trash command, but it makes no difference.
set exception_disk_list to list disks
----->
set ecxLS to paragraphs of (do shell script "ls /volumes/")
----->
repeat with i from 1 to number of items in ecxLS
set this_item to item i of ecxLS
if this_item is not in exception_disk_list then
copy this_item to end of exception_disk_list
end if
end repeat
tell application "System Events"
set user_art_folder to {documents folder, pictures folder, public folder, sites folder, music folder} of user domain
repeat with i from 1 to number of items in user_art_folder
set this_folder to path of (item i of user_art_folder)
try
tell application "Finder"
set t to delete entire contents of alias this_folder
end tell
end try
end repeat
tell application "Finder"
try
delete every file of entire contents of desktop
end try
try
delete (every folder of entire contents of desktop whose name is not in exception_disk_list)
end try
empty trash
end tell
end tell
tell application "Finder"
activate
open location "afp://art:password@195.226.25.58/Art/Public"
delay 3
close every Finder window
make new Finder window to folder "Public" of disk "Art"
set current view of Finder window "Public" to list view
set bounds of Finder window 1 to {880, 75, 1275, 626}
set position of Finder window 1 to {1170, 51}
end tell
Or replace the part of the script between the lines
------->
-------->
with
tell application "System Events"
set ecxLS to name of disks whose ejectable is true
end tell
One last thing. I am creating this script on a 23" display & setting the position of the window
set position of Finder window 1 to {1170, 51}
to appear just to the left of the desktop drive icons. Are these absolute coordinates? What if the script runs on a 17" display? Should I be concerned? How should I mod the script to do this?
I don’t understand your statement in your post dated 2007-10-20 10:08:37 am.
The original code was:
tell application "Finder"
activate
-- here the target app is "Finder"
close every window
end tell -- to Finder
tell application "Finder"
activate
open location "afp://art:password@195.226.25.58/Art/Public"
delay 3
make new Finder window to folder "Public" of disk "Art"
set current view of Finder window "Public" to list view
set bounds of Finder window 1 to {880, 75, 1275, 626}
set position of Finder window 1 to {1170, 51}
end tell
My edited one was:
tell application "Finder"
try
delete every file of entire contents of desktop
end try
try
delete (every folder of entire contents of desktop whose name is not in exception_disk_list)
end try
empty trash
delay 3
-- here the target app is "Finder" too
close every window
open location "afp://art:password@195.226.25.58/Art/Public"
delay 3
make new Finder window to folder "Public" of disk "Art"
set current view of Finder window "Public" to list view
set bounds of Finder window 1 to {880, 75, 1275, 626}
set position of Finder window 1 to {1170, 51}
end tell
I see no difference upon the “close every window”.
As far as I know, in both versions the script is “speaking” to the Finder.
The only change is that I removed a pair
end tell – to Finder
tell application “Finder”
so, the target appliction is always the Finder.
Yvan KOENIG (from FRANCE mercredi 24 octobre 2007 18:54:35)
Yes I would expect that to work. I am sure it has for me in the past.
But for some reason finder (mine at least ) did not like the statement, close every window
in this script. ?