System Event - Can Never Figure Them Out?

I always have problems with system events, and I can never figure out the logic behind what targets what. Seems like people use various ways, i.e., one-liners or separated as mine below. Does anybody know of a good resource that defines how to target menu items that are various levels deep within an application?

In my newest issue. I am simply trying to “Clear Guides” in Illustrator CS. It’s only 2 levels deep and I still can’t make it work?

Menu bar item = View
Menu item of Menu bar item = Guides
Menu item of Guides = Clear Guides

I am trying to use this script with no luck:

tell application "System Events"
	tell process "Illustrator CS"
		tell menu bar 1
			tell menu bar item "View"
				tell menu "Guides"
					click menu item "Clear Guides"
				end tell
			end tell
		end tell
	end tell
end tell

Hi,

first of all: the targeted application must be frontmost.
Every menu item is inherited by a menu, so the hierarchy is always menu - menu item - menu - menu item


activate "Illustrator CS"
tell application "System Events"
	tell process "Illustrator CS"
		tell menu bar 1
			tell menu bar item "View"
				tell menu 1
					tell menu item "Guides"
						tell menu 1
							click menu item "Clear Guides"
						end tell
					end tell
				end tell
			end tell
		end tell
	end tell
end tell

I tend to do as Stefank has has shown above but you will find that the element nesting of the GUI goes well beyond how far you can nest tell statements in AppleScript (forgot how deep I got these before reaching an error) then I had to resort to stringed objects within a tell statement.

Just tried again and can only get 15 deep don’t know if that is right.

Thanks once again Stefan

Yes, setting frontmost to true might have been my major flaw during my laborious trial and error process.

Needless to say, your example works like a charm.

Your logic is what I was really after, and it was that simplicity definition I needed.

-Jeff

Hi Jeff,

You don’t really need System Events for this task.

tell application "Illustrator CS" to delete (documents's page items whose its guides is 1)

Marc,
This is great! And what’s so nice about this is that it doesn’t force the script to clear any active selection. Thank you very much for sharing your knowledge. btw, I had to change it a little bit to make it work.

tell application "Illustrator CS" to delete (document 1's path items whose its guides is true)