While I classify this as a “Development” article, I mean this for anyone interested in getting more out of their drag-and-drop experience within Mac OS X. I’m going to talk a little about how DnD works behind the scenes because I’ve noticed that after implementing DnD in some of my programs that I’ve become a more pervasive user of it because I know what it’s doing and what will react to it. Hopefully by explaining how DnD is working behind the scenes more people will understand the technology and see more ways to use it in their daily flow.
Many people are initially thrown off DnD by the fact that dragging text in Mac OS X is substantially more difficult that doing the same in Mac OS 9 because the method changed and few learned the difference. I’ll tell you the secret, and it’s very simple: hold the mouse down. Select the text and click anywhere in the selection then keep the click for about two seconds before moving. Now it moves. And now we’re going to have fun.
You can shorten the delay as well.
Data Types
Every bit of data in Mac OS X conforms to a data type at some level, such as text (strings), numbers, URLs, images, and so on. Every one of these can be put onto a pasteboard for travel to another location in or outside of the current program via the system pasteboard.
The system pasteboard isn’t the only pasteboard, however. When dragging content it’s placed on a special pasteboard called the dragging pasteboard which both programs have access to before, during, and after the drag. Thus, a drag almost always like a copy-and-paste since the same behaviors can be used to do it. DnD does not come entirely free, however, so a programmer may or may not make it work like CnP, but usually does.
Copying to the Drag Pasteboard
Often a program will place several kinds of data on the pasteboard since it doesn’t know the capabilities of the receiver (or even who the receiver is). This means that when you go into Text Edit and try and drag a whole bunch of styled text out that you’re going to actually be dragging out styled and plain text. If you were to grab a URL from Safari’s location bar and drag, you’re grabbing a URL object and the URL as a plain text string. If you get an image from Safari you’re getting the image, the URL to the image, and a plain text representation of the URL to the image. Depending on the receiver’s capabilities, you will see the richest representation it has chosen to handle (the order of acceptance is dictated by the receiver).
Applications can also have custom types, like row indexes or objects. While these are on the pasteboard, the other program has no idea what to do with them. While you may be able to copy some data in one program, sometimes you won’t be able to paste in another. It simply doesn’t know what’s on the clipboard and how to use it. In this case, you complain to the author of the program that you copied the data from, not the one that you’re pasting to.
Receiving the Drag Pasteboard
Now when a drag is inbound the receiver will look at the pasteboard, ask it what kinds of data it has and then start down its list of accepted types until it finds one it likes. Safari’s URL bar prefers URLs, but will take plain text as well. Text Edit in plain text mode just wants plain text. In rich text mode it prefers rich text, but will take plain as well. You get the idea.
This is why dragging an image into plain-text-mode Text Edit gives you the URL to the image, or dragging any URL, like a link, will give you the plain text of the destination rather than the text of the link. Safari chose to make the plain text representation of the URL be the URL and not the link text itself, and the receiver chose that version.
Services and the Pasteboard
Well, that’s helpful, but let’s get fun. Programs on Mac OS X can provide services to the rest of the system. In doing so, they declare those same pasteboard types to the system and take their content to work with off of a services pasteboard as well. The great thing is that the application icon is linked to these services! Safari responds to URL objects via the “Open URL” service, and because of that you can drag URLs onto the Safari icon. One of my programs, Notae, responds to both text and URL objects and if either are dragged to the application icon it does something different for each one (makes a new note with text, or fetches the web page with a URL). This came “for free” when I added services support.
Try it!
Now that you know that there’s rich text, plain text, URLs, images, and other types all on the same pasteboard, you should be able to see some of the power that awaits. Browse through the services menu and look at what’s available from your most-used programs. You’ll get the same functionality on the Dock when the program is open. Look at where it would be useful to drag URLs (like Safari’s Download window) and try it. DnD is everywhere and powerful enough that, if you get used to using it, it will change how you use your Mac.
Do you know if using something such as Service Scrubber to remove items from an over-crowded Services menu will also cause the app to not respond to the drag-and-drop to the dock icon? I assume not, but just thought I’d double-check to see if you know.
It depends on how the program works. The only way I can think of a program like that working is to modify the plists of the applications to remove the service announcement and then tell Launch Services to regenerate the services database. If that’s the case, then the service has been disabled at a fundamental level and, no, the Dock would not respond.
But that’s just conjecture. I’ve not tried the program so I have no idea how it does what it does. I also see no need for it as I’ve assigned keyboard shortcuts to those services I use often so I never use the menu.
Post new comment