Is it possible to inspect the main application menu of some already running application and execute commands from this menu? For example retrieve the whole main menu hierarchy of Safari and execute the File → New Tab command (by its name, not using the keyboard shortcut). I’m interested in an Objective-C solution.
One way to do this is using the Cocoa Accessibility API
For example,
NSAccessibilityPickAction Selects the
object, such as a menu item
You could use Applescript This is not exactly calling the menus but for a well written program provides the same functionality and using a defined and discoverable API.
To send the events see Scripting Bridge
Related
This question is mostly just out of curiosity at what's possible. I'm interested to know any ideas at all.
If I'm on a website, is it possible to make a keyboard shortcut for clicking a button on that site? For example, if I'm on google.com, is it possible to write a script somewhere on my own system that will detect that I'm on google.com and make it so that if I press ctrl-g while that site is in focus, it will click Google Search?
If you know for sure that this isn't possible and have an explanation for why, I'd be interested to know that too.
Yes, it's possible.
I'll briefly describe how to do it in C# and Chrome, but it's not language-dependent and could be used in Edge or IE either.
Get the currently focused window by calling GetForegroundWindow API.
Get the process id by calling the GetWindowThreadProcessId API.
Get the process name by process id.
Proceed if it's Chrome.
Get access to UI Automation COM interface.
You will need to consume the COM interface in your language, but in C# it's quite easy: you'll need to add a reference to COM -> UIAutomationClient to your project. This will generate a wrapper for you, so the COM interface will be accessible in the code in a usual OO-style.
In Chrome, the Accessibility options must be enabled first to get access to UIA.
It could be done manually by navigating to chrome://accessibility/, but it's possible to do it programmatically as well.
Use UI Automation to detect the active URL.
You will need to find the Address bar in the UI tree using UIA by its Name or ClassName or another unique identifier.
You could first retrieve the UI Automation element for the Chrome window and then use the FindAll method to find the Address bar in the subtree to get its Value property which should contain the current URL.
Inspect tool will help you to investigate the Chrome UIA element tree and element properties (like Name or ClassName).
Do the same steps to find the Google Search button in the UI tree using UIA.
Call GetClickablePoint method to get its clickable point.
Click on the button programmatically using Win API.
To do this on a global shortcut, you could place a keyboard hook by calling SetWindowsHookEx API. Or use a library.
Here is another way.
That's it, easier than it looks.
How to execute a menu item (or, to obtain the menu items list) of a specific (external) application in Mac OS, like the 'Application Shortcuts' (in 'System Preferences -> Keyboard -> Keyboard Shortcuts') does?
I mean making an external application do a job by pointing to one of its menu item, just like what the 'Application Shortcuts'. I'm planning to write a program that can accomplish this job. Can anyone help me please?
In Xcode you can set shortcuts in your NSMenu in MainMenu.xib.
Just add a new NSMenuItem, set the Key Equivalent in the Attributes Inspector and then finally use Target-Action to connect it.
Sorry just saw that you wrote external application.
The application has to support this.
Most apple apps have support for AppleScript.
You can use this here to set up global hotkeys:
https://github.com/davedelong/DDHotKey
I'm trying to build an app that get's selected text from other applications if a certain shortcut/hotkey is pressed. First I tried the Carbon Hotkey API, but then I couldn't get selected text from other applications. Second I've tried building a service that gets the selected text from other applications and is called with a global shortcut, but I wasn't able to use a custom shortcut. Somehow only CMD+SHIFT is allowed as a modifier in the plist and these kind of shortcut is already used in many applications so it's often not working. CTRL and a key would be a cool shortcut, because not many applications use this kind of shortcut, but I can't specify such a modifier.
Any Ideas how to solve this problem?
To answer your second question, you can manually edit the Services plist file. Find details here:
Set Custom KeyEquivalent in Services Menu
Is there a way for one Cocoa application to attach drawer-like windows to another application? We might for example want a terminal drawer that followed around a particular Finder window.
There is a program called DTerm that opens little transparent windows over Finder windows, but one might prefer persistence.
You may want to checkout SIMBL. It allows you to write nifty bundles that are loaded into the application your targeting. If you go along with it I'd reccomend using class dump to gather more information on the application your working with (although Im not sure it would work with Finder)
Can any one tell me about how to lanch a self-made application using the "openwith" feature of a mouse click event?
Your question is not very clear, but I understand it as "How do I tell the Finder that my application knows handle certain file types?". Once the Finder knows the link, it will suggest your application in the Open With submenu that appears when you ctrl/right-click on a file.
Have a look at the Apple documentation. Basically, you have to declare all the supported file types in Info.plist (or what you called it). The system is based on Universal Type Identifiers.