drag and drop images from browser

hi,

I have an image view in my project in xcode. I can drag and drop images from finder to it. But I would also like to drag them from other sources (like the browser).

I use something like this:


on awake from nib theObject 
if the name of theObject is equal to "image view" then 
tell theObject to register drag types {"file names"} 
end if 
end awake from nib 

on drop theObject drag info dragInfo 
set dataTypes to types of pasteboard of dragInfo
if "file names" is in dataTypes then 
delete image of image view "image view" of window "main" 
set dataTypes to types of pasteboard of dragInfo 
set preferred type of pasteboard of dragInfo to "file names" 
set theDropFile to (contents of pasteboard of dragInfo as string) 
set currentImage to (load image theDropFile) 
set image of image view "image view" of window "main" to currentImage 
end if 
end drop

How can I extend this to also support dragging from other sources? I already tried:

tell theObject to register drag types {“file names” “image”} in the awake section
so I assume I have to continue with a if “image” is in dataTypes… in the drop section
but then I got stuck: how do I get the image from the pasteboard?
I guess I will have to save the image first to some location ?

thanks for any help

been trying to get this to work all day long and failure.

One funny thing I found along the way was Tim B’s answer to the same question here:

http://lists.apple.com/archives/applescript-studio/2003/Aug/msg00155.html

In which he claims that it’s in the AS Studio Examples:

/Developer/Examples/AppleScript Studio/Image

But it’s not there (at least in my backup of that file). They only implement dropping files, not images.

Here’s what I wrote to the AS Studio mailing list just now:

I’ve spent the day to no avail trying to drop an image onto an image view –
NOT a file (which I can pull off just fine) but an image object say from
Safari. The old AS Studio example (suspiciously?) only showed the file
example.

Basically it appears to recognize the “image” preferred type. But when I do
this:

set theImage to contents of pasteboard of dragInfo

It appears to silently fail, not just get a missing value but “undefining”
the defined missing value, causing the script to choke when checking whether
theImage = missing value, with a “variable is undefined” error.

I can find no working example of this. Maybe it’s just 10.6?

(and yes, all types are registered in awake from nib… I’ve tried both
with and without connected “prepare drop” and “conclude drop” handlers.)

If anyone has seen a solution that works, please let me know!

on drop theObject drag info dragInfo
        set theImage to missing value
        set dataTypes to types of pasteboard of dragInfo
        if "file names" is in dataTypes then
        [...]
        else if "image" is in dataTypes then
            set preferred type of pasteboard of dragInfo to "image"
            try
                set theImage to contents of pasteboard of dragInfo
                try
                    set a to theImage
                on error myerr
                    -- ERROR HERE with "variable theImage is not defined"
                end try
            on error myerr
                -- NO ERROR HERE
            end try
        else if "pict image" is in dataTypes then
            [...]
         end if

        try
            if theImage is not missing value then
            [...]
             end if
        on error
        -- ERROR here, same as above, variable theImage is not defined
        end try
End if

Here my actual logged data types

dyn.ah62d4rv4gu8yc6durvwwa3xmrvw1gkdusm1044pxqyuha2pxsvw0e55bsmwca7d3sbwuApp
le files promise pasteboard
typepublic.tiffimagedyn.ah62d4rv4gu8zs3pcnzme2641rf4guzdmsv0gn64uqm10c6xenv6
1a3kWebURLsWithTitlesPboardTypedyn.ah62d4rv4gu8yc6durvwwaznwmuuha2pxsvw0e55b
smwca7d3sbwuurlpublic.urlCorePasteboardFlavorType
0x75726C20public.url-nameCorePasteboardFlavorType
0x75726C6Epublic.utf8-plain-textstringcom.apple.flat-rtfdrich text
datacom.apple.webarchiveApple Web Archive pasteboard
typecom.apple.pasteboard.promised-file-content-typecom.apple.pasteboard.prom
ised-file-urldyn.ah62d4rv4gu8y6y4usm1044pxqzb085xyqz1hk64uqm10c6xenv61a3kNSP
romiseContentsPboardType

Here’s another weird thing about this. If you do nothing but return TRUE, the image still appears in the view, but it appears to be inaccessible to AppleScript at the time it is dropped, anyway.

Consider this:

on drop theObject drag info dragInfo
	set dataTypes to types of pasteboard of dragInfo
	if "file names" is in dataTypes then
		-- removed
	else if "image" is in dataTypes then
		tell window "main"
			set i to image of image view "image_view"  -- no error, but no result
			set c to count (every image of image view "image_view") 
			set contents of text field "text_field" to "IMAGE, count: " & c
		end tell
	end if
	return true
end drop

The first time you drop an image, you will get an image count of 0. The second time and after you will get an image count of 1.