Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

How to use AppleScript to clean up desktop?

I am writing a script that will take all the image documents on my desktop and place them into a newly created folder on the desktop called "desktop images". It'll take all the .dmgs and put them in another folder, etc. Then the remaining icons should arrange themselves by name. Disks should be ejected and caches dumped. Then the trash should be emptied. After all this, a dialog displays "Mission Accomplished. Would you like to Shut down, Restart, or Continue."

I have nearly everything I need put together except for the blasted desktop clean up. Here's how I have it right now:

tell application "Finder"
clean up window of desktop by name
end tell

{Finder got an error: Can't get window of desktop.}


tell application "Finder"
activate
clean up window 1 by name
end tell
--this one works on opened windows, but not on the desktop.

I have also tried other various methods to no avail. Help me please.

PowerBook G4 1.67 Ghz, Mac OS X (10.4.2)

Posted on Mar 23, 2006 10:37 PM

Reply
10 replies

Mar 24, 2006 2:43 AM in response to Joey Delli Gatti

Hi Joey,


I have two questions:

1.) How, specifically, do you want to "clean up" the desktop?

and

2.) Do you know that Scipt Editor in reference to the Finder is recordable, and that you could set all of the icons on the desktop exactly where you want them (if this is what you mean by cleaning up) while Script Editor is watching & recording your actions so that you'll have some code already written for you when your finished arranging?


Also, I would recommend an Applescripting book like "Applescript: The Missing Manual" by Adam Goldstein. I seem to recall that the author touches on this very subject.




'hope this helps.


Sincerely,


ByTheLight....

Mar 24, 2006 10:11 AM in response to Tony D

Thanks for the input Tony D and ByTheLightOfQuiveringAspens.

'm curious, why not do it in Automator?
I guess the biggest reason is because I'm learning AppleScript. I want to know how to do everything in AppleScript. Then, I also plan on making an AppleScript App using X Code stuff that implements all of my cool scripts I put together.

Some of the ones I have so far are a finder refresher, a show hidden files, an app unfreeze, bring all windows to left of monitor 1, and a really huge one that uses gui scripting to help people set up their ip printers at my college.

Now to the point at hand
2.) Do you know that Scipt Editor in reference to the Finder is recordable, and that you could set all of the icons...
I tried to use the record button but with no luck. Under OS 10.5, it just records every movement of the icons as "select window of desktop". And when I move the icons, it doesn't add to the script at all.

1.) How, specifically, do you want to "clean up" the desktop?
I tried selecting "Arrange By>Name" but it still doesn't record the action. The AS dictionary for the Finder says that it's possible; however, I can only get it to work in opened windows on the desktop using this script:

tell application "Finder"
activate
clean up window 1 by name
end tell

If there is no wat to do it with a traditional AS, if a shell script existed that could do it, I'd be happy to know it.

Mar 24, 2006 11:45 AM in response to Joey Delli Gatti

This is what Finder dictionary defined:
clean up v : Arrange items in window nicely (only applies to open windows in icon view that are not kept arranged)

So you method of 'clean up window of desktop by name' will not work.

The closest thing you can do in this case is:

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

So you have to set desktop position of each and every item of desktop folder.

Mar 24, 2006 11:54 AM in response to Joey Delli Gatti

Hi Joey,

Unfortunately, the desktop isn't scriptable in the same way as other folders - the only way you'd get it to work would be to go back to OS7, 8 or 9 😟 Then you could record setting the positions of the desktop icons using screen co-ordinates.

Try this: close every Finder window and then run the following:

tell application "Finder" to get every window

This returns {}, an empty list. For AppleScript purposes, the desktop isn't even a window!

You could do it with a UI script, though:

tell application "Finder" to activate
tell application "System Events"
tell process "Finder"
click menu item "Name" of menu "Arrange By" of menu item "Arrange By" of menu "View" of menu bar item "View" of menu bar 1
end tell
end tell

And yes, that is all one line...

Hope it helps,

H

Mar 24, 2006 2:51 PM in response to Joey Delli Gatti

Joey,

I can get you part of the way there, but hitting that last bit evades me so far.

tell application "Finder"
activate desktop
tell application "System Events" to tell menu bar item "View" of the front menu bar of process "Finder"
click
try
get the title of every menu item
--gets an empty set
end try
end tell
end tell

This gets you the drop down menu you need, but the items in it don't seem reachable. If you view the event log, you will see that you get {} - an empty set.

Andrew

Mar 30, 2006 2:01 PM in response to Joey Delli Gatti

To use the Clean Up menu, replace the main line with

click menu item "Clean Up" of menu "View" of menu bar item "View" of front menu bar

However, if anything on the desktop is selected, the menu item will become 'Clean Up Selection", so the script would fail.
You could use the numeric value:

click menu item 4 of menu "View" of menu bar item "View" of front menu bar

but then if anything is selected, you would only clean that up, not the remaining items.

Andrew

How to use AppleScript to clean up desktop?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.