Is it possible to run a command from the menus with a python script?
For example: i’d like to run some of the available filters within the macro window.
Is it possible to run a command from the menus with a python script?
For example: i’d like to run some of the available filters within the macro window.
You would simply load the filter class and execute one of its methods. Which filter do you want to run?
I want to be able to run any menu item. This was just an example.
Again: you do not run menu items in code. Au contraire, the menu items trigger code. Or to be precise, methods, and you would access those methods in code. In other words: Menu items are not an end in themselves, they trigger an action. And it is that action you want to code, not the triggering of GUI elements.
If you tell me what you want to do, I can help you find the right method.
Pardon my bad word choice.
So is it possible to access all those methods that are available in the menus?
There’s an easy way to do this with AppleScript:
tell application "System Events"
tell process "Glyphs"
click menu item "About Glyphs" of menu "Glyphs" of menu bar 1
end tell
end tell
This way i can easily access all menus and their names.
But i don’t want to use AppleScript.
You can run any method. Including those that are triggered by GUI items.
How can i find those then? Where do i look? Can’t see it in the docs.
First you need to get to the menu item. There is some sample code in the plugins SDK. Each item has a ‘action’ and a ‘target’. You can run the action on the target. But a lot of times, the target is not set. That means it will be called on the responder chain (read about that in the Apple docs if needed).
But it might be easier to give me a list of what you like to do and I tell you how to get there.
I see i can get to the menus like this:
for menu in Glyphs.menu:
print menu.submenu()
But i’m not sure what to do with it.
I wanted to write a plugin that would work more or less like the Help Search under cmd+shift+?
and add more functionality on top. That’s why i thought of accessing all of the visible menus. I thought it would be easy to just pass that to another interface.
Think of Spotlight, Alfred, or if you use Evernote they also have a nice “spotlight” alike feature under cmd+j
.
try this:
for menu in Glyphs.menu:
print menu
for item in menu.submenu().itemArray():
print "Item", item
I can change the title, hide, add tooltip; there are bunch of setters for that and it’s easy.
I can get to targets and actions with:
for item in Glyphs.menu[GLYPH_MENU].submenu().itemArray():
print "Title:", item.title()
print "Action:", item.action()
print "Target:", item.target()
print
I even changed the action for the menu item with: item.initWithTitle_action_keyEquivalent_
But i didn’t manage to call the action. Is it possible to call those actions?
This looked promising but i don’t know how to use it:
https://developer.apple.com/documentation/appkit/nsmenu/1518210-performactionforitem
Don’t do that. If, for whatever reason do item.setAction_()
Glyphs.menu[GLYPH_MENU].submenu().performActionForItemAtIndex_(index)
If you do this kind of hacking, you should have a proper understanding of the pyObjc bridge: https://pythonhosted.org/pyobjc/