Friday, October 22, 2021

#1 2021-08-01 09:33:44 am

From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1089

Sort and Arrange Desktop Icons

The following script sorts and arranges icons on the desktop. The primary sort order is by kind, and all icons of the same kind are grouped together in a separate column. The secondary sort is by name or, optionally, by modification date.  A few comments:

* To test this script, paste it into a script editor and run. For the script to work properly, "Sort By" in the desktop's context menu must be set to none.

* The top of the script contains six user-settable options.

* This script was originally written as two separate scripts (see links below).

* Nigel Garvey contributed significantly to this script.


-- Revised 2021.08.03

use framework "Foundation"
use scripting additions

on main()
   set firstIconPosition to {1850, 150} -- x and y coordinates of first icon
   set rowSpace to 120
   set columnSpace to -120 -- negative for right-to-left column ordering
   set columnIconCount to 6 -- maximum number of icons in a column
   set subsortByName to true -- false to subsort by modification date
   set ascendingSortOrder to true
   set iconCount to 0
   set nextIconPosition to firstIconPosition
   set {iconList, kindList} to getIconInfo(subsortByName, ascendingSortOrder)
   tell application "Finder"
       set selection to desktop -- removes selection from icons but disable if desired
       set desktop position of (item 1 of iconList) to firstIconPosition
       set firstIconKind to item 1 of kindList
       repeat with i from 2 to (count iconList)
           set anIcon to item i of iconList
           set iconCount to iconCount + 1
           set nextIconKind to item i of kindList
           if firstIconKind ≠ nextIconKind or iconCount mod columnIconCount = 0 then
               set nextIconPosition to {(item 1 of nextIconPosition) + columnSpace, item 2 of firstIconPosition}
               set firstIconKind to nextIconKind
               set iconCount to 0
               set nextIconPosition to {item 1 of nextIconPosition, (item 2 of nextIconPosition) + rowSpace}
           end if
           set desktop position of anIcon to nextIconPosition
       end repeat
   end tell
end main

on getIconInfo(subsortByName, ascendingSortOrder)
   set desktopPath to current application's NSHomeDirectory()'s stringByAppendingPathComponent:"Desktop"
   set desktopURL to current application's |NSURL|'s fileURLWithPath:desktopPath
   if subsortByName then
       set resourceKeys to {current application's NSURLPathKey, current application's NSURLNameKey, current application's NSURLLocalizedTypeDescriptionKey}
       set resourceKeys to {current application's NSURLPathKey, current application's NSURLContentModificationDateKey, current application's NSURLLocalizedTypeDescriptionKey}
   end if
   set iconURLs to (current application's NSFileManager's defaultManager())'s contentsOfDirectoryAtURL:desktopURL includingPropertiesForKeys:resourceKeys options:(current application's NSDirectoryEnumerationSkipsHiddenFiles) |error|:(missing value)
   if iconURLs's |count|() = 0 then error number -128
   set sortingList to {}
   repeat with aURL in iconURLs
       set end of sortingList to (aURL's resourceValuesForKeys:resourceKeys |error|:(missing value))
   end repeat
   set sortingArray to current application's NSMutableArray's arrayWithArray:sortingList
   set kindDescriptor to current application's NSSortDescriptor's sortDescriptorWithKey:(current application's NSURLLocalizedTypeDescriptionKey) ascending:true selector:"localizedStandardCompare:"
   if subsortByName then
       set subsortDescriptor to current application's NSSortDescriptor's sortDescriptorWithKey:(current application's NSURLNameKey) ascending:ascendingSortOrder selector:"localizedStandardCompare:"
       set subsortDescriptor to current application's NSSortDescriptor's sortDescriptorWithKey:(current application's NSURLContentModificationDateKey) ascending:ascendingSortOrder
   end if
   sortingArray's sortUsingDescriptors:({kindDescriptor, subsortDescriptor})
   set iconList to (sortingArray's valueForKey:(current application's NSURLPathKey)) as list
   repeat with aPath in iconList
       set contents of aPath to (aPath as POSIX file as alias)
   end repeat
   set kindList to (sortingArray's valueForKey:(current application's NSURLLocalizedTypeDescriptionKey)) as list
   return {iconList, kindList}
end getIconInfo


Last edited by peavine (2021-08-03 07:27:59 am)

2018 Mac mini - macOS Catalina - Script Debugger 8



Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)