Swapping an image in an image view?

okay… so I have an image view that I want to swap images on many times…

I start with an image I made (no_image) in the image view in my on awake from nib handler I try to assign the image to a property like so:

set no_image to image of image view "x" of window of theObject

well that doesn’t quite work right but let’s assume it does (any help from someone later on this point would be great too) and then at some point during the run somebody drops an image on the view… in my drop handler I save the path (for use in the application logic) and just allow the image to change automatically to the new image. Later, I hit a button that is supposed to remove this image from the view and put back my no_image image… I am trying to do this as follows:

set the image of the image view "x" of window of theObject to no_image.

With getting the image I get a NSArgumentEvaluationScriptError and setting gives a NSRecieverEvaluationError which tells me I’m doing it wrong… but I can’t see from the dictionary what else to try… Any thoughts? (PS: If someone also knows how I can load an image based on a path to it… that would be nice too)

I looked in some documentation and it says that I am mis-referencing the image of the image view… But in the debugger when I do it step by step I get :

get the window of theObject -- window id 1
get the image view "x" of the result -- image view id n
get the image of the result -- image id 6
set no_image to the result -- NSArgumentEvaluationScriptError... grr ,,,~_@,,,

okay… another tidbit on the way to figuring this out… I tried to do this and got an interesting build error:

tell the image_view to set its image to no_image

spits out an error saying I can’t access it…:

Can’t set class image to no_image. Access not allowed (-10003)

But the dictionary says it’s not r/o … :cry:


Since you have a default image you would like to use, it would probably be best to set it with your script, rather than trying to read it from your interface. This will save you the trouble of trying to figure out all of your errors. First, in the head of the script, declare an empty property variable to contain the path to the default image. I also included a property ‘currentImage’ which always keeps track of your current image…

property currentImage : missing value -- The currently displayed image
property default_Image : missing value -- The 'permanent' default image

In my ‘awake from nib’ handler for the image view I used the following. It will create the path to the image, save it’s value into the ‘default_Image’ property, and then set the image as the current image in the image view…

on awake from nib theObject
	-- Define where your image is located (i.e. 'Resources' directory)
	set PathToMe to (path to me)
	set PathToImage to ":Contents:Resources:theImage.jpg" as string
	set theFile to ((PathToMe & PathToImage) as string) as file specification

	-- Set the value of the 'default_Image' property to the path to the image
	set default_Image to theFile

	-- Display the image in the image view at startup
	set currentImage to (theFile as string)
	set newImage to load image (theFile)
	set image of image view "viewer" of window "window" to newImage
end awake from nib

Then, to set the image view back to the default at any time, use something like the following in a button event, menu item, etc…

on clicked theObject
	if name of theObject is "SetDefaultImage" then
		set oldImage to image of image view "viewer" of window "window"
		set image of image view "viewer" of window "window" to (load image default_Image)
		delete oldImage
	end if
end clicked

You’ll want to put much of this last code into ‘try’ statement(s) so you can prevent any conflicts. Trying to get/set/delete an image that does not exist will throw errors.

Sorry about the long response time, and hope this helps…

I’ve been guilty of this in the past, too, but I think it’s more efficient to use POSIX paths and to use the built in paths that the app itself provides:

on awake from nib theObject 
     set default_Image to (resource path of main bundle) & "/theImage.jpg" 
     set currentImage to default_Image 
     set image of image view "viewer" of window "window" to (load image default_Image) 
end awake from nib

[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]