Skip to content

Instantly share code, notes, and snippets.

@voccs
Created September 29, 2011 01:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save voccs/1249802 to your computer and use it in GitHub Desktop.
Save voccs/1249802 to your computer and use it in GitHub Desktop.
Split an iPhoto library into DVD-sized libraries fit for backup to disc.
-- ESotSM: Split an iPhoto folder into DVD burnable iPhoto folders
-- size of a DVD-sized photo library
set LIBRARY_SIZE_LIMIT to 4.69E+9
-- don't start checking size until library has fewer photos than this
set LIBRARY_LENGTH_LIMIT to 1200
-- Acquire photo library
set pictures_folder to path to pictures folder from user domain as string
set photos_folder to choose file with prompt "Select the iPhoto library to split." default location POSIX path of pictures_folder with invisibles
-- warn about backing up
set backed_up to button returned of (display dialog "Have you backed up your iPhoto library?" buttons {"No", "Yes"} default button 1 giving up after 10)
if backed_up is "No" then return
-- set initial values, especially the size of the current photo library
set library_size to missing value
tell application "System Events"
repeat while (library_size is missing value)
set library_size to get physical size of disk item (photos_folder as text)
delay 2
end repeat
end tell
set first_run to true
set highest_photo_filename to ""
-- mega loop, does all of the library resizing
repeat while library_size > LIBRARY_SIZE_LIMIT
-- set the system path to the default library to what was chosen
do shell script "defaults write com.apple.iPhoto RootDirectory " & quoted form of POSIX path of photos_folder
-- open iPhoto
tell application "iPhoto"
activate
select photo library album
end tell
-- sort photos by date
tell application "System Events"
tell process "iPhoto"
tell menu bar 1
tell menu "View"
tell menu item "Sort Photos"
tell menu "Sort Photos"
click menu item "By Date"
click menu item "Ascending"
end tell
end tell
end tell
end tell
end tell
end tell
-- figure out library volume number
set ending_text to last word of quoted form of POSIX path of photos_folder
try
set library_number to ending_text as number
set new_photos_folder to "/" & text from first word to word -2 of quoted form of POSIX path of photos_folder & " " & (library_number + 1)
on error
set library_number to 2
set new_photos_folder to "/" & quoted form of POSIX path of photos_folder & " " & library_number
end try
tell application "iPhoto"
-- eliminate all photos up to and including the highest photo of the last round
if not first_run then
set photo_list to photos of the photo library album
repeat with current_photo in photo_list
if the image filename of the current_photo is not equal to highest_photo_filename then
remove current_photo
else
remove current_photo
exit repeat
end if
end repeat
empty trash
end if
set sub_library_size to missing value
tell application "System Events"
repeat while (sub_library_size is missing value)
set sub_library_size to get physical size of disk item (photos_folder as text)
delay 2
end repeat
end tell
set library_size to sub_library_size
-- copy full library to incremented library volume number
if library_size > LIBRARY_SIZE_LIMIT then
do shell script "cp -R '" & POSIX path of photos_folder & "' '" & new_photos_folder & "'"
end if
-- get rid of anything in a library clearly too large to fit
set photo_list to photos of the photo library album
set library_length to length of photo_list
repeat while library_length > LIBRARY_LENGTH_LIMIT
remove the last item of the photo_list
set photo_list to photos of the photo library album
set library_length to length of photo_list
end repeat
-- remove and empty trash for all photos over the library size limit
repeat while sub_library_size > LIBRARY_SIZE_LIMIT
remove the last item of photo_list
empty trash
set photo_list to photos of the photo library album
set sub_library_size to missing value
repeat while (sub_library_size is missing value)
tell application "System Events"
set sub_library_size to get physical size of disk item (photos_folder as text)
end tell
delay 2
end repeat
set threshold to sub_library_size - LIBRARY_SIZE_LIMIT
if threshold < 5.0E+6 then
delay 10
else if threshold < 5.0E+7 then
delay 5
else if threshold < 5.0E+8 then
delay 4
end if
end repeat
-- determine the most recent photo in the now properly sized album
set photo_list to photos of the photo library album
set highest_photo_filename to the image filename of the last item of photo_list
end tell
-- set the new library to work on to the library newly copied in this round
set photos_folder to POSIX file new_photos_folder
-- only one first run
if first_run then
set first_run to false
end if
-- will either be resetting the library path or finishing
tell application "iPhoto"
quit
end tell
delay 5
end repeat
display dialog "Done" buttons {"OK"} default button 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment