Positioning Finder item's icons on the desktop but in columns per kind

Good day guys

I found this amazing script from peavine from 2018, it arranges finder icons perfectly and neatly on the right side of the desktop - cool stuff and still works flawlessly. Is it possible to tweak that script to do the following?

Use for each category/kind it’s own column/row. So that it would sort per kind but start for each kind it’s own new column. If there are more then one column of icons of the same kind needed, it would start a new row of the same kind to sort them.

Too difficult for a beginner like me, main question, how do you get the count per kind? But maybe one of the pros likes to have a look at it. Thanks in advance.

on main()
	
	set firstIcon to {1850, 150} -- x and y coordinates of first icon
	set iconSpace to 120 -- vertical space between icons
	set columnSpace to -130 -- make negative for right-to-left column ordering
	set columnIconCount to 9 -- maximum number of icons in a column
	
	set iconCount to 0
	set nextIcon to firstIcon
	
	tell application "Finder"
		-- Sort order options include date created, date modified, kind, name, and size
		set iconList to sort every item of (path to desktop) by kind
		-- set iconList to reverse of iconList -- reverse sort order
		repeat with anIcon in iconList
			set desktop position of anIcon to nextIcon
			set iconCount to iconCount + 1
			if (iconCount mod columnIconCount) = 0 then
				set nextIcon to {(item 1 of nextIcon) + columnSpace, item 2 of firstIcon}
			else
				set nextIcon to {item 1 of nextIcon, (item 2 of nextIcon) + iconSpace}
			end if
		end repeat
	end tell
	
end main

main()

This will cycle through every desktop file for its kind, making a list of the represented kinds. Then, cycle through that list counting how many instances of each there are. With each kind, it reports a count but you could easily make a list as well. Note that it ignores folders.

tell application "Finder"
	-- list of every file's kind	
	set allKind to kind of every file of desktop
	-- list of each represented kind (unique)
	set uniKind to {}
	repeat with efk in allKind
		if uniKind does not contain efk as text then set end of uniKind to efk as text
	end repeat
	
	-- count instances of each kind among every file
	-- cycle through list of kinds
	repeat with euk from 1 to count of uniKind
		set cpk to 0
		-- cycle through each file's kind
		repeat with efk from 1 to count of allKind
			if item efk of allKind is item euk of uniKind then set cpk to cpk + 1
		end repeat
		-- after each kind, give report
		tell me to display dialog (cpk as text) & " (of " & (count of allKind) & "): " & item euk of uniKind
	end repeat
end tell

NewtonsLaws. Thanks for the kind words about my script. I’m not entirely certain what you are asking but I’ve included a script below which may do what you want. If I misunderstand and you need a tabulation of file kinds of icons on the desktop then Mockman’s script is the one to use.

-- Revised 2021.07.03

on main()
	set firstIconPosition to {1850, 150} -- x and y coordinates of first icon
	set iconSpace to 120 -- vertical space between icons
	set columnSpace to -120 -- make negative for right-to-left column ordering
	set columnIconCount to 6 -- maximum number of icons in a column
	
	set iconCount to 0
	set nextIconPosition to firstIconPosition
	
	tell application "Finder"
		set iconList to sort every item of (path to desktop) by kind
		if iconList = {} then error number -128
		
		set desktop position of (item 1 of iconList) to firstIconPosition
		set firstIconKind to kind of (item 1 of iconList)
		
		repeat with anIcon in (rest of iconList)
			set iconCount to iconCount + 1
			set nextIconKind to kind of anIcon
			if firstIconKind ≠ nextIconKind or (iconCount mod columnIconCount) = 0 then
				set nextIconPosition to {(item 1 of nextIconPosition) + columnSpace, item 2 of firstIconPosition}
				set iconCount to 0
			else
				set nextIconPosition to {item 1 of nextIconPosition, (item 2 of nextIconPosition) + iconSpace}
			end if
			set desktop position of anIcon to nextIconPosition
			set firstIconKind to nextIconKind
		end repeat
	end tell
end main

main()

peavine, you really rock it! Your script does EXACTLY what I wanted to accomplish and even in a very short code-structure (a lot to learn from that), thanks a million! Also thanks to Mockman for giving me a helpful starting point. Thanks again guys.

I rewrote my script in Post 3 using ASOjbC code provided by Nigel and found the new version to be almost three times faster. This script arranges desktop icons in columns by kind and subsorts by name.

use framework "Foundation"
use scripting additions

on main()
	set firstIconPosition to {1850, 150} -- x and y coordinates of first icon
	set iconSpace to 120 -- vertical space between icons
	set columnSpace to -120 -- make negative for right-to-left column ordering
	set columnIconCount to 7 -- maximum number of icons in a column
	
	set {iconList, kindList} to getIconInfo()
	if iconList = {} then error number -128
	
	set iconCount to 0
	set nextIconPosition to firstIconPosition
	
	tell application "Finder"
		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
			else
				set nextIconPosition to {item 1 of nextIconPosition, (item 2 of nextIconPosition) + iconSpace}
			end if
			set desktop position of anIcon to nextIconPosition
		end repeat
	end tell
end main

on getIconInfo()
	set fileManager to current application's NSFileManager's defaultManager()
	
	set desktopPath to POSIX path of (path to desktop)
	set desktopURL to current application's |NSURL|'s fileURLWithPath:(desktopPath)
	set resourceKeys to {current application's NSURLPathKey, current application's NSURLNameKey, current application's NSURLLocalizedTypeDescriptionKey}
	set iconURLs to fileManager's contentsOfDirectoryAtURL:(desktopURL) includingPropertiesForKeys:(resourceKeys) options:(current application's NSDirectoryEnumerationSkipsHiddenFiles) |error|:(missing value)
	
	set sortingDictionaries to {}
	repeat with thisURL in iconURLs
		set end of sortingDictionaries to (thisURL's resourceValuesForKeys:(resourceKeys) |error|:(missing value))
	end repeat
	set sortingDictionaries to current application's NSMutableArray's arrayWithArray:(sortingDictionaries)
	
	set kindDescriptor to current application's NSSortDescriptor's sortDescriptorWithKey:(current application's NSURLLocalizedTypeDescriptionKey) ascending:(true)
	set nameDescriptor to current application's NSSortDescriptor's sortDescriptorWithKey:(current application's NSURLNameKey) ascending:(true) selector:("localizedStandardCompare:")
	sortingDictionaries's sortUsingDescriptors:({kindDescriptor, nameDescriptor})
	
	set kindList to (sortingDictionaries's valueForKey:(current application's NSURLLocalizedTypeDescriptionKey)) as list
	set iconList to (sortingDictionaries's valueForKey:(current application's NSURLPathKey)) as list
	repeat with anItem in iconList
		set anItem's contents to (anItem as POSIX file as alias)
	end repeat
	
	return {iconList, kindList}
end getIconInfo

main()

A similar script that subsorts by modification date can be found at:

https://macscripter.net/viewtopic.php?id=48569