User:Hendy:ImageToIPhoto

From Camino Wiki
Jump to navigation Jump to search

Add "Add Image to iPhoto Library" to contextual menu

Safari has this feature. It downloads the image to a folder in /private/tmp, like /private/tmp/SafariiPhoto6ImagesSuudKO, and then somehow adds it to iPhoto.

We can do a similar thing, by saving it to a temporary folder and using something like the following Applescript:

tell application "iPhoto"
    import from "/path/to/temp/folder/lolcat.jpg"
end tell

The main issue is to get the image into that temporary folder. Our present Save Image… command eventually gets to CHBrowserView's

saveInternal: withDocument: suggestedFilename: bypassCache: filterView:

method. This is the method that sets up an nsHeaderSniffer, which then gives us the Save dialog and pops up the Downloads window. We don't want either of those, since we want to make the process seamless for the user.

Removing the nsHeaderSniffer from the method, for example by commenting out the line

webPersist->SetProgressListener(sniffer);  // owned

gets us partway there. If you save an image using the context menu, neither the save dialog nor the downloads window comes up. The download image isn't in any of the places you'd expect, either. You'll find it in

~/Caches/TemporaryItems

with a name like -sav0tmp. That's set up earlier in the method.

What we could do is write a similar method, that sets up a temporary file with the same name as the src attribute of the image. We can tell Gecko to grab it from the cache. It doesn't seem to matter if the file name doesn't have an extension on it, since iPhoto will happily import a test PNG with the name -sav0tmp. It's all about the magic numbers, apparently.

We wouldn't need to remove the file after iPhoto imports it either, as

~/Caches/TemporaryItems

is automatically cleared each reboot, as is the directory that Safari uses.


Apple Events

Smokey has documented the Apple Events that Safari uses.