Skip to content

Instantly share code, notes, and snippets.

@ccstone
Last active January 5, 2025 00:27
Show Gist options
  • Select an option

  • Save ccstone/5823998 to your computer and use it in GitHub Desktop.

Select an option

Save ccstone/5823998 to your computer and use it in GitHub Desktop.

Revisions

  1. Christopher Stone revised this gist Oct 3, 2015. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions Desktop_Sweeper.applescript
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,9 @@
    ------------------------------------------------------------------------------------------------
    # 2015/10/03 10:42
    # NOTE: El Capitan breaks the whose clauses in this.
    # They still work but take 30+ seconds instead of 3-4.
    # I will update this with ASObjC when I get the chance.
    ------------------------------------------------------------------------------------------------
    # Auth: Christopher Stone <[email protected]>
    # dCre: 2012-09-30 : 16:51
    # dMod: 2013-06-20 : 10:35
  2. ccstone revised this gist Jun 20, 2013. No changes.
  3. ccstone revised this gist Jun 20, 2013. No changes.
  4. ccstone renamed this gist Jun 20, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. ccstone renamed this gist Jun 20, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  6. ccstone revised this gist Jun 20, 2013. No changes.
  7. ccstone created this gist Jun 20, 2013.
    122 changes: 122 additions & 0 deletions Desktop_Sweeper
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,122 @@
    ------------------------------------------------------------------------------------------------
    # Auth: Christopher Stone <[email protected]>
    # dCre: 2012-09-30 : 16:51
    # dMod: 2013-06-20 : 10:35
    # Appl: Finder
    # Task: Sweep Stuff off the desktop in an organized fashion.
    # Deps: Only OSX components used.
    # Tags: @Applescript, @Finder, @Sweep, @Desktop
    # Vers: 1.0
    ------------------------------------------------------------------------------------------------
    property exlusionList : items 1 thru -2 of {¬
    "Action_List.txt", ¬
    "Aliases to Directories", ¬
    "Catch-All", ¬
    "Desktop Notes.rtf", ¬
    "Duplicates", ¬
    "FILE!", ¬
    "Meta-Research.txt", ¬
    "Sweep Desktop", ¬
    "Working Items (Current)", ¬
    "Working Scripts", ¬
    ""}
    ------------------------------------------------------------------------------------------------
    # Separation between file-extensions & folder name must be at least 5 spaces.
    # Multiple file-extensions MUST be separated by one space.
    property fileExtFilters : text 2 thru -2 of "
    dmg Disk_Images
    flv mov mp4 Movie_Files
    gif jpg png Image_Files
    rtf rtfd RTF_Files
    scpt Script_Files
    sit zip Compressed_Archives
    txt Text_Files
    textClipping Clipping_Files
    webarchive Web_Archives
    webbookmark webloc Web_Bookmarks
    worksheet BBEdit_Worksheets
    "
    ------------------------------------------------------------------------------------------------

    try

    set desktopFolder to path to desktop folder
    set cleanupFolder to ("" & (path to documents folder) & "Sweep Desktop:")
    set cleanList to {}

    try
    alias cleanupFolder
    on error
    do shell script "mkdir " & (quoted form of (POSIX path of cleanupFolder))
    end try

    set usefileExtFilters to do shell script "sed -E 's![ ]{5,}! !' <<< " & quoted form of fileExtFilters

    set AppleScript's text item delimiters to tab

    tell application "Finder"
    tell desktop

    # Create move-to record with file-extension filters.
    repeat with i in (paragraphs of usefileExtFilters)
    set _temp to {words of first text item, last text item} of contents of i
    set itemsToMove to (items whose kind is not "Volume" and name is not in exlusionList and ¬
    name does not start with "Cleaned-" and name extension is in (item 1 of _temp) and ¬
    label index is not in {2, 6, 7}) as alias list
    if itemsToMove ≠ {} then
    set end of cleanList to {File_List:itemsToMove, Sub_Folder:item 2 of _temp}
    end if
    end repeat

    # Add folders to move-to record if appropriate.
    set itemsToMove to (folders whose kind is not "Volume" and name is not in exlusionList and ¬
    name does not start with "Cleaned-" and label index is not in {2, 6, 7}) as alias list
    if itemsToMove ≠ {} then
    set end of cleanList to {File_List:itemsToMove, Sub_Folder:"Folders"}
    end if

    # return cleanList

    if cleanList ≠ {} then

    # Make new Clean-up Folder
    set dateTimeString to do shell script "date \"+%Y%m%d-%H%M%S\""
    set newFolderName to "Cleaned-" & dateTimeString
    set newFolder to make new folder at (cleanupFolder as alias) with properties {name:newFolderName}
    set label index of newFolder to 2
    set _alias to make alias to newFolder at it
    set label index of _alias to 2

    # Move items in move-to record.
    repeat with ndx in cleanList
    set _folder to (make new folder at newFolder with properties {name:Sub_Folder of ndx}) as alias
    move File_List of ndx to _folder
    end repeat

    # Move left-over (miscellaneous) items.
    set itemsToMove to (items whose kind is not "Volume" and name is not in exlusionList and ¬
    name does not start with "Cleaned-" and label index is not in {2, 6, 7}) as alias list
    if itemsToMove ≠ {} then
    set _folder to (make new folder at newFolder with properties {name:"Miscellaneous_Files"}) as alias
    move itemsToMove to _folder
    end if

    open newFolder
    tell application "Finder"'s front window to if its bounds ≠ {0, 44, 844, 1196} ¬
    then set its bounds to {0, 44, 844, 1196}

    else
    beep
    end if

    end tell

    end tell

    on error e number n
    set e to e & return & return & "Num: " & n
    tell current application to set dDlg to display dialog e with title "ERROR!" buttons {"Cancel", "Copy", "OK"} default button "OK"
    if button returned of dDlg = "Copy" then set the clipboard to e
    end try

    ------------------------------------------------------------------------------------------------