Skip to content

Instantly share code, notes, and snippets.

@septicwolf818
Created February 5, 2024 22:03
Show Gist options
  • Save septicwolf818/357e62ef3d3680123542d917a2ef9186 to your computer and use it in GitHub Desktop.
Save septicwolf818/357e62ef3d3680123542d917a2ef9186 to your computer and use it in GitHub Desktop.
Organize files on Desktop (Apple Script)
-- Sort files on the desktop into folders
-- Function to sort files
on sortFiles()
tell application "Finder"
set desktopPath to path to desktop as string
set desktopFolder to folder desktopPath
-- Get list of files on the desktop
set desktopFiles to every item of desktopFolder
repeat with desktopItem in desktopFiles
if (class of desktopItem) is not folder then
set itemName to name of desktopItem
set itemType to kind of desktopItem
-- Replace characters that might cause issues in folder names
set itemType to my replaceCharacters(itemType, "/", "-")
-- Create folder for the file type if it doesn't exist
if not (exists folder (desktopPath & itemType)) then
make new folder at desktopFolder with properties {name:itemType}
end if
-- Move the file to its respective folder
move desktopItem to folder (desktopPath & itemType)
end if
end repeat
end tell
end sortFiles
-- Function to replace characters in a string
on replaceCharacters(theText, oldString, newString)
set AppleScript's text item delimiters to the oldString
set theTextItems to every text item of theText
set AppleScript's text item delimiters to the newString
set theText to theTextItems as string
set AppleScript's text item delimiters to ""
return theText
end replaceCharacters
-- Call the function to sort files
sortFiles()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment