Desktop Icon location

Is there way to find the X/Y placement of a desktop Icon?
And a way to place an icon to the result of above?
(via AS of course)

Hi,

there is a property desktop position in Finder’s dictionary

get:

tell application "Finder"
	get desktop position of item 1 of desktop --> {x, y}
end tell

set:

tell application "Finder"
	set desktop position of item 1 of desktop to {100, 100}
end tell

Hope this helps a little. (Borrowed Stefan’s ideas, then came up with this)


tell application "Finder"
	set firstIcon to (choose file) --or path if you know it
	set p to desktop position of firstIcon
	set p1 to item 1 of p
	set p2 to item 2 of p
	set secondIcon to (choose file) --or path
	set desktop position of secondIcon to {p1, p2}
end tell

:stuck_out_tongue:

Greg Ledger
macproductionartist.wordpress.com

Hi,

When my computer restarts, all my desktop icons loose their original position. What would the easiest way to get the position of all desktop items and to later restore them to there original place ?

Thanks in advance.

Robert Lespérance

If you save this as a stay open application bundel and add the application to your log-in items, it should do what you’ve asked.

property dekstopIconLocations : missing value -- {icon alias string, x, y}

on run
	if desktopIconLocations is not missing value then
		repeat with i in desktopIconLocations
			tell application "Finder" to if exists (alias (item 1 of i)) then set desktop position of (alias (item 1 of i)) to {(item 2 of i), (item 3 of i)}
		end repeat
	end if
end run

on quit
	set newDesktopIconLocations to {}
	tell application "Finder"
		set allDesktopIcons to (every item of (path to desktop))
		
		repeat with i in allDesktopIcons
			set aliasString to (i as alias) as string
			set thePosition to desktop position of i
			set end of newDesktopIconLocations to {aliasString, (item 1 of thePosition), (item 2 of thePosition)}
		end repeat
	end tell
	
	set desktopIconLocations to newDesktopIconLocations
end quit

When you login, the script will restore all desktop item locations according to the desktopIconLocations-variable.
When you shut down your computer (and the application quits), it saves all the desktop item locations to the same variable.

If you want your application to be not shown in the dock you’ll have to right click it, choose show package contents and open the info.plist file in TextEdit and change it contents to:

hope it works,
ief2

PS: script is not tested

Hi ief2,

Thanks for your help …

I have a ON IDLE handler running in background on my computer that launches at startup. I would like to insert «the icon positionning» checkup in that handler. I want it to:

¢ take a snapshot of my desktop’s icons position at every loop of the IDLE handler and keep that positionning in memory
¢ in case of a crash or a computer restart all icon be put back to their last snapshot position at startup

How can I do that ?

Regards.

Robert Lespérance

Not tested and done in a hurry, so there could be a couple of mistakes:

property dekstopIconLocations : missing value -- {icon alias string, x, y}

on run
	tell application "Finder" to if (exists (folder ".desktopIconLocations" of home)) then if (count every item of folder ".desktopIconLocations" of home) is not 0 then
		set allFiles to every item of folder ".desktopIconLocations" of home
		
		set allDates to {}
		repeat with i in allFiles
			set myName to name of i
			set end of allDates to my extractDate(myName)
		end repeat
		
		set oldestDate to getOldest(allDates)
		set fileName to (DDMMYYYY("-", oldestDate) & "**" & HHMM(".", oldestDate) & " DIL") as string
		
		tell application "Finder" to set fileToRead to (item fileName of folder ".desktopIconLocations" of home) as alias
		set myData to (read fileToRead as list)
		
		loadDesktopLocations(myData)
	end if
end run

on idle
	tell application "Finder" to if not (exists (folder ".desktopIconLocations" of home)) then make new folder at home with properties {name:".desktopIconLocations"}
	set newFileName to (DDMMYYYY("-", (current date)) & "**" & HHMM(".", (current date)) & " DIL") as string
	tell application "Finder" to set dilFolder to (folder ".desktopIconLocations" of home) as alias
	tell application "Finder" to if not (exists (item newFileName of dilFolder)) then make new file at dilFolder with properties {name:newFileName}
	set newFile to (result as alias)
	
	set myData to getDesktopLocations()
	
	set OA to open for access newFile with write permission
	try
		write myData to OA starting at 0
		close access OA
	on error
		try
			close access OA
		end try
	end try
	
	return 65
end idle

on quit
	tell application "Finder" to if not (exists (folder ".desktopIconLocations" of home)) then make new folder at home with properties {name:".desktopIconLocations"}
	set newFileName to (DDMMYYYY("-", (current date)) & "**" & HHMM(".", (current date)) & " DIL") as string
	tell application "Finder" to set dilFolder to (folder ".desktopIconLocations" of home) as alias
	tell application "Finder" to if not (exists (item newFileName of dilFolder)) then make new file at dilFolder with properties {name:newFileName}
	set newFile to (result as alias)
	
	set myData to getDesktopLocations()
	
	set OA to open for access newFile with write permission
	try
		write myData to OA starting at 0
		close access OA
	on error
		try
			close access OA
		end try
	end try
end quit

on HHMM(seperator, aDate)
	tell (aDate)
		set myHours to (hours of it)
		set myMins to (minutes of it)
	end tell
	set myHours to oneToTwoDigits(myHours)
	set myMins to oneToTwoDigits(myMins)
	
	return (myHours & seperator & myMins) as string
end HHMM

on DDMMYYYY(seperator)
	set myDate to {}
	tell (aDate)
		set myDays to (day of it)
		set myMonth to (month of it) as integer
		set myYear to (year of it)
	end tell
	set myDays to oneToTwoDigits(myDays)
	set myMonth to oneToTwoDigits(myMonth)
	
	set myDate to myDays & seperator & myMonth & seperator & myYear
	return myDate as string
end DDMMYYYY

on oneToTwoDigits(anInt)
	set anInt to anInt as string
	if (count every character of anInt) is 1 then set anInt to (0 & anInt) as string
	return anInt
end oneToTwoDigits

on getDesktopLocations()
	set newDesktopIconLocations to {}
	tell application "Finder"
		set allDesktopIcons to (every item of (path to desktop))
		
		repeat with i in allDesktopIcons
			set aliasString to (i as alias) as string
			set thePosition to desktop position of i
			set end of newDesktopIconLocations to {aliasString, (item 1 of thePosition), (item 2 of thePosition)}
		end repeat
	end tell
	
	return newDesktopLocations
end getDesktopLocations

on loadDesktopLocations(desktopIconLocations)
	repeat with i in desktopIconLocations
		tell application "Finder" to if exists (alias (item 1 of i)) then set desktop position of (alias (item 1 of i)) to {(item 2 of i), (item 3 of i)}
	end repeat
end loadDesktopLocations

on extractDate(aString)
	set tid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to "**"
	set dateString to text item 1 of aString
	set timeString to text item 2 of aString
	set AppleScript's text item delimiters to " DIL"
	set timeString to text item 1 of timeString
	set AppleScript's text item delimiters to tid
	
	set myDate to date (dateString & " " & timeString)
end extractDate

on getOldest(aList)
	if aList is {} then missing value
	if (count aList) is 1 then return item 1 of aList
	
	set myDate to item 1 of aList
	repeat with i from 2 to (count aList)
		if item i of aList > myDate then set myDate to (item i of aList) as date
	end repeat
	
	return myDate
end getOldest

hope it works,
ief2

Can someone help me… I searched it on google with “desktop position site:macscripter.net” and nothing helped. Here is my problem, I want to check if a space on the desktop is avialible. Look at this, it takes the location of the item and then checks it but it says that it’s empty. Look:

tell application "Finder" to set this_ to desktop position of (choose file default location (path to desktop as alias))
checkspace(this_)
on checkspace(var)
	set yes_ to missing value
	set no_ to missing value
	tell application "Finder" to set thelist to desktop position of every item of desktop
	repeat with i in thelist
		if i is var then
			set yes_ to true
		end if
	end repeat
	if yes_ is true then
		return true
	else
		return false
	end if
end checkspace

Hi,

a common mistake, you compare the variable var with a reference to a list item, not with the value.
To dereference the list, add contents of


.
if contents of i is var then
.

The whole script can be simplified

tell application "Finder" to set this_ to desktop position of (choose file default location path to desktop)
checkspace(this_)
on checkspace(var)
	tell application "Finder" to set thelist to desktop position of every item of desktop
	return {var} is in thelist
end checkspace

note: the result of path to is always an alias

Thanks. That’s something I could study to make an app!

I got this:

tell application "Finder" to set this to desktop position of (choose file default location path to desktop)
tell application "Finder"
	set _b to bounds of window of desktop
	set dimensions1 to item 3 of _b
	set dimensions2 to item 4 of _b
end tell
if this is not missing value then
	set this_ to {((item 1 of this) + 150), (item 2 of this)}
	set option to checkspace(this_)
	if not option then
		activate
		set thefile to (choose file default location path to desktop)
		if not dimensions1 < (item 1 of this_) then
			if not dimensions2 < (item 2 of this_) then
				tell application "Finder" to set desktop position of thefile to this_
			end if
		end if
	else
		activate
		set thefile to (choose file default location path to desktop)
		set this_ to {(item 1 of this), ((item 2 of this) + 175)}
		set option to checkspace(this_)
		if not option then
			set this_ to {((item 1 of this) - 150), (item 2 of this)}
		end if
		if not dimensions1 < (item 1 of this_) then
			if not dimensions2 < (item 2 of this_) then
				tell application "Finder" to set desktop position of thefile to this_
			end if
		end if
	end if
end if
on checkspace(var)
	tell application "Finder" to set thelist to desktop position of every item of desktop
	return {var} is in thelist
end checkspace

Hi ief2,

I thank you for all the time you spent on this script … but it does not work. It looks very complicated to do a so simple task. I didn’t see any other way to do that in MacScripter, but there should be.

Thanks for your help.

Robert