I have succeeded in placing an image into an NSView and I had difficulty with initWithContentsOfFile.
The path was the issue as it needs an NSString.
I eventually succeeded with this:
tell class "NSString" of current application to set imagePath to its stringWithString_("/Users/terry/Desktop/Devon/DSCF0534.jpg")
tell class "NSImage" of current application to set myImage to its alloc
tell myImage to initWithContentsOfFile_(imagePath) -- myImage is NSImage
my setNeedsDisplay_(true) -- my is the NSView
This is correct. Check that your file path is correct. It must be a POSIX style path.
To release you call the instantiated’s release method. This is the same for all objects.
newImage's release()
I have never used Garbage collection so you will have to read up on that to see
exactly how to use it. There are some caveats, but I do not remember what they are.
set newImage to NSImages's alloc()'s initWithContentsOfFile_(this_file)
imageField's setImage_(newImage)
or this
tell class "NSString" of current application to set imagePath to its stringWithString_(this_file)
tell class "NSImage" of current application to set myImage to its alloc
tell myImage to initWithContentsOfFile_(imagePath) -- myImage is NSImage
theimageField's setImage_(myImage)
myImage's release()
but this does work:
tell class "NSString" of current application to set imagePath to its stringWithString_(this_file)
tell class "NSImage" of current application to set myImage to its alloc
tell myImage to initWithContentsOfFile_(imagePath) -- myImage is NSImage
theimageField's setImage_(myImage)
myImage's release()
I had to create an instance of NSString at top too:
Hi Craig,
It seems like this simpler approach should work but
property NSString : class "NSString" of current application -- at top
set newImage to NSImages's alloc()'s initWithContentsOfFile_(this_file)
theimageField's setImage_(newImage)
newImage's release()
this doesn’t.
I tried adding something like:
set imagePath to NSString's stringWithString_(this_file)
set newImage to NSImages's alloc()'s initWithContentsOfFile_(this_file)
theimageField's setImage_(newImage)
newImage's release()
I thought it was the stringWithString_(this_file) that got this_file into the right form for NSImages but can’t find a simple way to get it to work other than the loner approach in my last post.
I have to really get my syntax straight. I just noticed that extra 's after my last post but had all the other code in there and didn’t think to try the original line sans the extra “s”
That sorts it simply and elegantly. I have now completely converted my app over to the new form and it seems so much more direct and fast! Now the old applescript bridge to OBJ-c seems convoluted and awkward.
One more thing left for this app - I have to save all my settings to user defaults. Since the UI elements are now bound to my properties (which is excellent) I can not bind them to Shared User defaults. There is always an external file (your booklist app) but I want to use the user plist. lI’ll start another topic on that when I get totally confused.