Image Wells

2 things.

  1. How do I get the alias of the image someone drops in?

  2. How do I set the image using resources?

I tried image() and setImage_() but I can’t do anything but set the default in IB.

Hi,

1.) You have to implement the drag&drop event handlers

2.) create an NSImage with initWithContentsOfFile:, the file path can be specified with the method pathForResource:ofType: of NSBundle

Deal with 2 first.

valwiniconpic's setImage_(pathForResource_ofType_("AlertStopIcon","icns"))

gets:

2009-09-30 15:24:25.523 Dialog Maker[408:a0f] *** -[Dialog_MakerAppDelegate pathForResource:ofType:]: unrecognized selector sent to object <Dialog_MakerAppDelegate @0x2002dc060: OSAID(4)>
2009-09-30 15:24:25.530 Dialog Maker[408:a0f] *** -[Dialog_MakerAppDelegate subiconpic:]: *** -[Dialog_MakerAppDelegate pathForResource:ofType:]: unrecognized selector sent to object <Dialog_MakerAppDelegate @0x2002dc060: OSAID(4)> (error -10000)

pathForResource returns a string path, but setImage expects an NSImage

Take a look at this thread

PS: You can’t initialize a NSImage directly with an .icns file using initWithContentsOfFile:, but there is a work around

NSImage *iconImage = [[NSWorkspace sharedWorkspace] iconForFile:@"/path/to/file.icns"];
log "3"
set newImage to NSImage's alloc()'s initWithContentsOfFile_(pathForResource_ofType_("AlertStopIcon", "icns"))
log "3.1"
valwiniconpic's setImage_(newImage)
log "3.2"

It logs this error:

If the image is in the bindle, you can also do it this way:

		tell class "NSImage" of current application to set thePic to imageNamed_("Chart.icns")
		imageWell's setImage_(thePic)

How come that works and the other one didn’t? Oh well it’s working brilliantly, thanks. I need a way now to set the well with an icon file from anywhere in the system (with value returned from

choose file of type {"com.apple.icns"}

) , I’m guessing your method won’t work.

The problem was in pathToResource_ofType_ – you were trying to use a class method without reference to the NSBundle class.

Like this:

set posPath to POSIX path of (choose file)
		set newImage to NSImage's alloc()'s initWithContentsOfFile_(posPath)
		imageWell's setImage_(newImage)

Thanks, but just as I saw the panel post I changed my mind (sorry XD :lol:)

How can I put a Finder window inside one of those ‘panels’? I looked at browser view but I’m not sure about it all.

:slight_smile:

What do you mean by a “Finder window”?

Like save panels that come down like in TextEdit.

There are classes like NSOpenPanel and NSSavePanel, which include everything

OK how do I go about that then?

OK I have put in a choose file instead, but now all I need to implement it is the way to get the file location from the well’s image, whether my app chose the image or it was dragged in.

The image doesn’t record the location of the original; you’ll need to store it yourself when you create the image.

I already have IBActions for dragging and choosing, by me writing ‘choosing’ just there I remembered something, so now getting the icon on choose file is sorted so now what I need to know is how to get the alias of an image dragged in.

:slight_smile:

Bump. Please reply to my recent question about drag and drop.

You’re going to have to override the drag-and-drop code so you can catch the path. Not exactly straight forward.

This post shows how to accomplish this.

http://www.cocoadev.com/index.pl?FakeImageView

Make sure to read the documentation for registerForDraggedTypes since those have
changed in 10.6.