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
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.
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)