Skip to content

Instantly share code, notes, and snippets.

@mrienstra
Last active January 11, 2024 15:51
Show Gist options
  • Select an option

  • Save mrienstra/8330528 to your computer and use it in GitHub Desktop.

Select an option

Save mrienstra/8330528 to your computer and use it in GitHub Desktop.

Revisions

  1. mrienstra revised this gist Jan 9, 2014. 1 changed file with 10 additions and 11 deletions.
    21 changes: 10 additions & 11 deletions Rearrange Desktop Icons.scpt
    Original file line number Diff line number Diff line change
    @@ -1,24 +1,23 @@
    (*
    Based on http://www.tuaw.com/2012/12/24/applescript-desktop-icon-race/
    Inspired by http://namesakecomic.com/comic/happy-new-year-from-namesake/#comment-1182035013
    -- https://gist.github.com/mrienstra/8330528
    -- Based on http://www.tuaw.com/2012/12/24/applescript-desktop-icon-race/
    -- Inspired by http://namesakecomic.com/comic/happy-new-year-from-namesake/#comment-1182035013

    Rearranges Desktop icons to flow from left to right, top to bottom.
    To have this run automatically every time files are added or removed from the Desktop, set this script to run as a Desktop "Folder Action". (See https://developer.apple.com/library/mac/documentation/applescript/conceptual/applescriptlangguide/reference/ASLR_folder_actions.html )
    -- Rearranges Desktop icons to flow from left to right, top to bottom.

    You can edit lines 17 through 19 to change the spacing. The current values are reasonably close to the default grid spacing.
    -- To have this run automatically every time files are added or removed from the Desktop, set this script to run as a Desktop "Folder Action". (See https://developer.apple.com/library/mac/documentation/applescript/conceptual/applescriptlangguide/reference/ASLR_folder_actions.html )

    -- This is currently a rough proof-of-concept. It has only been tested with OS X 10.8.5 (Mountain Lion).

    -- Current known limitations: Does not work with "Label position" set to "Right" (specifically, icons will overlap).

    This is currently a rough proof-of-concept. It has only been tested with OS X 10.8.5 (Mountain Lion).

    Current known limitations: Does not work with "Label position" set to "Right" (specifically, icons will overlap).
    *)

    -- Adjust these for different spacing
    property theSpacingFactor : 1.0
    property theGutterXFactor : 0.57
    property theGutterYFactor : 0.57

    --


    on rearrangeDesktopIcons()
    tell application "Finder"
  2. mrienstra revised this gist Jan 9, 2014. 1 changed file with 62 additions and 45 deletions.
    107 changes: 62 additions & 45 deletions Rearrange Desktop Icons.scpt
    Original file line number Diff line number Diff line change
    @@ -3,10 +3,14 @@ Based on http://www.tuaw.com/2012/12/24/applescript-desktop-icon-race/
    Inspired by http://namesakecomic.com/comic/happy-new-year-from-namesake/#comment-1182035013
    Rearranges Desktop icons to flow from left to right, top to bottom.
    To have this run automatically every time files are added or removed from the Desktop, set this script to run as a Desktop "Folder Action". (See https://developer.apple.com/library/mac/documentation/applescript/conceptual/applescriptlangguide/reference/ASLR_folder_actions.html )
    You can edit lines 17 through 19 to change the spacing. The current values are reasonably close to the default grid spacing.
    This is currently a rough proof-of-concept. It has only been tested with OS X 10.8.5 (Mountain Lion).
    Known limitations: Does not work with "Label position" set to "Right" (specifically, icon text overlaps with icons).
    Current known limitations: Does not work with "Label position" set to "Right" (specifically, icons will overlap).
    *)

    -- Adjust these for different spacing
    @@ -16,49 +20,62 @@ property theGutterYFactor : 0.57

    --

    -- Determine the screen size
    tell application "Finder"
    tell icon view options of window of desktop
    set theArrangement to arrangement
    if {"not arranged", "snap to grid"} does not contain theArrangement as string then
    set theArrangementString to do shell script "python -c \"import sys; print unicode(sys.argv[1], 'utf8').title().encode('utf8')\" " & quoted form of (theArrangement as string)
    display alert "\"Desktop Rearranger\" AppleScript says:" message "Cannot rearrange Finder items, please change Desktop \"Sort by\" to \"None\" or \"Snap to Grid\" (currently set to \"" & theArrangementString & "\")." giving up after 10
    return
    end if
    set theIconSize to icon size
    set theLabelSize to text size
    end tell

    set theDesktopBounds to bounds of window of desktop
    set theDesktopWidth to item 3 of theDesktopBounds
    set theDesktopHeight to item 4 of theDesktopBounds

    -- Retrieve a list of items on the desktop
    set theDesktopItems to every item of desktop
    set theContestantOffset to theIconSize / 2

    set theSpacing to (theIconSize + theLabelSize + theContestantOffset) * theSpacingFactor
    set theGuttersX to theSpacing * theGutterXFactor
    set theGuttersY to theSpacing * theGutterYFactor
    set theMaxColumns to ((theDesktopWidth - theGuttersX * 2) / theSpacing) as integer
    set theMaxRows to ((theDesktopHeight - theGuttersY * 2) / theSpacing) as integer
    set theMaxLocations to theMaxRows * theMaxColumns

    set y to 1
    repeat with a from 1 to length of theDesktopItems
    set x to a mod theMaxColumns
    if x is 0 then
    set x to theMaxColumns
    end if
    on rearrangeDesktopIcons()
    tell application "Finder"
    tell icon view options of window of desktop
    set theArrangement to arrangement
    set theArrangementString to theArrangement as string
    if {"not arranged", "«constant ****narr»", "snap to grid", "«constant ****grda»"} does not contain theArrangementString then
    display alert "\"Rearrange Desktop Icons\" AppleScript says:" message "Cannot rearrange Desktop items, please change Desktop \"Sort by\" to \"None\" or \"Snap to Grid\"." giving up after 10
    return
    end if
    set theIconSize to icon size
    set theLabelSize to text size
    end tell

    set theDesktopBounds to bounds of window of desktop
    set theDesktopWidth to item 3 of theDesktopBounds
    set theDesktopHeight to item 4 of theDesktopBounds

    if a is greater than theMaxLocations then
    set desktop position of item a of theDesktopItems to {theGuttersX, theGuttersY}
    else
    set desktop position of item a of theDesktopItems to {theGuttersX + (x - 1) * theSpacing, theGuttersY + (y - 1) * theSpacing}
    end if
    -- Retrieve a list of items on the desktop
    set theDesktopItems to every item of desktop
    set theContestantOffset to theIconSize / 2

    if a mod theMaxColumns is 0 then
    set y to y + 1
    end if
    end repeat
    end tell
    set theSpacing to (theIconSize + theLabelSize + theContestantOffset) * theSpacingFactor
    set theGuttersX to theSpacing * theGutterXFactor
    set theGuttersY to theSpacing * theGutterYFactor
    set theMaxColumns to ((theDesktopWidth - theGuttersX * 2) / theSpacing) as integer
    set theMaxRows to ((theDesktopHeight - theGuttersY * 2) / theSpacing) as integer
    set theMaxLocations to theMaxRows * theMaxColumns

    set y to 1
    repeat with a from 1 to length of theDesktopItems
    set x to a mod theMaxColumns
    if x is 0 then
    set x to theMaxColumns
    end if

    if a is greater than theMaxLocations then
    set desktop position of item a of theDesktopItems to {theGuttersX, theGuttersY}
    else
    set desktop position of item a of theDesktopItems to {theGuttersX + (x - 1) * theSpacing, theGuttersY + (y - 1) * theSpacing}
    end if

    if a mod theMaxColumns is 0 then
    set y to y + 1
    end if
    end repeat
    end tell
    end rearrangeDesktopIcons



    on adding folder items to alias after receiving listOfAlias
    rearrangeDesktopIcons()
    end adding folder items to

    on removing folder items from alias after losing listOfAliasOrText
    rearrangeDesktopIcons()
    end removing folder items from

    rearrangeDesktopIcons()
  3. mrienstra created this gist Jan 9, 2014.
    64 changes: 64 additions & 0 deletions Rearrange Desktop Icons.scpt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,64 @@
    (*
    Based on http://www.tuaw.com/2012/12/24/applescript-desktop-icon-race/
    Inspired by http://namesakecomic.com/comic/happy-new-year-from-namesake/#comment-1182035013
    Rearranges Desktop icons to flow from left to right, top to bottom.
    This is currently a rough proof-of-concept. It has only been tested with OS X 10.8.5 (Mountain Lion).
    Known limitations: Does not work with "Label position" set to "Right" (specifically, icon text overlaps with icons).
    *)

    -- Adjust these for different spacing
    property theSpacingFactor : 1.0
    property theGutterXFactor : 0.57
    property theGutterYFactor : 0.57

    --

    -- Determine the screen size
    tell application "Finder"
    tell icon view options of window of desktop
    set theArrangement to arrangement
    if {"not arranged", "snap to grid"} does not contain theArrangement as string then
    set theArrangementString to do shell script "python -c \"import sys; print unicode(sys.argv[1], 'utf8').title().encode('utf8')\" " & quoted form of (theArrangement as string)
    display alert "\"Desktop Rearranger\" AppleScript says:" message "Cannot rearrange Finder items, please change Desktop \"Sort by\" to \"None\" or \"Snap to Grid\" (currently set to \"" & theArrangementString & "\")." giving up after 10
    return
    end if
    set theIconSize to icon size
    set theLabelSize to text size
    end tell

    set theDesktopBounds to bounds of window of desktop
    set theDesktopWidth to item 3 of theDesktopBounds
    set theDesktopHeight to item 4 of theDesktopBounds

    -- Retrieve a list of items on the desktop
    set theDesktopItems to every item of desktop
    set theContestantOffset to theIconSize / 2

    set theSpacing to (theIconSize + theLabelSize + theContestantOffset) * theSpacingFactor
    set theGuttersX to theSpacing * theGutterXFactor
    set theGuttersY to theSpacing * theGutterYFactor
    set theMaxColumns to ((theDesktopWidth - theGuttersX * 2) / theSpacing) as integer
    set theMaxRows to ((theDesktopHeight - theGuttersY * 2) / theSpacing) as integer
    set theMaxLocations to theMaxRows * theMaxColumns

    set y to 1
    repeat with a from 1 to length of theDesktopItems
    set x to a mod theMaxColumns
    if x is 0 then
    set x to theMaxColumns
    end if

    if a is greater than theMaxLocations then
    set desktop position of item a of theDesktopItems to {theGuttersX, theGuttersY}
    else
    set desktop position of item a of theDesktopItems to {theGuttersX + (x - 1) * theSpacing, theGuttersY + (y - 1) * theSpacing}
    end if

    if a mod theMaxColumns is 0 then
    set y to y + 1
    end if
    end repeat
    end tell