VERY n00bish: Activating a menu in Finder?

Finder/View/Arrange By/Name

How do I click this in a script? I don’t need any context, just that single line of code, thanks in advance guys.

this does the job

tell application "Finder" to activate
tell application "System Events"
	tell process "Finder"
		tell menu item 1 of menu 1 of menu item 7 of menu 1 of menu bar item 5 of menu bar 1 to click
	end tell
end tell

Haha, stupid simple.

Thanks, man.

Also,

tell application "Finder"
	if current view of front Finder window is icon view then set arrangement of icon view options of front Finder window to arranged by name
end tell

I’ve been struggling with how to accomplish the View/Arrange By/Name for several days and had about given up. Both of the methods work but give slightly different results:

The second method is certainly a lot easier but I ran into two problems:
1. for some reason you can’t substitute “date modified” or “date created” for “name”
2. more important for me, when you run the script the folder is re-arranged but when you click on
“Show View Options” the “Keep arranged by” is checked and if you un-check this box the folder is immediately un-arranged.

The first method appears to work OK, but it’s not obvious why. What does item 7 refer to? Would somebody tell me what each of the items in that line refer to in English (e.g., is item 5 a reference to “Arrange By”?).

Thanks

Check yourself:

tell application "Finder" to activate
tell application "System Events"
	tell process "Finder"
		name of menu item 7 of menu 1 of menu bar item 5 of menu bar 1
	end tell
end tell

The first method is called “GUI Scripting” (Graphical User Interface). It’s a method of having System Events do to an application interface what you might have done with your cursor to the window or menu bar. The mystery is that it’s not always obvious how to get there so there are tools to help.