What I’m trying to do is write a AppleScript that I can check folders either to remove all files from them or a certain file.
In this test script I wrote it works fine but I was unable to get the variable currentUser to replace JackDamore in the path to delete all files from the folder testQ. I tried putting it in there and i received a error. I also tried quotes around it that didn’t work either.
I will be going into multiple locations to delete files a examples would be ~/Library/… and /Library/… even /usr/lib/… as some of the locations.
Being new to AppleScripting I would appreciate a good example to work from and learn these technics.
tell application “System Events”
set currentUser to (name of current user)
end tell
set folderOne to “testQ”
tell application “Finder”
delete (files of folder (“Macintosh HD:Users:JackDamore:Downloads:” & folderOne))
end tell
say currentUser
Any of your fine help would be appreciated I am not a programmer but a new iMac user that wants to learn more.
BigJack
Model: iMac with i& and 8gigs of ram
AppleScript: 2.3.3
Browser: Safari Version 7.1 (9537.85.10.17.1)
Operating System: Mac OS X (10.8)
I don’t understand why the late one fails and would be interested to read an explanation.
This edited one works.
tell application "System Events"
set currentUser to (name of current user)
end tell
set userHome to path to home folder as text
-->> "<startup Volume>:Users:<current User>:"
set folderOne to "testQ"
tell application "System Events"
set theItems to every disk item of folder (userHome & "Downloads:" & folderOne) # EDITED
repeat with anItem in theItems
delete disk item (path of anItem)
end repeat
end tell
say currentUser
An alternate answer is :
tell application "System Events"
set currentUser to (name of current user)
end tell
set userHome to path to home folder as text
-->> "<startup Volume>:Users:<current User>:"
set folderOne to "testQ"
tell application "Finder"
delete every file of folder (userHome & "Downloads:" & folderOne) # EDITED
end tell
say currentUser
It doesn’t use a loop but the files are not really deleted, they are moved to the trash.
Yvan KOENIG (VALLAURIS, France) mercredi 24 septembre 2014 21:43:15
I already wrote that your Keynote documents are not files but package (“file package” in System Events vocabulary)
During my tests, the original code removed every flatfiles including rtf ones.
It failed to remove Keynote, Numbers, Pages packages as well as rtfd documents which are packages too.
Yvan KOENIG (VALLAURIS, France) mercredi 24 septembre 2014 22:00:38
May you delete the thread entitled : Ok a issue I have found may be you awesome experts can help me with. by BigJack which duplicate this one ?
One thing, you can get the path to the current users download folder like this:
tell application "System Events"
set downloadsFolderPath to (path to downloads folder as string)
end tell
Edited: also, with Finder references, you can use partial references like this:
tell application "System Events"
set downloadsFolder to (path to downloads folder)
end tell
tell application "Finder"
delete (every item of folder "TestQ" of downloadsFolder)
end tell
i.e. it uses a Finder reference mixed with alias reference.