SOLVED AppleScript to Edit Photo Library File Meta Data

Joined
Jul 3, 2022
Messages
9
Reaction score
0
I am attempting to write a script that will more easily allow me to change some meta data for files within the Apple Photos Library without requiring file export. In doing so, I have hit a snag that I don't seem to be able to overcome. I've gotten a lot of it done, but I keep getting a problem when I try to set the year data. Just wondering if anyone can turn me on to a solution. I am hoping to get this accomplished because I think other users may find it useful.

Here's the code so far:


AppleScript:
(*

This AppleScript allows changes to Photos Library files meta data without requiring export.

I want an AppleScript that when activated will sequentially display photos together with the photos’ info pallet that are located in an album titled —Working—. The script should then wait for the user to select possible changes of the file’s meta data in this order: filename, title, adjust date/time? It should offer the user selections to enter data if the needs to be changed or select no change or select a recommended change. I want the AppleScript to edit meta data for files contained in the Apple Photos Library without needing export. For example: I have a photo file with the name '2022-10-13 ~Danny.jpg It has no title. When I look at the photo, I can easily see that the month should be May and not October. So I want the script to step through allowing me to do one of three possible selections, being make no changes, make a recommended change or make an entered change in three items in succession, being first the filename, second the title, and third the adjusted date/time. So for the example photo I would first type in and select ‘2022-05-13 ~Danny’ (extension doesn’t change), then select recommended title of May ’13, 2022 - Danny’ which the script should recommend based on data available from the filename, and third select month 05 for the adjusted date/time as recommended by the data available from the filename.

*)

tell application "Photos"
    -- Ensure the working album exists
    set albumName to "--Working--"
    try
        set workingAlbum to album albumName
    on error
        display dialog "Album '" & albumName & "' not found." buttons {"OK"} default button "OK"
        return
    end try
    
    -- Process each photo in the album
    set photoList to media items of workingAlbum
    repeat with aPhoto in photoList
        -- Extract the filename
        set originalFilename to filename of aPhoto
        display dialog "Processing file: " & originalFilename buttons {"OK"} default button "OK"
        
        set dateString to "" -- Initialize dateString to avoid referencing it before definition
        try
            -- Extract the date from the filename
            set dateString to my extractDateFromFilename(originalFilename)
            display dialog "Extracted date: " & dateString buttons {"OK"} default button "OK"
            
            -- Convert date to desired format
            set reformattedDate to my convertDateToText(dateString)
            display dialog "Reformatted date: " & reformattedDate buttons {"OK"} default button "OK"
            
            -- Set the reformatted date as the title
            set title of aPhoto to reformattedDate
            display dialog "Updated title to: " & reformattedDate buttons {"OK"} default button "OK"
        on error errMsg
            -- Log errors for debugging
            display dialog "Error processing file: " & originalFilename & return & "Details: " & errMsg & return & "Extracted date: " & dateString buttons {"OK"} default button "OK"
        end try
    end repeat
end tell

-- Extracts the date portion from a filename in the format YYYY-MM-DD
on extractDateFromFilename(filename)
    -- Regular expression to extract the first date-like pattern (YYYY-MM-DD) from the filename
    set datePattern to "\\d{4}-\\d{2}-\\d{2}"
    set datePart to ""
    
    -- Use do shell script with grep to extract the date
    try
        set datePart to do shell script "echo " & quoted form of filename & " | grep -oE '" & datePattern & "'"
        
        -- Check if a valid date was found
        if datePart is not "" then
            return datePart
        else
            error "Date not found in filename: " & filename
        end if
    on error
        error "Error extracting date from filename: " & filename
    end try
end extractDateFromFilename

-- Converts a date in YYYY-MM-DD format to Month Day, Year
on convertDateToText(dateString)
    display dialog "Starting conversion for date: " & dateString buttons {"OK"} default button "OK"
    set dateParts to my splitText(dateString, "-")
    set year to item 1 of dateParts
    set monthText to item 2 of dateParts
    set day to item 3 of dateParts as integer
    
    -- Debug extracted parts
    display dialog "Year: " & year & ", Month: " & monthText & ", Day: " & day buttons {"OK"} default button "OK"
    
    -- Ensure that the year is a valid number (convert to integer)
    try
        set year to (year as number) -- Force year to be treated as a number
    on error
        error "Invalid year: " & year
    end try
    
    -- Validate monthText for valid month numbers
    if (monthText is not in {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"}) then
        error "Invalid month: " & monthText
    end if
    
    -- Validate the day is within range
    if (day < 1 or day > 31) then
        error "Invalid day: " & day
    end if
    
    -- Convert month number to name
    set monthNames to {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}
    set monthName to item (monthText as integer) of monthNames
    
    return monthName & " " & day & ", " & year
end convertDateToText

-- Splits text by a delimiter
on splitText(textToSplit, delimiter)
    set oldDelims to AppleScript's text item delimiters
    set AppleScript's text item delimiters to delimiter
    set textItems to text items of textToSplit
    set AppleScript's text item delimiters to oldDelims
    return textItems
end splitText
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top