Using an Image View and a repeat loop... my brain hurts!

I have an AS/S project which has a small window containing an image view, some text fields and a few buttons. When image files are dropped on to the image view, it displays the image, opens Image Events to extract information about the file and then displays the info in the text fields. There is also a drawer which, when a button is pushed, opens to display additional information. Okay - all of that works fine. However, if a group of files is dropped, I want it to replace the preview and information from the first image with those of the next, etc., when a “next” button is pushed. This is where my brain starts to hurt. I have no problem doing this in Applescript by itself, but when the interface is tied into it… drawing a blank. Here is a slice of my code:

on drop theObject drag info dragInfo
	set dataTypes to types of pasteboard of dragInfo
	if "file names" is in dataTypes then
		set preferred type of pasteboard of dragInfo to "file names"
		set thePaths to contents of pasteboard of dragInfo
		set preferred type of pasteboard of dragInfo to ""
		repeat with thePath in thePaths
			set theFile to (POSIX file thePath as string) as file specification
			set fileInfo to info for theFile
			if (folder of the fileInfo is false) and ¬
				(alias of the fileInfo is false) then
				set image of image view "dropZone" of window "Main" to (load image theFile)
					tell application "Image Events"

--Do all kinds of neat stuff that gets information about the image, then displays part of it on the main window, and part of it on a drawer which is opened by an "on clicked".

end try
			end if
		end repeat
	end if
end drop

If anyone can steer me in the right direction, I would appreciate it. Please take it easy, though, I am relatively new to all of this…


I am not sure if I could help but let me try. There’s nothing to lose except that you may still have to do some work. All I would like to do here is give you some lead that might (or might not) help.

I have a program that successively displays images out of a chosen folder. I imagine that your group of image files is contained in a folder. How I have the folder chosen and how the images are pulled out is where we differ. Please refer to the script codes below.

In my case, the images in the target folder are stored in a list and randomly pulled out (not via POSIX path to file). I read somewhere, though I have forgotten where, that image events does not “like” POSIX path to the file so that may be one of your problems. Once the image is pulled out, it can then be loaded for eventual display in the image view.

set folderFile to (choose folder)
--get all the images in the folder and store them in the list
set list_imagefiles to {}
set myFiles to every file of folder (folderFile as string)
repeat with i from 1 to count myFiles
	set end of list_imagefiles to item i of myFiles
end repeat
--now, get random picture from the list of images
set randompic to random number from 1 to count list_imagefiles
set imageFile to item randompic of list_imagefiles
set theFile to (imageFile as string) as file specification
--then load the image for eventual display into the image view
set newImage to load image (theFile)
set image of image view "myPicture" of window "pictureShow" to newImage

--Note that I did not use a POSIX path to the image files; I read somewhere that image event does not "like" POSIX path; I just can't remember where.

I can visualize a situation where you could do the same- drop your image folder, store the images in a list, source the list successively via a “Next” button, until all the images have been processed. As an image is being displayed, you could also extract the other info to be displayed in the textfields of your window. Once the list is empty, you can then end the program or ask for more images to process. Meanwhile, the “Next” button can be deactivated if there are no more images to process and activate it again once more images have been sourced.

Try it and see if it works.

Good luck.



You are approaching this whole thing the wrong way. Repeat loops are generally inappropriate for use in controlling your interface. In your case, where you expect to change your interface based on user interaction, I’m not sure how you arrived at cycling through your images in a repeat loop in the first place.

It is important to get a better understanding of not only how things work in ASStudio, but why they work that way. ‘Handlers’ exist to do one thing… to handle something. In this case, the drop handler should only be responsible for one thing, which is to handle the drop of your files. End of story. Any more that you try to do in the drop handler should be limited only to actions or configuration that directly relates to the files having been dropped. After receiving your dropped files, you should save the list of files, and then wait for the user to do something. When the user clicks a button, then react accordingly, most likely in the ‘clicked’ handler of the “Previous” and “Next” buttons which I assume will grace your application.

This is certainly not a complex thing to do, it just requires that you take special care to understand exactly what the flow of information is and how to best compartmentalize your code.

Here is some pseudo-code that should get you started…

property droppedFiles : {}
property displayIndex : 1

on drop theObject drag info dragInfo
	(*SNIP* : pasteboard stuff *)
	set droppedFiles to contents of pasteboard of dragInfo --> Save the list of files in the property variable

	set displayIndex to 1
end drop

on clicked theObject
	if name of theObject is "Next" then
		set displayIndex to (displayIndex + 1)
		if (displayIndex > (count droppedFiles)) then set displayIndex to 1
	else if name of theObject is "Previous" then
		set displayIndex to (displayIndex - 1)
		if (displayIndex < 0) then set displayIndex to (count droppedFiles) 
	end if
end clicked

to displayImageAtIndex(tmpIndex)
	set tmpImagePath to (POSIX file ((item tmpIndex of droppedFiles) as string)) as file specification
	(* Load your image, get info about it, display it and it's info in your UI *)
	(* ...Don't forget to delete the image view's old image *)
end displayImageAtIndex

Note that this is not actual code, and hasn’t been tested or compiled. It is to illustrate the separation of tasks and how to approach this. It’s up to you to fill in the meat and potatoes.


Thanks, for the lesson as well as the example. My problem is that I have in my head an idea of how things work in Applescript and I am struggling to make the paradigm shift. I think I get what you are saying, though. Off to try to work it out…

I got it working. Thank you very much!