scrod / nv

Notational Velocity: modeless, mouseless Mac OS X note-taking application
http://notational.net
GNU General Public License v3.0
2.07k stars 405 forks source link

Services Menu item unnecessarily present for ALL file types #200

Open Westacular opened 13 years ago

Westacular commented 13 years ago

With the current Info.plist, the "NV: New Note With Selection" item in the Services Menu shows up for all file types when using Finder (among other applications). If you try to use it on a non-text file (e.g. a movie file) NV will load/take focus and then silently fail to do anything.

Snow Leopard allows for much more fine-grained and contextual control over when a service item is and isn't relevant, but NV is using the old/deprecated services and pasteboard API. The presence of NSURLPboardType and NSFilenamesPboardType in the service item's NSSendTypes array (without any further contextual information) are basically telling the system that it will accept references to any sort of file.

The solution is to remove those two items from NSSendTypes, and add a NSSendFileTypes array to the service dict, listing the UTIs for filetypes it can accept -- in this case, the same ones listed among CFBundleDocumentTypes (e.g., com.apple.rtfd, com.adobe.pdf, public.text, etc... I'm not sure if there's any need to specify things like RTF and HTML that are subtypes of public.text?)

Or, better yet, split it into three separate services items, one for selected text content, one for importing from URLs, and one for importing files. This can be done purely within Info.plist, and doesn't require any change to the code -- all the services items can still call the same function.

Advantages to this approach:

  1. "New Note With Selection" sounds misleading when the selection is a file and NV will be trying to import it. A "NV: Import file content as new note" label for file-mode would be better.
  2. Users could turn the items on and off separately in System Preferences -- e.g., enabled for text and URLs but disabled for files
  3. Right now, NV's behaviour when calling the service with an URL selected seems somewhat unpredictable -- likely depending on whether or not it's handed the data in the form of an NSURL or an NSString, and the effectiveness of your URL-recognizing heuristic. Distinct items (plus a few small code changes) would solve this by letting the user explicitly tell you whether they want the URL itself or the data it references. (You can rely on Apple's contextual URL detection via the NSTextContent key.)
  4. "NSSendFileTypes" is new to Snow Leopard, and I'm unaware of any trick to simultaneously tell prior versions OS X "this service can handle files" that won't override NSSendFileTypes and be interpreted by Snow Leopard as "this service can handle ALL TYPES of files". But you're more likely to find such a trick if the different content types aren't all lumped together in a single service entry.

On that last point, one idea, not sure if it's possible: the file import entry uses NSSendFileTypes and is valid only under Snow Leopard, while the "New Note With Selection" entry stays mostly as it currently is (including NSFilenamesPboardType) but has some added parameters (eg under NSRequiredContext) that tell Snow Leopard the service should only operate on text selections, and not files.

Alternately: given the state of the Services menu prior to Snow Leopard, I question how many people might be using the file-import capabilities of this service on older versions of OS X. You might be able to take a Snow Leopard-only approach to the file import service without anyone noticing.

References: http://developer.apple.com/documentation/Cocoa/Conceptual/SysServices/Articles/properties.html http://developer.apple.com/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSPasteboard_Class/Reference/Reference.html

scrod commented 13 years ago

Thank you, these are very helpful suggestions. I do agree with you that it aids usability for the services menu item name to more accurately reflect the resulting action, but on the other hand having multiple items appear in 10.5 and under might be annoying.

Since most of these changes are isolated to the Info.plist file, do you think you'd mind trying some of them out to see how well they work?