dotfiles

My personal shell configs and stuff
git clone git://git.alex.balgavy.eu/dotfiles.git
Log | Files | Refs | Submodules | README | LICENSE

export-photos-albums-to-folders (3060B)


      1 #!/bin/sh
      2 # Export selected albums from macOS Photos to folders
      3 os=$(uname -s | tr '[:upper:]' '[:lower:]')
      4 case "$os" in
      5   darwin*)
      6     # code for macOS
      7     command -v osascript >/dev/null 2>&1 || { printf "osascript not present.\n" && exit 1; }
      8     ;;
      9   *)
     10     printf "This script is only needed on macOS.\n"
     11     exit 1
     12     ;;
     13 esac
     14 
     15 osascript - <<EOF
     16 on sortList(theList)
     17     set theIndexList to {}
     18     set theSortedList to {}
     19     try
     20         repeat (length of theList) times
     21             set theLowItem to ""
     22             repeat with a from 1 to (length of theList)
     23                 if a is not in theIndexList then
     24                     set theCurrentItem to item a of theList as text
     25                     if theLowItem is "" then
     26                         set theLowItem to theCurrentItem
     27                         set theLowItemIndex to a
     28                     else if theCurrentItem comes before theLowItem then
     29                         set theLowItem to theCurrentItem
     30                         set theLowItemIndex to a
     31                     end if
     32                 end if
     33             end repeat
     34             set end of theSortedList to theLowItem
     35             set end of theIndexList to theLowItemIndex
     36         end repeat
     37     on error errMsg number errorNumber
     38         return {"An unknown error occurred:"} & theSortedList
     39     end try
     40     return theSortedList
     41 end sortList
     42 
     43 set destination to choose folder with prompt "Select a destination folder to save the albums to" default location (the path to the desktop folder as alias)
     44 set dest to ((the POSIX path of destination) as text) as POSIX file as text
     45 
     46 set r to display dialog "Do you want to export the originals or the edited versions?" buttons {"Originals", "Edited versions"} default button 1 with icon 2
     47 set orig to (button returned of r is "Originals")
     48 
     49 tell application "Photos"
     50     activate
     51     set unsorted to (name of albums)
     52 
     53 end tell
     54 
     55 set l to sortList(unsorted)
     56 
     57 set albNames to choose from list l with prompt "Select some albums" with multiple selections allowed
     58 if albNames is not false then -- not cancelled
     59     set progress description to "Photos export"
     60     set progress additional description to "Preparing..."
     61     set progress total steps to number of albNames
     62     repeat with tName in albNames
     63         set tFolder to dest & tName
     64         set progress additional description to "Processing: " & tFolder
     65         set progress completed steps to (progress completed steps + 1)
     66         my makeFolder(tFolder)
     67         if orig then
     68             tell application "Photos" to export (get media items of album tName) to (tFolder as alias) with using originals
     69         else
     70             tell application "Photos" to export (get media items of album tName) to (tFolder as alias) without using originals
     71         end if
     72     end repeat
     73     display dialog "Done" buttons {"Finish"} default button 1
     74 end if
     75 
     76 
     77 tell application "Finder"
     78     open (tFolder as alias)
     79     return {"Done: "} & albNames
     80 end tell
     81 
     82 on makeFolder(tPath)
     83     do shell script "mkdir -p " & quoted form of POSIX path of tPath
     84 end makeFolder
     85 EOF