Using Menu Items

Hi there

I am trying to use a menu item in a from an apple script. I found this bit of code as an example


on do_menu(app_name, menu_name, menu_item)
	try
		-- bring the target application to the front
		tell application app_name
			activate
		end tell
		tell application "System Events"
			tell process app_name
				tell menu bar 1
					tell menu bar item menu_name
						tell menu menu_name
							click menu item menu_item
						end tell
					end tell
				end tell
			end tell
		end tell
		return true
	on error error_message
		return false
	end try
end do_menu

Problem I have is that menu_item has a space in the name. Any one know how I wrap the name I have tried “menu item” which applescript excepts, but I don’t think it understands it as the menu item.

Any suggestions?

Thanks

Hi,

space or other special characters don’t matter at all.
Don’t specify menu_name by name, just write


.
 tell menu bar item menu_name
                       tell menu 1
                           click menu item menu_item
.

Thanks for that.

Could’nt quite figure it out, but ended up with this as an exampe which seems to work.

Not sure if there is an easier way.


tell application "TextEdit"
	activate
end tell
tell application "System Events"
	tell process "TextEdit"
		click menu item "New" of menu 1 of menu bar item "File" of menu bar 1
	end tell
end tell

For your example there is indeed an easier way, TextEdit is scriptable


tell application "TextEdit" to make new document

Thanks for that.

I was just using text edit as an example before jumping into what I needed. What I am trying to do is to click a menu item in vector works.

which is my next question.

the item I want to click is a sub menu so I have

Menu bar as File then the menu is Export and the sub menu is Export PDF…

I was trying


click menu item "Export PDF..." of menu item "Export" of menu 1 of menu bar item "File" of menu bar 1

But that does not seem to work I get the error:

System Events got an error: Can’t get menu item “Export PDF…” of menu item “Export” of menu 1 of menu bar item “File” of menu bar 1 of process “Vectorworks 2010”.

After that it should bring up the dialog box with options to select, which I would hope to be able to automatically select the ones I wan’t with the apple script, but need to get the menu to click the “Export PDF…” option first.

could it be that the three periods in Export PDF. are one character (horizontal ellipsis → ⌥.) not three periods?

That is very possible I remember vaguely reading something about this in the past. Although I have tried to change this but it still does not work. I am assuming it is something to do with the menu name and not the script.

Is there any way to wild card this something like “Export PDF*” ?

Yes, something like

1st menu item of . of . of . of whose title starts with "Export PDF"

but it’s better to use an UI inspector like UIElementInspector to gather the proper references

Well managed to sort it out. Thank you for your help.

I used the following in the end which works.


tell application "Vectorworks 2010"
	activate
end tell
tell application "System Events"
	tell process "Vectorworks 2010"
		click menu item "Export PDF..." of menu 1 of menu item "Export" of menu 1 of menu bar item "File" of menu bar 1
	end tell
end tell