10 Replies Latest reply: Mar 30, 2006 2:01 PM by Andrew Wilson
Joey Delli Gatti Level 1 Level 1 (65 points)
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)
  • ByTheLightOfQuiveringAspens Level 1 Level 1 (15 points)
    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....
  • Tony D Level 1 Level 1 (55 points)
    I'm curious, why not do it in Automator?

    You can use "Find Finder Items" and "Move Finder Items" to clean files off your desktop.

    You can save it as an application and make it a startup item so it runs every time you reboot or logon.


    Dual G5   Mac OS X (10.2.x)  
  • Joey Delli Gatti Level 1 Level 1 (65 points)
    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.
  • Cyclosaurus Level 6 Level 6 (12,915 points)
    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.
  • HD Level 4 Level 4 (3,510 points)
    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
  • Andrew Wilson Level 2 Level 2 (285 points)
    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
  • ByTheLightOfQuiveringAspens Level 1 Level 1 (15 points)
    Hi Joey,

    I found this thread:

    http://bbs.applescript.net/viewtopic.php?id=16609


    at MacScripter, it's title is "Desktop Cleanup", and there is one response. Check it out.


    'hope this helps.


    Sincerely,


    ByTheLight....
  • Andrew Wilson Level 2 Level 2 (285 points)
    A bit more work and it now seems to work:

    tell application "System Events"
    tell application "Finder" to activate desktop
    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 front menu bar
    end tell
    end tell

    Andrew
  • Joey Delli Gatti Level 1 Level 1 (65 points)
    I guess I was just looking for a way to do it using the "clean up" verb. Thanks for all the input. It is nice to have some way to do it.
  • Andrew Wilson Level 2 Level 2 (285 points)
    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