Script to move random selection of files to folder

Hi all,

I use AppleTV Screensaver to show photos on my TV. AppleTV is pointed to a folder of photos on the LAN and it plays them in a random order. Unfortunately if there are more than 200 photos in the folder, it always picks the same ones.

My idea is to clear the folder down each day and use AppleScript to select 200 photos at random from my main photo folder (“Folder A”) and copy the files to the folder that AppleTV is pointed to (“Folder B”). I will also put the AppleTV on a timer socket (the type you use to turn lights on at dawn) which means that when it powers off and back on, it will be forced to rescan Folder B and voila, find a new set of 200 photos.

Unfortunately I know little about AppleScript, although I’m willing to try and learn!

Any ideas how I can set up a scheduled task to achieve the following:

  1. Delete all files in Folder B
  2. Select 200 files from Folder A and copy them to Folder B

Thanks
Tim

Hi Tim,

Are you using iPhoto?

I usually use Aperture, but iPhoto is an option too.

Thanks
Tim

I have my photos in iPhoto but from many different folders depending on the subjects.

Interesting question. I haven’t worked with random lists in a long time.

Hi Tim,

I don’t have aperture so found an easy way with iPhoto. You create a blank album call it “Slideshow1”. Then create a smart album say “NotSlideshow1”. Make its criteria ‘Album is not Slideshow1’. Now any photo you copy to Slideshow1 will be removed from NotSlideshow1. So, your program would look something like this:

  1. delete all photos in Slideshow1
  2. repeat 200 times
  3. get random number from 1 to count NotSlideshow1
  4. add photo whose index is the random number to Slideshow1
  5. go to repeat
  6. play the slideshow

I’m still experimenting with that.

Note that this method might be too slow and you might need to create the random list. Not sure still playing around with this.

gl,
kel

Here’s an example of creating the slideshow album:

-- remove every photo from slideshow album
tell application "iPhoto"
	set temp_ids to id of every photo of album "Slideshow1"
	repeat with this_id in temp_ids
		remove photo id this_id from album "Slideshow1"
	end repeat
end tell

-- add 200 random photos to slideshow album
tell application "iPhoto"
	repeat 200 times
		set id_list to id of every photo of album "NotSlideshow1"
		set c to count id_list
		tell me
			set r to random number from 1 to c with seed 0
		end tell
		set this_id to (item r of id_list)
		add photo id this_id to album "Slideshow1"
	end repeat
end tell

Remember that the album “NotSlideshow1” is a smart album.

It took about 10 seconds on my machine. It seems like deleting the photos from the slideshow album is taking most of the time.

Note that the photos are random, but I think sorted. If you could only play the random photos randomly, that would be better. Still experimenting with slideshows.

gl,
kel

Found how to show the slideshow in the random order. First select the album “Slideshow1”. Then go to menu View > Sort Photos and check Manually. Here the script shows the slideshow:

-- remove every photo from slideshow album
tell application "iPhoto"
	set temp_ids to id of every photo of album "Slideshow1"
	repeat with this_id in temp_ids
		remove photo id this_id from album "Slideshow1"
	end repeat
end tell

-- add 200 random photos to slideshow album
tell application "iPhoto"
	activate
	repeat 200 times
		set id_list to id of every photo of album "NotSlideshow1"
		set c to count id_list
		tell me
			set r to random number from 1 to c with seed 0
		end tell
		set this_id to (item r of id_list)
		add photo id this_id to album "Slideshow1"
	end repeat
	start slideshow using album "Slideshow1"
end tell
-- note: set Sort Photos to Manually in iPhoto for album Slideshow1

gl,
kel

Fixed the script up a little.

property num_slides : 200
property ss_name : "Slideshow1"
property smartf_name : "NotSlideshow1"

-- remove every photo from slideshow album
tell application "iPhoto"
	set temp_ids to id of every photo of album ss_name
	repeat with this_id in temp_ids
		remove photo id this_id from album ss_name
	end repeat
end tell

-- add num_slides random photos to slideshow album
tell application "iPhoto"
	activate
	set c to count (every photo of album smartf_name)
	set p to (c - num_slides) + 1
	repeat with i from c to p by -1
		tell me
			set r to random number from 1 to i with seed 0
		end tell
		set temp_ids to (id of every photo of album smartf_name)
		set this_id to (item r of temp_ids)
		add photo id this_id to album ss_name
	end repeat
	start slideshow using album ss_name
end tell
-- needs error checking for total photos being greater than slidshow slides
(*
Note: set Sort Photos to Manually in iPhoto for album Slideshow1 or in the slidshow settings
Also set music and theme there
Instruction:
make an album with all the slides
make an empty album
make a smart album with criteria
> Album is the album with all the slides
> Album is not album that is empty	
*)

Also have a random list maker if you need it.

Edited: and about the random folder A to folder B, would creating alias files in folder B to the images in folder A work?

gl,
kel

Hi Tim,

Modified the script with help from Nigel Garvey:

property num_slides : 200
property ss_name : "Slideshow1"
property smartf_name : "NotSlideshow1"

-- remove every photo from slideshow album
tell application "iPhoto"
	set temp_ids to id of every photo of album ss_name
	repeat with this_id in temp_ids
		remove photo id this_id from album ss_name
	end repeat
end tell

-- add num_slides random photos to slideshow album
tell application "iPhoto"
	activate
	-- get num_slides random photos
	repeat num_slides times
		set this_id to (id of some photo of album smartf_name)
		add photo id this_id to album ss_name
	end repeat
	start slideshow using album ss_name
end tell
-- needs error checking for total photos being greater than slidshow slides
(*
Note: set Sort Photos to Manually in iPhoto for album Slideshow1 or in the slidshow settings
Also set music and theme
Instruction:
make an album with all the slides
make an empty album
make a smart album with criteria
> Album is the album with all the slides
> Album is not album that is empty	
*)

It’s a few seconds quicker.

gl,
kel