how to set (x,y) location of icons in a folder?

Hi,

Does anyone know how I can find out how to rearrange icons in a folder programmatically in Applescript?

I basically want to create a folder, drop some files in it and precisely programmatically set the (x,y) location of each of the icons.

I would also like to programmatically set the background image of the folder.

Thanks very much for your help.

Hi zacharydenison,

You can position items in the Finder using code like follows. But the folder in question must currently be set to icon view. Of course, you can also set the view options of a folder with the Finder’s AppleScript commands. I recommend to have a look at the Finder’s AppleScript dictionary, it’s very comprehensive (Script Editor > Window > Library > Finder).

To test the script below, just open a folder, set its view to icon view and select one or more items therein. Then start the script.


tell application "Finder"
	set finderitem to item 1 of (selection as list)
	-- current position of the Finder item
	set itemposition to position of finderitem
	-- returns e.g. {94, 33}
	set posx to item 1 of itemposition
	set posy to item 2 of itemposition
	-- repositioning the Finder item to a new position
	set newitemposition to {posx + 50, posy + 50}
	set position of finderitem to newitemposition
	-- see the result?
	delay 3
	-- okay, let's go back to the old position
	set position of finderitem to itemposition
end tell

The code worked fine on my Mac running Mac OS X 10.5 Leopard.

Automatically setting the background image of a folder is not easy, I guess it cannot be done with AppleScript alone…

Don’t worry, no offense here.
As the OP didn’t described the OS he uses, I wish to say that if he is using 10.4.x, the given script will be useless.
Due to a Finder oddity, the icon will revert to its original position some seconds later.

Yvan KOENIG (from FRANCE samedi 17 mai 2008 18:11:52)

actually I am using leopard myself and the script works great. I can’t figure out how to set the background image in applescript though - I guess I will use the .DS_Store from a folder with a background image, and just change the image using the shell - then reposition the icons using your script.

Hi zacharydenison

To set the background of a open folder window try the below.

tell application "Finder"
	set Image to "Macintosh HD:Users:Budgie:Pictures:iPhoto Library:CHICK.jpg"
	set background picture of icon view options of front window to Image as alias
end tell

Budgie

OS 10.4.11