Hi all. First-time poster who is brand new to AppleScript. Been using Macs since 1994 but never wrote an AppleScript until this week, when I wrote the below to automate the download of a file and deletion of files 30 days or more older. It works, so that’s good. The one thing that bugged me though was I couldn’t find a way to delete all files that are in my list in one command. I needed to iterate through them using repeat. Is that the only way to do this?
Also, if there is anything that can be done more efficiently I’d appreciate that feedback too.
Thanks.
on run
tell application "Safari"
tell window 1
set current tab to (make new tab with properties {URL:"http://HUBIP/hub/backupDB?fileName=latest"})
delay 5
close current tab
end tell
end tell
tell application "Finder"
set sourceFolder to alias "Macintosh SSD:Users:dworkman:Downloads:"
set targetFolder to alias "Macintosh SSD:Users:dworkman:Documents:Hubitat:"
set latestFile to last item of (sort (files in (sourceFolder)) by modification date)
move latestFile to targetFolder
set movedFile to name of last item of (sort (files in (targetFolder)) by modification date)
end tell
set modDate to (30)
tell application "System Events"
set fileList to (every item of targetFolder whose modification date is less than ((current date)) - modDate * days)
set listOfNames to {}
repeat with i in fileList
set currentFileName to (the name of i)
copy currentFileName to end of listOfNames
end repeat
set myString to items of listOfNames
set my text item delimiters to ", "
text items of listOfNames
set listSize to count of fileList
end tell
tell application "Finder"
if listSize is 0 then
display notification "Hubitat backup complete. File downloaded: " & movedFile & ". No files deleted." with title "Hubitat Backup" sound name "Blow"
else
repeat with i in fileList
delete i
end repeat
display notification "Hubitat backup complete. File downloaded: " & movedFile & ". File(s) deleted: " & myString & "." with title "Hubitat Backup" sound name "Blow"
delay 1
end if
end tell
end run
set targetFolder to ((path to desktop as text) & "1 dossier copie:") as alias
set modDate to 30
set limite to ((current date) - modDate * days)
tell application "Finder"
delete (items of targetFolder whose modification date is less than limite)
end tell
It seems that this scheme can’t be used to delete with System Events.
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 28 décembre 2020 17:22:05
Hi, dorian 35.
The critical moment of your script (time-consuming fragment) is not deleting the filtered files, but getting a list of files and folders to delete. This is because your entire list of items in Downloads will contain about 30 times more items than a filtered list.
Therefore, it makes sense to get the filtered list as quickly as possible and use a repeat loop to remove it. This makes even more sense, because older files are better judged by Date Added, rather than Date Modified. And for deleting by Date Added, there is no way to do without a repeat loop.
Filtering by Date Added and deleting must be done with AsObjC. Somewhere on this site there are AsObjC examples with filtering and deletion by Date Added. Use search with keyword “Date added”.
Hi, thanks for the reply. Aren’t I already getting the filtered list with this command?
tell application "System Events"
set fileList to (every item of targetFolder whose modification date is less than ((current date)) - modDate * days)
I apologize but, as modification date and date added are independent parameters, only the asker is able to decide which one is relevant.
In some folders I have scripts added on 2020/10/09 and modified on 2015/16/07.
Some other scripts were modified on 2020/10/09 and added on 2015/16/07.
In such case, from my point of view, the scripts modified on 2015/06/07 are older than those modified on 2020/10/09.
Other point. If I remember well, ASObjC is unable to move/delete filtered files without using a repeat loop.
Only the Finder, which I hate, is able to do that.
I know that the shell command rm is able to apply to a string concatenating several quoted form of POSIX paths but creating such string requires a repeat loop so I assume that it doesn’t match the original asker’s requirement.
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 30 décembre 2020 16:10:34