Making a slideshow app.

Greetings all, this is my first post. I’m an Applescript newbie, and this is my first major undertaking.
I’m building an application in Applescript Studio. Everything is going fine, except that I need it to display a jpg that it finds in a folder.

Here is the setup:
- The picture is called “picture1.jpg”
-“picture1.jpg” is the first in a series of pictures, so I need need create a variable that will hold “picture” and “.jpg” as text and some sort of counter variable.
- The picture is located in the folder “area1” inside another folder called “pictures”, which is sitting in a disk called “storage”. I’ve created the disk using disk utility, if that means anything. Later, it’ll be a CD.
- The window holding the picture is called “pictureWindow” in its applescript tab.
- The window is sitting directly on the main window, called “mainWindow” in its applescript tab.

  • Also, I understand that I must release the file to save on memory usage. I don’t know the code for that, either.

I think that’s it. I’ve done this already in Script Editor, but I just used Quicktime Player to open and display the file. I’d like to do it all in a self-contained app. Thanks.

Greetings.

I assume you’re trying to make a slideshow app. Below is what worked for me. I set it up to run images from a folder structure identical to yours on my desktop, so you’ll have to fiddle with the path a bit to get it to work from your disk or on your cd, but that should be a small issue. I also put the code to change the picture in a button clicked event but you could automate it or use another event with little modification. The on awake from nib handler should be attached to the window “mainWindow” so it loads the first image when the app is launched.

property writePicture : "picture"
property writeJPG : ".jpg"
property currentImage : 1
property imagesDirectory : "storage/pictures/area1/"

on clicked theObject
	if name of theObject is "showNextImage" then
		try
			-- Save the old image for deletion later
			set oldImage to image of image view "pictureWindow" of window "mainWindow" 

			-- Increment the counter
			set currentImage to (currentImage + 1)
			
			-- Set the path to the new image to display
			set desktopPath to (POSIX path of (path to desktop))
			set pathToImages to ((desktopPath & imagesDirectory) as string)
			set fullImagePath to ((pathToImages & writePicture & currentImage & writeJPG) as string)

			-- The test text field that shows the full image path (can delete)
			set contents of text field "currentImagePath" of window "mainWindow" to fullImagePath 

			--Load the new image
			set newImage to load image (fullImagePath) 
			set image of image view "pictureWindow" of window "mainWindow" to newImage

			-- Try to delete the old image
			try
				delete oldImage
			end try
		on error
			-- Do something --
		end try
	end if
end clicked

on awake from nib theObject
	try
		set desktopPath to (POSIX path of (path to desktop))
		set pathToImages to ((desktopPath & imagesDirectory) as string)
		set fullImagePath to ((pathToImages & writePicture & currentImage & writeJPG) as string)
		set contents of text field "currentImagePath" of window "mainWindow" to fullImagePath
		set newImage to load image (fullImagePath)
		set image of image view "pictureWindow" of window "mainWindow" to newImage
	end try
end awake from nib

I took the delete image code from Apple’s “image view” page and haven’t checked to make sure it’s actually deleting the old image. The page also has some other info you might want to read, and may suggest a way to check that it is in fact deleting them. I also added in a text field for testing that shows the file path, so I could see what it was looking for when I got errors.

Hope this helps…
j

Close, but no cigar. I had some of this before, so it’s nice to see from your code that I’m on the right path. The problem is in the file path. I used your code verbatim, and the picture didn’t load. I added the filepath text field like you suggested, and the path looked good.
In an attempt to isolate the problem, I moved picture1.jpg around and adjusted the path in the Applescript to see if I could find it. I put it at the root of the ‘storage’ disk, and it didn’t work. Then I put it on the desktop, and it did work. This was very interesting, so I double-checked all my file, folder and disk names, both in the script and on my desktop. Everything appears to be good. So it’s the imagePath variable that’s giving me problems.

Right now, the code snippet is as follows:

set desktopPath to (POSIX path of (path to desktop))
set pathToImages to ((desktopPath) as string)
set fullImagePath to ((pathToImages & writePicture & currentImage & writeJPG) as string)
set contents of text field “currentImagePath” of window “mainWindow” to fullImagePath
set newImage to load image (fullImagePath)
set image of image view “pictureWindow” of window “mainWindow” to newImage

Notice imagePath has been eliminated. Any attempt to put the picture anywhere else makes it dissappear.

So here are some questions: Let’s assume I don’t need the whole ‘counter and text’ variable thing. How do I simply load a picture that isn’t on the desktop? Does and AS app have a problem loading an image that isn’t on the same disk? And why do I have to go to the desktop first at all? Why can’t I just start the filepath with ‘storage/’ ?

Thanks again for your help. This is a very cool group.

No, applescript can load images from anywhere, local or remote. You just have to go through the motions of figuring out how to get to it. :wink:

The following will work no matter where you put the app, because it will find the “storage” directory relative to it’s own location. This is ASSUMING that you have the “/storage” directory in the same directory as the app. I put this into a dmg and is worked smashingly. When you open the disk, you should see the app, and the storage directory (and anything else you’ve got on the disk, of course). :slight_smile:

on awake from nib theObject
	try
		set pathToMe to (path to me)
		
		set OldDel to AppleScript's text item delimiters
		set AppleScript's text item delimiters to ":"
		set pathToAppDir to (text items 1 through -3 of (pathToMe as string) as list) as string
		set AppleScript's text item delimiters to OldDel
		set pathToImages to (POSIX path of ((pathToAppDir & imagesDirectory) as string))

		set fullImagePath to ((pathToImages & writePicture & currentImage & writeJPG) as string)
		set contents of text field "currentImagePath" of window "mainWindow" to fullImagePath
		set newImage to load image (fullImagePath)
		set image of image view "pictureWindow" of window "mainWindow" to newImage
	end try
end awake from nib

I only posted the ‘awake from nib’ handler because the other is pretty much the same but with a bit of extra stuff that you should be able to figure out.

Also, make sure to change the property “imagesDirectory”, as it needs a colon-delimited path to make this new code work right.

property imagesDirectory : ":storage:pictures:area1:"

Good luck…
j

Yup, that was it. I did some experimenting, and I found that I could get your first code to work if ‘storage’ was a folder and not a disk. Which was frustrating, because the path in the text field looked identical either way. When I use the new code, the path has “Volumes/” in it. At any rate, it works.
On a related note, is there any way I can tell if the old image has been successfully deleted, other than flipping through pics until I get a memory error?

Thanks for your help. I’m sure I’ll be posting again here soon.

Sorry, I wrote that code and forgot the important ‘delete’ part was in it. The following code SEEMS to be deleting the image. Here’s what I was using…

on clicked theObject
	if name of theObject is "showNextImage" then
		try
			set oldImage to image of image view "pictureWindow" of window "mainWindow" -- Save the path to the old image
			set currentImage to (currentImage + 1)
			set pathToMe to (path to me) 
			
			set OldDel to AppleScript's text item delimiters
			set AppleScript's text item delimiters to ":"
			set pathToAppDir to (text items 1 through -3 of (pathToMe as string) as list) as string
			set AppleScript's text item delimiters to OldDel
			set pathToImages to (POSIX path of ((pathToAppDir & imagesDirectory) as string))
			
			set fullImagePath to ((pathToImages & writePicture & currentImage & writeJPG) as string)
			set contents of text field "currentImagePath" of window "mainWindow" to fullImagePath
			set newImage to load image (fullImagePath)
			set image of image view "pictureWindow" of window "mainWindow" to newImage
			try -- Delete the old image from memory --
				delete oldImage
			end try
		end try
	end if
end clicked

I couldn’t find a good way of looking into whether the images were “actually” being deleted. I did set up a second image view, and tried to set the image to the value of “oldImage” AFTER I used the ‘delete oldImage’ command, and it didn’t have any image to place there. To me, this says that the image IS being deleted, but if you don’t believe it (me) you’ll have to try clicking through tons of images and seeing if it gets funky or not. :lol:

Cheers…
j