Get the position of icons on the desktop
How can I get the positio开发者_Python百科n of an icon on the desktop in OS X? Or, how can I get a list of all icons on the desktop and their positions?
Try this AppleScript code
tell application "Finder"
get desktop position of every item of desktop
end tell
The solution for a folder is to use a catalog iterator to get the Finder info for every item in the folder. The Finder info is a FileInfo or FolderInfo structure that contains a QuickDraw Point
(integral co-ordinates, origin top-left, positive y down) specifying where the icon will appear.
I don't know how you would do this for the Desktop, though. There is a Desktop folder in the Home folder, but examining its contents will probably get you the positions that the Finder will use to display that folder in a window. It also will probably not include anything “on the Desktop” that isn't in the Desktop folder, such as mounted volumes.
following apple script will get those positions (use either scripting bridge or NSAppleScript)
tell application "Finder"
tell every item of desktop
get position
get name
end tell
end tell
Another solution (Very similar to the one of @Vaskravchuk)
tell application "Finder"
repeat with anItem in desktop
get {desktop position, name} of anItem
end repeat
end tell
精彩评论