read and write position of item on desktop folder

Hey guys! I got a question about Scripting Finder.

I ran across this problem. I have a program that is not scriptable, and changes my moniter resolution. To fix this problem, I was hoping to write an applescript that once you run it, it will record all the file locations into a file, and another applescript that will restore these files to their origional place. However, I haven’t figured out how to script finder very well. Any help would be appriciated.

Sincerely, David

David,
This is a PRIME candidate for recording, assuming you’re still using OS 9. (OS X Finder isn’t yet recordable.) Just open your script editor, hit record, move your desktop items to their desired positions, then hit stop in your script editor. You’ll then have all the coordinates and you can tweak from there, if you like. :slight_smile:
Be well.

tell application "Finder"
	get {position, name} of every item of desktop
end tell
--And now lets move someting around the screen
tell application "Finder"
	set position of (item 1 of desktop) to {40, 40}
	set position of (item 1 of desktop) to {760, 40}
	set position of (item 1 of desktop) to {720, 560}
	set position of (item 1 of desktop) to {40, 560}
	set position of (item 1 of desktop) to {40, 40}
end tell

Jean-Baptiste LE STANG

Here is a more detailed script.

property PLIST : {}
property Nlist : {}
set mybutt to button returned of (display dialog "What to do" buttons {"Restore", "Memorize", "Quit"})
if mybutt = "Quit" then quit
if mybutt = "Restore" then
	tell application "Finder"
		repeat with x from 1 to count Nlist
			set (position of item x of the desktop) to item x of PLIST
		end repeat
	end tell
end if
if mybutt = "Memorize" then
	tell application "Finder"
		set PLIST to position of every item of the desktop
		set Nlist to name of every item of the desktop
	end tell
end if

Below is my “Desktop Backup/Restore” script, which does exactly what you want. It records the names and positions of all desktop icons to a file in backup mode. In restore mode it reads that files and uses the information to put the icons back where they belong.

global myFile, myFldr, fileNbr, oldFile
set fileNbr to 0

tell application "Finder"
	set myFldr to the (path to the preferences folder) as text
	set myFile to myFldr & "DesktopIconBackup"
	set oldFile to myFldr & "PriorDesktopIconBackup"
	tell me
		activate
		set dialogResult to display dialog ¬
			"Do you want to:" buttons {"Backup Desktop icons", "Restore Desktop icons", "Cancel"} ¬
			default button 1
	end tell
	if button returned of dialogResult is "Backup Desktop icons" then
		tell me to doBackup()
	else
		tell me to doRestore()
	end if
end tell

on doBackup()
	tell application "Finder"
		try
			delete file oldFile
		end try
		if file myFile exists then
			set locked of file myFile to false
			set name of file myFile to "PriorDesktopIconBackup"
		end if
		make file at container myFldr with properties ¬
			{name:"DesktopIconBackup", locked:false, creator type:"ToyS", file type:"TEXT", comment:"I hold the secrets of the Desktop!"}
		set itemCnt to count of the items of the desktop
		set theItems to the items of the desktop
		set thePos to the position of the items of the desktop
		tell me to set fileNbr to (open for access file myFile with write permission)
		try
			set eof fileNbr to 0
			repeat with i from 1 to itemCnt
				set theName to item i of theItems as string
				set x to item 1 of item i of thePos
				set y to item 2 of item i of thePos
				tell me to writeItem(theName, x, y)
			end repeat
			tell me to close access fileNbr
			set locked of file myFile to true
		on error m number n
			try
				tell me to close access fileNbr
			end try
			tell me to onError(1, m, n)
		end try
	end tell
end doBackup

on doRestore()
	tell application "Finder"
		if file myFile exists then
			try
				set fileNbr to (open for access file myFile)
			on error m number n
				tell me to onError(2, m, n)
			end try
		else
			try
				set theFile to (choose file with prompt "Where is the file \"DesktopIconBackup\"?" of type {"TEXT"})
				if the name of theFile is "DesktopIconBackup" then
					set fileNbr to (open for access theFile)
					set myFile to (theFile as text)
				else
					display dialog "You have chosen an invalid " & ¬
						"file!" buttons {"Cancel"} default button 1 with icon stop
				end if
			on error m number n
				tell me to onError(3, m, n)
			end try
		end if
		try
			set theList to (read fileNbr before return as {text} using delimiter {tab})
			repeat until theList is {}
				set theName to item 1 of theList
				if alias theName exists then
					set x to ((item 2 of theList) as integer)
					set y to ((item 3 of theList) as integer)
					set position of alias theName to {x, y}
				end if
				tell me to set theList to (read fileNbr before return as {text} using delimiter {tab})
			end repeat
			tell me to close access fileNbr
		on error m number n
			try
				tell me to close access fileNbr
			end try
			if n is not -39 then
				tell me to onError(4, m, n)
			end if
		end try
	end tell
end doRestore

on writeItem(theName, x, y)
	tell application "Finder"
		try
			write (theName & tab & x & tab & y & return) to fileNbr
		on error m number n
			try
				tell me to close access fileNbr
			end try
			tell me to onError(5, errMsg, errNbr)
		end try
	end tell
end writeItem

on onError(locNum, errMsg, errNbr)
	display dialog "Error encountered at #" & (locNum as text) & return & ¬
		"Error message: " & errMsg & return & ¬
		"Error number: " & (errNbr as text) ¬
		buttons {"Cancel"}
end onError

Jean,
Man am I swamped. I tried running your script, and it keeps giving me this error, saying the window has to be button, or icon based… Well, Finder only has those two choices, but I still don’t get why it willnot work. I also forgot to lay out in my last posting that i am running mac os 9.0.4. Major bummer, but I don’t like the "Window"menu that pops up when I upgrade… Oh well. Any way, this script is cool, It just will not let me do what it is suppost to do. Please, can anyone help me out a little on this problem, I would greatly appriciate it.
Sincerely, David

In the Finder’s “View” menu, select “View options”. I believe icon arrangement needs to be set to none.
Rob

You can temporarily change the “spatial view arrangement” of the desktop folder, move stuff around, and then change it back:

tell application "Finder" 
	set theDesktop to content space of desktop 
	set holdArrange to spatial view arrangement of theDesktop 
	set spatial view arrangement of theDesktop to not arranged 
	--Move stuff around 
	set spatial view arrangement of theDesktop to holdArrange 
end tell

Marc,
Gotta a different problem now. How do you display a dialog that will show the type (or rather, Kind) of file, but scripting finder to do this? Greatly appriciate it.
Sincerely, David

The file type is a four character code. By the file “kind” I’m assuming you’re talking about what shows up in a column in a Finder list view. You can get it this way:

set theFile to (choose file) 
tell application "Finder" 
	display dialog paragraph 1 of (get description of theFile) 
end tell