I am Developing the zip extractor app in cocoa for which i'm using findersync to show context menu item. But, the problem is item is showing for every file i want to show only for .zip files so how do i do that .
Any Suggestion.
Thanks in Advance!
Consider adding a service instead. That will allow you to add your item for any file not just those in monitored folders. The plist entries for a service allow you to directly specify what file types are acceptable, i.e. Restrict the service to ZIP files
Try this
NSURL *selectedURL = FIFinderSyncController.defaultController.selectedItemURLs[0];
NSURL *fileURL = selectedURL.filePathURL;
if([fileURL.pathExtension isEqualToString:#"zip"]) {
NSMenu *menu = [[NSMenu alloc] initWithTitle:#""];
NSMenuItem *item = [menu addItemWithTitle:#"Hello" action:#selector(itemTarget:) keyEquivalent:#""];
item.target = self;
return menu;
}
Related
I'm working on a cross platform app that doesn't use NIB files and trying to figure out how to create the standard OS X "Services" menu (a submenu of the application menu in most applications).
Looking at the nib file for standard Cocoa app, the services menu is defined like this:
<menuItem title="Services" id="NMo-om-nkz">
<modifierMask key="keyEquivalentModifierMask"/>
<menu key="submenu" title="Services" systemMenu="services" id="hz9-B4-Xy5"/>
</menuItem>
Obviously the bit that makes it work is systemMenu="services" but I can't see how to programmitically create a NSMenu item like this - there's no "systemMenu" property on NSMenu.
What magic is happening here?
You find the Services menu on NSApplication.
-[NSApplication servicesMenu]
See documentation.
For future reference, based on #catlan's answer here's some code...
// Create the services menu
NSApp.servicesMenu = [[NSMenu alloc] init];
// Create menu item for it
NSMenuItem* servicesItem = [[NSMenuItem alloc] init];
servicesItem.title = #"Services";
servicesItem.submenu = NSApp.servicesMenu;
// Add it to the app menu
NSMenu* appMenu = [[NSApp mainMenu] itemAtIndex:0].submenu;
[appMenu addItem:servicesItem];
I'm having a problem with cocoa, when I run the app the tabs get added as expected, but all the web views take the same string from the url field.
Basically, if I go to google on one tab, it goes to google on all of them.
Is there any way to make only the web view on the selected tab respond, and not on the others?
Here is the code:
- (IBAction)newTab:(id)sender {
NSTabViewItem *item = [NSTabViewItem new];
[item setView:_webView];
[item setLabel:#"New Tab"];
[_tabView addTabViewItem:item];
}
It looks like you're creating a new tab and then moving your WebView to it. To create a new web view for each tab, you have some options but one is to use NSViewController:
If you create the web view in the same xib as the tab view, move it to a separate xib. Change the class of the owner of that xib to NSViewController.
When adding a new tab in your code, load the xib (assuming it is named WebView.xib):
- (IBAction) newTab: (id) sender
{
NSTabViewItem *item = [[NSTabViewItem alloc] init];
NSViewController *viewController =
[[NSViewController alloc] initWithNibName: #"WebView" bundle: nil];
WebView *webView = [viewController view];
[item setView: webView];
[_tabView addTabViewItem: item];
[_webViewControllers addObject: viewController]; // Store the view controller, remove when the user closes the tab.
}
Here's a tutorial on view controllers: http://comelearncocoawithme.blogspot.fi/2011/07/nsviewcontrollers.html
I'm trying to add a button in my Preferences Bundle to open an URL in Safari (or in the Pref Bundle).
I'm looking this: https://github.com/hbang/NotiQuiet/blob/master/prefs/ADNQListController.m
But I don't understand the else, if eccc...
I want just a button (for example "My website") that open www.mywebsite.com
Thanks all!
Add the following dictionary to your preferences plist:
{
action = link;
cell = PSButtonCell;
label = "Google";
}
And the following method to your bundle:
- (void)link {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"https://www.google.com"]];
}
I have a NSPopUpButton configured with bindings and coredata. Everything is working perfectly, however I would like to add a item that implements an action to "edit the list", like
Item 1
Item 2
Item 3
Item 4
------
Edit List..
Is this Possible to do with Bindings?
I think that the answer is NO, at least not completely. I thought I would provide the content to the button programatically and maintain bindings for the Selected Value , so this is what I came up with
- (void)updateSectorPopupItems
{
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:#"Sector"];
NSSortDescriptor *sortPosition = [[NSSortDescriptor alloc] initWithKey:#"position" ascending:YES];
[request setSortDescriptors:#[sortPosition]];
NSError *anyError = nil;
NSArray *fetchObjects = [_gdcManagedObjectContext executeFetchRequest:request
error:&anyError];
if (fetchObjects == nil) {
DLog(#"Error:%#", [anyError localizedDescription]);
}
NSMutableArray *sectorNames = [NSMutableArray array];
for (NSManagedObject *sector in fetchObjects) {
[sectorNames addObject:[sector valueForKey:#"sectorCatagory"]];
}
[_sectorPopUpBotton addItemsWithTitles:sectorNames];
NSInteger items = [[_sectorPopUpBotton menu] numberOfItems];
if (![[_sectorPopUpBotton menu] itemWithTag:1] ) {
NSMenuItem *editList = [[NSMenuItem alloc] initWithTitle:#"Edit List..." action:#selector(showSectorWindow:) keyEquivalent:#""];
[editList setTarget:self];
[editList setTag:1];
[[_sectorPopUpBotton menu] insertItem:editList atIndex:items];
}
A couple of problems I'm having with this
1) When adding the Menu Item using
[_sectorPopUpBotton menu] insertItem:editList atIndex:items];
no matter what value is entered in atIndex, the item always appears at the top of the Menu list.
2) I just want the "Edit List..." menuitem to initiate the action, how do I prevent this from being selected as a value?
You might as well do that using an NSMenuDelegate method.
Actually in this way you can also keep the bindings for getting the NSPopUpButton content objects (in your case from the NSArrayController bound to the CoreData stack).
1) Set an object as delegate for the NSPopUpButton internal menu, you can do that in the Interface Builder by drilling down the NSPopUpButton to reveal its internal menu. Select it and then set its delegate in the Connections Inspector panel to the object you have designated to this task. As such delegate you might for example provide the same ViewController object which manages the view where the NSPopUpButton exists.
You'll then need to have the object provided as delegate adhere to the NSMenuDelegate informal protocol.
2) Implement the NSMenuDelegate method menuNeedsUpdate: there you'll add the NSmenuItem(s) (and eventually separators) you want to provide in addition to those already fetched by the NSPopButton's bindings.
An example code would be:
#pragma mark NSMenuDelegate
- (void)menuNeedsUpdate:(NSMenu *)menu {
if ([_thePopUpButton menu] == menu && ![[menu itemArray] containsObject:_editMenuItem]) {
[menu addItem:[NSMenuItem separatorItem]];
[menu addItem:_editMenuItem];
}
}
In this example the _editMenuItem is an NSMenuItem property provided by the object implementing this NSMenuDelegate method. Eventually it could be something as this:
_editMenuItem = [[NSMenuItem alloc] initWithTitle:#"Edit…" action:#selector(openEditPopUpMenuVC:) keyEquivalent:#""];
// Eventually also set the target for the action: where the selector is implemented.
_editMenuItem.target = self;
You'll then implement the method openEditPopUpMenuVC: to present to the user the view responsible for editing the content of the popUpButton (in your case the CoreData objects provided via bindings).
The only problem I haven't yet solved with this approach is that when getting back from the view where the edit happens, the NSPopUpButton will have the new item "Edit…" selected, rather than another "valid" one, which is very inconvenient.
I try to make a PDF document manager for my own OSX by Xcode. Now I can get the URL of the document. With the code below:
NSOpenPanel *documentOpenPannel = [NSOpenPanel openPanel];
NSInteger situationInt = [documentOpenPannel runModal];
if (situationInt == NSOKButton) {
NSURL *documentPath = [[documentOpenPannel URLs] lastObject];
}
From the documentPath, I can get some properties already, like file size:
NSString *fileSize;
[documentPath getResourceValue:&fileSize forKey:NSURLFileSizeKey error:nil];
Now I want to get more attributes of the documents, e.g. version number, total pages.
Could you give me some suggestion?
You need to use CGPDFDocument. CGPDFDocumentGetVersion(), CGPDFDocumentGetNumberOfPages() solve your specific problem.