ImageView Drag & Drop

Hello everyone,
I am trying to use an image view for a drag and drop operation. Here is some more information, I want to drop an application onto the image view and set a variable called theApp to that app. I know this sounds confusing so maybe this applescript will clear it up a bit:


--drag and drop operation here
set theApp to (whatever the user dropped on the image view)

Thanks,
TechExpertHD

I think you’ll have to subclass NSImageView, and add some code like this:

script TheImageView
property parent : class “NSImageView”

on draggingEntered_(info)
	return current application's NSDragOperationCopy
end draggingEntered_

on performDragOperation_(info)
	set pb to info's draggingPasteboard()
    set theURL to pb's readObjectsForClasses_options_({current application's NSURL},missing value)'s lastObject()
    set theApp to theURL's lastPathComponent()
    log theApp
    return true
end performDragOperation_

end script

Then, in your app delegate, you could add a line in the applicationWillFinishLaunching method to register the image view for dragged types:

imageView's registerForDraggedTypes_({"public.file-url"})

Ric

Thanks!

A few questions:

  1. Did you mean property imageView: class “NSImageView”? Because when I try it it says that the property “parent” is specified more than once.

  2. For this: imageView’s registerForDraggedTypes_({“public.file-url”}) can I change it to imageView’s registerForDraggedTypes_({“com.apple.application”}) to make sure that only applications are dropped onto the NSImageView?

Thanks a lot!

TechExpertHD

  1. TheImageView is a separate script, so there shouldn’t be any other parent (what I posted is the entire script).

  2. I don’t know if you can use that – I haven’t tried that before, and it would probably require other changes to make it work (and it would only work with apple apps, maybe that’s what you want). The other way to go is to put an if statement in there to check if the file extension is .app, and if not return false instead of true.

Ric

Thanks! No wonder I was having problems with it, I put it in my app delegate… Thanks so much, my app is almost complete!

Okay for people that have this same problem, step by step I’ll explain how to do this:

  1. Create a new applescript class in your project.
  2. The content of the class should be this:

script  (Script Name)
    property parent : class "NSImageView"

     on draggingEntered_(info)
        return current application's NSDragOperationCopy
    end draggingEntered_
   
    on performDragOperation_(info)
        set pb to info's draggingPasteboard()
        set theURL to pb's readObjectsForClasses_options_({current application's NSURL},missing value)'s lastObject()
        set randomVar to theURL's lastPathComponent()
        log randomVar
        return true
    end performDragOperation_
   
end script

  1. In your app delegate input the following code:

(Script Name)'s registerForDraggedTypes_({"public.file-url"})

  1. In Interface Builder, find your class called (Script Name) in the “Classes” section of your Library.
  2. Add it to your project by drag and dropping it onto a window or some place else.
  3. Build and Run!

Hello again,
As you mentioned earlier, I could use an if statement for checking if the file dropped onto the ImageView was an application. After tinkering around with it, I came up with this code but it doesn’t work. If you could help me with this issue it would be greatly appreciated. Here is the code I came up with:


set theApp to (choose application)
if the name extension of theApp is {"app"} then
	display dialog "Nice!"
else
	display dialog "So close..."
end if

Thanks,
TechExpertHD (though I’m not feeling like an expert after all these questions… :/)

You really need to know quite a bit of the “vocabulary” of cocoa to understand how to do these things – it takes a lot of time and study before you can get to that point. Here’s one way to do it:

on performDragOperation_(info)
		set pb to info's draggingPasteboard()
        set theURL to pb's readObjectsForClasses_options_({current application's NSURL},missing value)'s lastObject()
        set theApp to theURL's lastPathComponent()
        set theExt to theURL's pathExtension()
        if theExt as text is "app" then
            log theApp
            return true
        else
            log "That's not an application"
            return false
        end if
	end performDragOperation_

Ric

Ahhh, I was thinking in applescript language, forgot about the cocoa and objective c part! If there are any tutorials that would point me in the right direction for learning this vocabulary of cocoa, please include a link! Thanks a lot rdelmar, you’ve helped me a lot ;).

Thanks,
TechExpertHD