Desktop cleanup

I have a problem with desktop clean up. Put simply, nothing works. I have the following snippet which does half the job.
After a bit of script which checks if the the enable assistive devices box in system prefs is checked, the first half of the script appears to do nothing until you click on the desktop and then on menu bar 1: Then it all cleans up nicely. The second part of the script effectively brings the desktop window to the fore, but I still have to manually click on any blank prt of the menu bar. I have tried all sorts of ways to script this, but nothing works. Any suggestions?

tell application "System Events"
	set UI_enabled to UI elements enabled
end tell
if UI_enabled is false then
	tell application "System Preferences"
		activate
		set current pane to ¬
			pane "com.apple.preference.universalaccess"
		set the dialog_message to "This script utilizes " & ¬
			"the built-in Graphic User Interface Scripting " & ¬
			"architecture of Mac OS X " & ¬
			"which is currently disabled." & return & return & ¬
			"You can activate GUI Scripting by selecting the " & ¬
			"checkbox "Enable access for assistive devices" " & ¬
			"in the Universal Access preference pane."
		display dialog dialog_message buttons {"Cancel"} ¬
			default button 1 with icon 1
	end tell
end if
end
tell application "System Events"
	activate
	set frontmost to true
	
	click menu item "kind" of menu "Arrange By" of menu item "Arrange By" of menu "View" of menu bar item "View" of menu bar 1 of application process "Finder" of application "System Events"
end tell
tell application "Finder"
	activate
	set visible of every process whose visible is true and name is not "Finder" to false
	delay 2
end tell

I think you want to be telling the Finder to do the cleanup rather than System Events

Do you mean simply substitute "finder for “System events” That certainly doesnt work .:slight_smile: I cant make the simple clean up command work at all. I am at my wits end…

DK

Finder is certainly the better choice for this job when working with Finder windows, dorian. However, the implementation for the desktop window itself seems to be a bit buggy - so System Events may be the way forward in this particular case. Perhaps something like this:

tell application "System Events"
	if not UI elements enabled then
		tell me to display dialog "This script requires GUI scripting to be enabled. For this, you may need to enter your user password."
		set UI elements enabled to true
		if not UI elements enabled then error number -128
	end if
	set visible of every process whose visible is true and name is not "Finder" to false
	tell process "Finder"
		perform action "AXRaise" of scroll area " "
		click menu item "Kind" 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