Image Wells

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.

Isn’t that Obj-C? Would I have to rewrite it all in ASOC?

Hang on. Is this all because of ASOC or is there even no way of doing this in Obj-C?

Also what is the value path binding for an image well?

In would be interested but I guess it’s too much for ASOC, thanks for the help this post.

It turns out that it’s not very hard, either. First, you need to make your image well editable. Then you need to subclass it: File → New File, where it says “NSObject”, change it to “NSImageView”, and then change the class of your image well to the new class.

Then there’s no need to register or include a draggingEntered_ handler – you just need a performDragOperation_ handler. So if you had a text field that you wanted to contain the path to the image, you could use:

	property parent : class "NSImageView" -- subclass NSImageView
	property textField : missing value -- connected to a text field/label
	
	on performDragOperation_(sender)
		-- Get the file path
		set pb to sender's draggingPasteboard()
		set theFiles to pb's propertyListForType_("NSFilenamesPboardType")
		set filePath to item 1 of (theFiles as list)
		-- Set text field to path
		my textField's setStringValue_(filePath)
		return true -- otherwise it doesn't happen
	end performDragOperation_