Contextual Menu item method swizzling - macos

Any idea how do I handle contextual menu click inside finder via method swizzling ? I am trying to find which method needs to be overridden from Finder.h for this?

I found this sample which shows how to add the items using method swizzling and code injection.
https://github.com/infinitelabs/FinderMenu
Adds submenu to the context menu of Mac OS X Finder. Submenu has two items, clicking on them the window with the selected items is displayed.
It uses https://github.com/rentzsch/mach_inject to update the Finder process. Seems like it should do what you want.

Use gdb and set breakpoints on objective-c messages. You can also use instrument tool for finding method names.

Related

Cocoa menubar programmatically with NSMenu, how to get standard items "Services", "Hide App", "Hide Others", "Show All" etc

In decarbonizing I have now come to creating a menubar programmatically using NSMenu.
Carbon seems to be so nice to add standard items to the application menu: Services, Hide app, Hide Others, Show All, Quit app,
and they are even added using the user’s Preferred Language setting in System Preferences’s Language & Region,
so in German Hide Others is added as Andere ausblenden.
I only had to add the About and Preferences items.
However, it seems that in Cocoa I have to add these standard items myself,
but how can I find out what Hide Others is called in the user’s Preferred Language ?
And what is the Cocoa equivalent of CreateStandardWindowMenu ?
If you have Xcode create a new Cocoa application project, it will make a MainMenu.xib that you can examine. It looks like "Hide Others", "Quit" etc. are just menu items with particular actions assigned, but "Services" seems to have some special magic. Even if you don't want to present a whole menu bar from a nib, you could load it from a nib and then pick out certain menu items and move them to another menu, rename them or whatever. Though I'm not sure I understand what you're trying to accomplish. Even if you don't want to localize, you can use a nib.
To address the last part of your revised question, the NSApplication object has a windowsMenu property, i.e., you could create a menu and say [NSApp setWindowsMenu: myMenu]. But I don't know if that adds items like Bring All to Front or if you must create those yourself.

Cannot connect Objects to actions - no drag'n'drop option?

I have my MainMenu.xib with the Main Menu in it. I also have several NSObject subclasses as Menu Controllers, which are all represented in IB as objects, so that each Menu Item in the Main Menu corresponds to a dedicated Menu Controller
Whenever I try to drag'n'drop some menu item to its Menu Controller header and create an IBAction , Xcode does not give me that option. I suppose it's an Xcode bug, but I'm still confused...
When I created my first Menu Controller everything worked fine. However, now that I've added all Menu Controllers (the very same way as before), only the first one can be "linked" via IB.
What am I doing wrong? Any ideas on how to fix that?
Strange, Make sure the following
you have used the word "IBOutlet" for each to be linkable
clean the code
restart xcode
And just to make it clear,if you think its a bug in your XCode copy, try creating a sample project and see if it also do the strange behavior

Programmatically adding "Open Recent' menu to context popup menu

I have a non-document Cocoa application with a menubar icon and status menu. I've added an "Open Recent" menu to the status menu in Interface Builder. Populating the menu works just fine:
[[NSDocumentController sharedDocumentController] noteNewRecentDocumentURL:
[NSURL fileURLWithPath:filename]]
Now I would also like to add a second "Open Recent" menu to a context popup menu. How would I create the menu programmatically so that it gets populated with entries automatically as it does for the version in the status menu?
I tried creating a copy of the one in the status menu, but it does not get populated. I assume that NSDocumentController is not aware of the menu (frankly, I don't know how it knows about the one in the status menu).
For reference, the best documentation on the inner workings of the Open Recent menu that I found is this:
http://lapcatsoftware.com/blog/2007/07/10/working-without-a-nib-part-5-open-recent-menu/
Unfortunately, it doesn’t help much with this, because even if you create the menu like this, it will be ignored by NSDocumentController. The menu must exist in the main menu before applicationDidFinishLaunching: call, otherwise it won’t be picked up — and consequently, duplicate ones are ignored too.
What I ended up doing, and what seems to work so far, is this:
The first idea was to pick the corresponding NSMenu from the main menu and attach it into other menus as well, hoping that reference counting will make this work. No such luck, setSubmenu throws if the submenu is already in another NSMenuItem.
So I “reparent” the submenu instead — when I need to show it in another menu, I remove it from the main menu’s Open Recent item and set it as submenu in the new menu. Later, I move it back. It’s an ugly hack, of course, but it gets the job done.

Mac mainmenu - help implementing a mainmenu

I have created my mainmenu in one of my window xibs.
My application has multiple windows and the mainmenu should be the same for all of them.
But, for each window some of the items will be disabled and others enabled.
The main problem that I see is the selectors.
Since every window controller class (mm) implements some of the functions how can I use the interface builder to set the selectors across multiple controllers.
I guess I need some sort of a "MainMenu Manager" class that will know all of the window controllers that "wants" to get called from the main menu and also the manager will have the code for enabling/disabling items when switching between windows.
Does that make sense? am I missing something here?
If not - can you provide any help on implementing this manager in terms of application flow?
(It should probably init the main menu before I load my first application window)
Thanks,
Make the various targets and selectors properties of the window controllers, make the menubar in MainMenu.xib. Then bind the menu items' target and selector properties to "Application"mainWindow.windowController.property". You can bind other properties such as the title too.

Does Xcode have code navigation feature?

Does Xcode have a code navigation feature? Like unix's cscope type of tool which I can enter the name of a class and it will open the file for me? Or find out the caller of a particular method of a class? Or show the type hierarchy of a class?
command-shift-D (Xcode 4: command-shift-O) opens a Quick Open window where you can type a partial class name and hit enter to open the file. As far as more complex versions such as what Eclipse offers (method names, symbol names, etc), I don't believe anything like that is built into XCode.
In addition to the Quick Open window, there is the class browser. The class browser allows you to view the class hierarchy. The Class Browser can be found under the Project menu.
The "Jump To Definition" item is accessible through CMD + double click, which takes you directly to the relevant code
Also, you can "Jump To Documentation" by (Option or ALT) + double clicking on a class/method/property
Update:
Option + double click no longer takes you directly to documentation in Snow Leopard. Now it opens up a little documentation "bubble", with the option to jump to the documentation browser if needed.
If you Ctrl click a class name, method name or function name you will get a long meny where you can find the Jump to Definition item.
From XCode 4.4 upwards, you can find callers and callees by opening the "show related items" pop-up for a selected (highlighted) method:
Mark method in code (using your cursor or double click name)
Press Ctrl+1
Select "Callers" from the pop-up menu
You can also go View->Standard Editor->show related items or press the tiny button just left of the arrow buttons in the line just above the editor window (where it shows your currently selected file and method).
Also: select an identifier, right-click on it, and choose Find in Project > As Symbol (or Find in Project and choose Symbol as the search type) shows all symbolic uses, that is, declarations, definitions, and call sites.
There should opens up something like an element preview window or bubble when the user point to a method or item. The "Jump to Definition" change the users focus and take 2 steps to go to the target.
The relation window and context window in Source Insight is a good example,but I can't find them in Xcode and other dev tools for the mac.
By the way,the context window should have multiple layers to trace a deep referenced method like the split window in Xcode, not only one layer in the source insight.

Resources