1

I'm trying to export photos from iPhone using mac os Photos app.

The problem is that there is no way of structuring them in a format that will allow to easily access them by date.

Ideally I'd like to have filenames in with timestamp of a photo, something like: 2017-10-03-12-05 but it is not possible with the Photos app.

I would also tolerate some folders structure with date information, but I haven't found any suitable way.

The only way I found so far is by using subfolder format with Moment Name which creates subfolders with name of the location and date

enter image description here Here is the example of folder name generated in this way:

Schloßpark Nymphenburg - Munich, Bavaria, October 19, 2017

However such name is almost unusable because there is almost no way to filter all photos let's say from October 2017. I would really appreciate name like this:

2017-10-19 - Schloßpark Nymphenburg - Munich, Bavaria

which would allow easily sorting and structuring my photos.

Q: Does someone know any way of exporting photos in more usable names? I'd appreciate any way that will at least improve the way I'm exporting now.

1

Whilst this isn't the solution you were asking for, an equivalent outcome could be achieved if you were willing to employ a bit of AppleScript. AppleScript could be used to automate the exporting process from Photos and then rename the files that are created in Finder. But, honestly, I would suggest the exporting be done just as you're doing it now—from within the application—and then use AppleScript to collectively rename/move them afterwards.

    tell application "System Events"
        set ExportPath to POSIX file "/path/to/exported/photos" as alias
        set ExportedPhotos to every file in ExportPath
    end tell

    repeat with PhotoFile in ExportedPhotos
        tell application "System Events" to set Filename to name of PhotoFile
            --> e.g. "Schloßpark Nymphenburg - Munich, Bavaria, October 19, 2017.jpg"

        set [file_extension, theYear, theDay, theMonth] to reverse of words of Filename
            --> {"jpg", "2017", "19", "October", "Bavaria", "Munich", "Nymphenburg", "Schloßpark"}

        set AppleScript's text item delimiters to " "
        set PhotoDate to date ({theDay, theMonth, theYear, "12:00:00 AM"} as text)
            --> date "Thursday, 19 October 2017 at 12:00:00"

        set AppleScript's text item delimiters to "-"
        set {theYear, theMonth, theDay} to {year of PhotoDate as text, ¬
            text -2 thru -1 of ("0" & (month of PhotoDate) * 1), ¬
            text -2 thru -1 of ("0" & day of PhotoDate)}
            --> {"2017", "10", "19"}

        set isoDate to result as text
            --> "2017-10-19"

        set AppleScript's text item delimiters to ", " & month of PhotoDate
        first text item of Filename
            --> "Schloßpark Nymphenburg - Munich, Bavaria"

        set Filename to [isoDate, " - ", result, ".", file_extension] as text
            --> "2017-10-19 - Schloßpark Nymphenburg - Munich, Bavaria.jpg"

        tell application "System Events" to set the name of PhotoFile to Filename
    end repeat

You could also choose to format the date portion of the filename using the bash function date called from AppleScript with a do shell script command, which does reduce the number of lines of code overall.

    do shell script "date -j -f '%B %d, %Y' 'October 19, 2017' '+%F'"
        --> "2017-10-19"

I've pasted this shorter version of the script below for completeness.

It's worth saying that this code only works if the exported filenames are of a consistent pattern in line with the example you provided. Specifically, the exported filename needs to end in a date that occupies the last three words of the filename; and any text before it needs to have a comma and a space separating it from the date portion. Of course, the code can be adjusted accordingly if a different naming pattern is chosen, but it's best to choose one and stick with it.

If you need to create a directory hierarchy based on dates, this extra portion of script will create nested folders %Year/%Month/%Day/ and move the photo into the appropriate directory:

    -- Continuing the script above from the following line
    -- (the "[to]" is now omitted to create a "tell" block)
    tell application "System Events" [to]
        set the name of PhotoFile to Filename

        set NestedFolders to [ExportPath, theYear, theMonth, theDay]

        repeat until number of NestedFolders is 1
            set [This, NestedFolders] to [item 1, rest] of NestedFolders
            set Child to first item of NestedFolders

            if not (exists folder Child of This) then
                set first item of NestedFolders to ¬
                    (make new folder at folder (POSIX path of This) with properties {name:Child})
            else
                set first item of NestedFolders to folder Child of This
            end if
        end repeat

        move (alias Filename in ExportPath) to POSIX path of (first item of NestedFolders)
    end tell

Now all of your exported photos will be renamed as per your guideline above, and organised in date-based directory hierarchies. Again, this extra portion of code could have been achieved using some do shell script commands (bash actually has a ready-made function for creating nested folders easily).

As a final thought, if you wanted to automate the whole process, you could adapt this script to a folder action, and attach it to the folder into which your photos are exported. This means that, whenever Finder detects new photos added to the folder, it will automatically run the folder action script for you. Therefore, as soon as the photos are exported, they will immediately get renamed and moved accordingly.

Here's the first part of the script again, this time using a shell command to reformat the date portion of the filename:

    tell application "Finder"
        set PhotoFile to file "Schloßpark Nymphenburg - Munich, Bavaria, October 19, 2017.png" in desktop as alias
        set Filename to name of PhotoFile
            --> e.g. "Schloßpark Nymphenburg - Munich, Bavaria, October 19, 2017.jpg"
        set Extension to name extension of PhotoFile
    end tell

    set AppleScript's text item delimiters to {", ", "." & Extension}
    text items -3 thru -2 of Filename as text
        --> "October 19, 2017"

    do shell script "date -j -f '%B %d, %Y' " & quoted form of result & " '+%F'"
    set isoDate to result
        --> "2017-10-19"

    text items 1 thru -4 of Filename as text
        --> "Schloßpark Nymphenburg - Munich, Bavaria"

    set Filename to [isoDate, " - ", result, ".", Extension] as text
        --> "2017-10-19 - Schloßpark Nymphenburg - Munich, Bavaria.jpg"

    tell application "Finder" to set the name of PhotoFile to Filename

As you can see, it's quite a bit shorter in terms of the number of lines of code, though I couldn't honestly tell you which will be faster or more robust.

  • Wow. Thank you. I'll try to understand and use this script. By the way you can organize the code and put it on GitHub so everyone could use it as a lib/script. – Oleksandr Shpota Dec 4 '17 at 17:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .