Last active
October 17, 2023 05:34
-
-
Save donatj/1108691 to your computer and use it in GitHub Desktop.
Photo Booth Rebuild Shell Script - see http://donatstudios.com/Lion-Photobooth-Repair for usage.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env php -q | |
<?php | |
/** | |
* Photo Booth Plist Rebuilder / Repair | |
* @author Jesse G. Donat <donatj@gmail.com> http://donatstudios.com | |
* @license http://opensource.org/licenses/mit-license.php | |
*/ | |
# Update this path with your username | |
$libraryPath = getenv('HOME') . '/Pictures/Photo Booth Library/'; | |
if( !is_dir($libraryPath) ) { | |
echo 'Failed to locate ' . $libraryPath; | |
die(1); | |
} | |
$photos = scandir($libraryPath . 'Pictures'); | |
// Timezone makes zero difference in comparison - just need to set it to prevent PHP from barking. | |
date_default_timezone_set('America/Chicago'); | |
const REG_A = '/(Photo|Movie) (?P<index>[0-9]+)\.(jpg|mov)/'; | |
const REG_B = '/(Photo|Movie) on (?P<date>[0-9]+-[0-9]+-[0-9]+) at (?P<time>[0-9]+\.[0-9]+)( #)*(?P<index>[0-9]+)*\.(jpg|mov)/'; | |
const REG_C = '/(Photo|Movie) on (?P<date>[0-9]+-[0-9]+-[0-9]+) at (?P<time>[0-9]+\.[0-9]+ (AM|PM))( #)*(?P<index>[0-9]+)*\.(jpg|mov)/'; | |
$photos = array_filter($photos, function($file){ | |
return $file[0] !== '.'; | |
}); | |
usort($photos, function( $a, $b ){ | |
preg_match(REG_A, $a, $a_match) || | |
preg_match(REG_B, $a, $a_match) || | |
preg_match(REG_C, $a, $a_match); | |
preg_match(REG_A, $b, $b_match) || | |
preg_match(REG_B, $b, $b_match) || | |
preg_match(REG_C, $b, $b_match); | |
if( !isset($a_match['date']) && !isset($b_match['date']) ) { | |
return $a_match['index'] < $b_match['index'] ? -1 : 1; | |
} | |
if( !isset($a_match['date']) ) { | |
return -1; | |
} | |
if( !isset($b_match['date']) ) { | |
return 1; | |
} | |
$a_match['date'] = str_replace('-','/',$a_match['date']); | |
$a_match['time'] = str_replace('.',':',$a_match['time']); | |
$b_match['date'] = str_replace('-','/',$b_match['date']); | |
$b_match['time'] = str_replace('.',':',$b_match['time']); | |
$a_time = strtotime($a_match['date'] . ' ' . $a_match['time']); | |
$b_time = strtotime($b_match['date'] . ' ' . $b_match['time']); | |
return $a_time == $b_time ? ( $a_match['index'] < $b_match['index'] ? -1 : 1 ) : ( $a_time < $b_time ? -1 : 1 ); | |
}); | |
$xml = | |
'<?xml version="1.0" encoding="UTF-8"?> | |
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | |
<plist version="1.0"> | |
<array> | |
'; | |
foreach( $photos as $filename ) { | |
if( $filename[0] !== '.' && is_file($libraryPath . 'Pictures/' . $filename ) ) { | |
$xml .= "\t<string>".htmlentities($filename)."</string>" . PHP_EOL; | |
} | |
} | |
$xml .= '</array> | |
</plist>'; | |
if(file_exists( $libraryPath . 'Recents.plist' )) { | |
rename( $libraryPath . 'Recents.plist', $libraryPath . 'Recents.plist.bk.' . time() ); | |
} | |
file_put_contents( $libraryPath . 'Recents.plist', $xml ); | |
echo "\033[0;32mFinished Successfully!\033[0m" . PHP_EOL . PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment