Positioning Finder item's icons on the desktop

I wrote the following script having come across this article: Can I use AppleScript to reset items on my desktop?
As I have on my desktop shortcut to Scripts folder of user domain (that is, alias), I can do this:


tell application "Finder"
	set myAlias to alias file "Scripts alias" of desktop
	set desktop position of myAlias to {1000, 400}
end tell

or, this:


tell application "Finder"
	select alias file "Scripts alias"
	set desktop position of item 1 of (get selection) to {100, 600}
	close front Finder window
end tell

Thanks KniazidisR–that’s very useful. With a few additional lines of code, I can position icons on my desktop just as I want. This script sorts icons by categories, which consist of broken aliases then folders then files. Within each of these categories, icons are sorted by name. The user is notified of broken aliases, although this can be disabled.


set iconStartPosition to {75, 75} -- x and y coordinates of first icon
set iconPadding to 110 -- make negative for bottom-to-top icon ordering
set columnPadding to 110 -- make negative for right-to-left column ordering
set columnIconCount to 6 -- maximum number of icons in a column
set notifyBrokenAlias to true -- notify if an alias is broken

set {desktopFolders, desktopFiles} to {{}, {}}

tell application "Finder"
	
	set desktopIcons to sort (every item of (path to desktop)) by name
	
	repeat with anItem in desktopIcons
		if (class of anItem) = alias file then
			try
				if class of (original item of anItem) = folder then
					set end of desktopFolders to anItem
				else
					set end of desktopFiles to anItem
				end if
			on error
				if notifyBrokenAlias then my brokenAliasDialog(anItem)
				set beginning of desktopFolders to anItem
			end try
		else
			if (class of anItem) = folder then
				set end of desktopFolders to anItem
			else
				set end of desktopFiles to anItem
			end if
		end if
	end repeat
	
	set iconCount to 0
	set iconPosition to iconStartPosition
	repeat with anItem in (desktopFolders & desktopFiles)
		set desktop position of anItem to iconPosition
		set iconCount to iconCount + 1
		if ((iconCount mod columnIconCount) = 0) then
			set iconPosition to {((item 1 of iconPosition) + columnPadding), (item 2 of iconStartPosition)}
		else
			set iconPosition to {(item 1 of iconPosition), ((item 2 of iconPosition) + iconPadding)}
		end if
	end repeat
	
end tell

on brokenAliasDialog(anItem)
	set dialogText to "The folder or file referenced by desktop icon " & quote & (name of anItem) & quote & " may not exist."
	display dialog dialogText buttons {"OK"} default button 1 with title "Desktop Reset" with icon caution
end brokenAliasDialog

After almost a year’s use, I decided that my script in post 2 did too much and so I simplified it significantly. The operation of the revised script requires no explanation except to note:

  • For the script to operate, the Finder sort-by view preference has to be set to none.

  • If the user wants the icons to align with the right side of the screen, the columnSpace value has to be negative.

  • Sort order options include date created, date modified, kind, name, and size.

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 -100 -- make negative for right-to-left column ordering
	set columnIconCount to 7 -- maximum number of icons in a column
	
	set iconCount to 0
	set nextIcon to firstIcon
	
	tell application "Finder"
		set iconList to sort every item of (path to desktop) by name
		-- 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()




global fl, f

tell application "Finder"
	set fl to files of folder POSIX file "/Users/alias/" as alias list
end tell

tell application "Finder"
	repeat with f in fl
		tell application "Finder"
			open f
			activate
		end tell
	end repeat
end tell

repeat with j from 1 to 2
	tell application "Finder"
		set theCount to count of Finder windows
		repeat with i from 1 to theCount
			set theWindow to Finder window i
			tell theWindow
				set properties to {current view:list view, statusbar visible:true, sidebar width:0, toolbar visible:false, text size:10}
				tell its list view options
					set properties to {text size:10}
				end tell
			end tell
		end repeat
	end tell
end repeat

tell application "Finder"
	repeat with f in fl
		tell application "Finder"
			open f
			activate
		end tell
	end repeat
end tell

--set window position

tell application "Finder" to set the bounds of the window "alias name1" to {0, 23, 316, 258}
tell application "Finder" to set the bounds of the window "alias name2" to {0, 259, 316, 505}



to find your {0, 259, 316, 505} of a selected window

tell application "Finder"
	set theCount to count of Finder windows
	repeat with i from 1 to 1
		set theBounds to {0, 0, 0, 0}
		set theWindow to ""
		set theWindow to Finder window i --to front window
		tell theWindow
			set theName to name of theWindow
			set theBounds to the bounds of the theWindow
		end tell
	end repeat
end tell
{theName, theBounds}


– I thought I would add this “positioning icons on the desktop” script here. The script aligns icons on the desktop according to the selected icons’ leftmost or topmost icon position. (Topmost align code is commented out.) It works for me as-is, but you may want to customize it or improve it. I save a left-align version and a top-align version as apps with an “L” or “T” Finder icon, and put them in my Dock.
– Tested in Mac OS X 10.15.7 Catalina


tell application "Finder"
	
	set theItems to the selection
	
	set boundsList to {}
	repeat with anItem in theItems
		
		set theseBounds to (desktop position of anItem)
		set boundsList to boundsList & {theseBounds}
		
	end repeat
	
	--return boundsList --> {{2092, 1238}, {2128, 1352}, {2116, 1467}} -- test 3 icons
	
	
	-------------------------------------------Get minLeft
	
	set minLeft to item 1 of (item 1 of boundsList)
	
	repeat with thisPosition in boundsList
		
		set thisLeft to item 1 of thisPosition
		
		if thisLeft is less than minLeft then
			set minLeft to thisLeft
		end if
		
	end repeat
	
	-------------------------------------------Get minTop
	
	set minTop to item 2 of (item 1 of boundsList)
	
	repeat with thisPosition in boundsList
		
		set thisTop to item 2 of thisPosition
		
		if thisTop is less than minTop then
			set minTop to thisTop
		end if
		
	end repeat

	
	-------------------------------------------SET minLeft
	
	repeat with itemPosition in theItems
		
		set dtPos to desktop position of itemPosition
		
		set (item 1 of (dtPos)) to minLeft
		
		set desktop position of itemPosition to dtPos
		
	end repeat
	
	
	(*
	-------------------------------------------SET minTop
	
	repeat with itemPosition in theItems
	
		set dtPos to desktop position of itemPosition 
		
		set (item 2 of (dtPos)) to minTop

		set desktop position of itemPosition to dtPos
		
	end repeat
	*)
	
end tell

Browser: Safari 605.1.15
Operating System: macOS 10.14

kerflooey. I tested your script which works well, but I did notice one possible issue. If no desktop icons are selected, the script throws the following error:

Thanks for the script.

Thanks, peavine. Yeah, I left out the error checking. Again! -k