FYI Applescript to arrange folder icons in a circle

This is one of those nonsensical scripts that might be handy some of the time…

First you select the folder for which the icons to be put in circle formation, then you enter the circle radius in px.
Before running the script, open the folder and make the finder window large enough to contain the intended circle.
Documents and alias’ folders are re-arranged, folders are not.

I use it to clean up the desktop. Move all the desktop stuff into a temporary folder, run the script and move the re-arranged icons back to the desktop.


(*
 Organize your folder items in a circle
 
 Example:
 Choose arrange by name and then apply this script to show 
 all folder items (except subfolders) in a circle (clockwise ordered by name)

 Copyright 2007 Chaos Geordend, Johan van Oostrum

 2007-03-08 JvO, New script
*)

--
-- Main()
--
copy the text item delimiters to old_tid -- save text delimiters
set the text item delimiters to ", "

set theFolder to (choose folder with prompt "Choose a folder to re-organize...")
tell application "Finder"
	open folder theFolder
	--display dialog name of theFolder as text
end tell

--set radius to text returned of (display dialog "Radius?" default answer 250)
--do_Organize(theFolder, radius)

set distance to text returned of (display dialog "Icon spacing (in px)?" default answer 100)
do_Organize(theFolder, distance)

copy old_tid to the text item delimiters -- restore original delimiters
return

--
-- Procedures
--
----------------------------
on do_Organize(theFolder, d)
	local r, O, n, phi, cosPhi, sinPhi
	local theFiles, theBounds, startpos, thepos
	
	tell application "Finder"
		
		-- get the file(reference)s in the desktop folder
		set theFiles to every file of theFolder
		set n to the count of theFiles
		if n < 2 then return
		
		-- rotation step angle in degrees
		set phi to 360 / n
		
		-- radius depends on the icons spacing
		set r to (n * d / (2 * pi))
		
		-- determine the center of the window (containing the items to be organized)
		set O to {0, 0}
		set theBounds to the bounds of Finder window (name of theFolder as text)
		copy (round ((third item of theBounds) - (first item of theBounds)) / 2) to first item of O
		copy (round ((fourth item of theBounds) - (second item of theBounds)) / 2) to second item of O
		
		-- reposition the first item
		set the position of the first item of theFiles to {first item of O, (second item of O) - r}
		
		-- rotate the items that remain...
		set startpos to {0, -r}
		set thepos to {0, 0}
		repeat with i from 2 to n
			
			--display dialog "name: " & the name of item i of theFiles
			tell me
				copy cosine((i - 1) * phi) to cosPhi
				copy sine((i - 1) * phi) to sinPhi
			end tell
			
			-- Rotate:			
			-- x := x * cos(phi) - y * sin(phi) 
			-- y := x * sin(phi) + y * cos(phi)
			copy ((first item of startpos) * cosPhi - (second item of startpos) * sinPhi) to first item of thepos
			copy ((first item of startpos) * sinPhi + (second item of startpos) * cosPhi) to second item of thepos
			
			set the position of item i of theFiles ¬
				to {round ((first item of thepos) + (first item of O)), ¬
				round ((second item of thepos) + (second item of O))}
			
		end repeat
		
	end tell
end do_Organize

-- Trig functions from http://lists.apple.com/archives/applescript-users/2004/Feb/msg00939.html
-- "A simple Taylor series expansion needs a whole lot more iterations for angles for 
-- which the answer is nearly unity. Chebycheff polynomials are a whole lot better."
----------------------------
on cosine(x) -- degrees
	local myCos, numerator, denominator, factor
	
	set myCos to 0
	if (x = 90) or (x = 270) then
		set myCos to 0
	else
		set x to (x - (((x / 360) div 1) * 360)) * (pi / 180)
		set {myCos, numerator, denominator, factor} to {0, 1, 1, -(x ^ 2)}
		repeat with i from 2 to 40 by 2
			set myCos to myCos + numerator / denominator
			set numerator to numerator * factor
			set denominator to denominator * i * (i - 1)
		end repeat
	end if
	return myCos
end cosine

----------------------------
on sine(x) -- degrees
	local mySin, numerator, denomintator, factor
	
	set mySin to 0
	if (x = 180) or (x = 360) then
		set mySin to 0
	else
		set x to (x - (((x / 360) div 1) * 360)) * (pi / 180)
		set {mySin, numerator, denominator, factor} to {0, x, 1, -(x ^ 2)}
		repeat with i from 3 to 40 by 2
			set mySin to mySin + numerator / denominator
			set numerator to numerator * factor
			set denominator to denominator * i * (i - 1)
		end repeat
	end if
	return mySin
end sine

I like the idea :smiley:
On first run I detected something minor.
Depending on how you have your Finder set up, the window name will differ.
To fix that, I replaced:

set theBounds to the bounds of Finder window (name of theFolder as text)

with:

set theBounds to the bounds of first Finder window

after that mod the script ran without errors on my current setup: OS X 10.5.x AS 2.0.1
unfortunately, my icons didn’t move the first time as I had forgotten to turn off the window’s automatic icon rearrangement.

I had a script that would scale the finder window and move it in a circle. Well the idea would have been to spiral away the window.

hmm, icons arranged in a spiral, that’d be the hit!