Skip to content

Instantly share code, notes, and snippets.

@kevin-mitchell
Last active September 14, 2024 03:20
Show Gist options
  • Select an option

  • Save kevin-mitchell/641cfa267c5ac957214d42cbf1d460ac to your computer and use it in GitHub Desktop.

Select an option

Save kevin-mitchell/641cfa267c5ac957214d42cbf1d460ac to your computer and use it in GitHub Desktop.
MacOS: Clear out desktop of files over 5 days old
set targetFolder to "/Users/<your username>/Desktop/garbage"
-- This is the folder on your desktop you want to ignore
-- ...in my case I have a folder "garbage" on my desktop I put everything
set ignoreFolderName to "garbage"
-- Get current date
set currentDate to (current date)
-- Set the desktop folder path
set desktopFolder to (path to desktop folder as text)
tell application "Finder"
-- Get a list of all items (files and folders) on the desktop
set itemList to every item of folder desktopFolder
repeat with currentItem in itemList
-- Get the name of the current item
set itemName to name of currentItem
-- Ignore the "garbage" folder
if itemName is not equal to ignoreFolderName then
-- Get the modification date of the item
set modDate to modification date of currentItem
-- Calculate the difference in days between the current date and modification date
set daysDifference to (currentDate - modDate) / days
-- If the item is older than 5 days, move it to the target folder
if daysDifference > 5 then
set targetFolderPath to POSIX file targetFolder as alias
-- ***WARNING*** this will replace files when moving!
move currentItem to targetFolderPath with replacing
end if
end if
end repeat
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment