Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Aragornii

macrumors 6502a
Original poster
Jun 25, 2010
521
148
I created an AppleScript to update metadata based on a filename. It works fine. I'd like to modify my script so it automatically loops through all files in a folder rather running it for each file. Any ideas how to do that?

For reference, here is my script:
AppleScript:
-- Set the file path of the video file
set videoFile to choose file with prompt "Choose a video file to update the 'date created' attribute for:"

-- Get the POSIX path of the video file
set videoFilePath to POSIX path of videoFile

-- Get the filename and extension of the video file
set {filename, filenameExtension} to {name, name extension} of (info for videoFilePath without size)

-- Extract the date from the filename
set AppleScript's text item delimiters to {"-", " ", ";", "."}
set filenameItems to text items of filename
set yearText to item 2 of filenameItems
set monthText to item 3 of filenameItems
set dayText to item 4 of filenameItems
set h to item 5 of filenameItems
set m to item 6 of filenameItems
set s to item 7 of filenameItems

-- Convert the date text to a date object
set newCreateDate to monthText & "/" & dayText & "/" & yearText & " " & h & ":" & m & ":" & s


-- Use the "SetFile" command-line tool to change the "date created" attribute
do shell script "/usr/bin/SetFile -d '" & (newCreateDate as string) & "' " & quoted form of videoFilePath

-- Display a dialog box to confirm the update
display dialog "The 'date created' attribute of the video file has been updated." buttons {"OK"} default button 1
 

bogdanw

macrumors 603
Mar 10, 2009
6,118
3,030
Something like this
Code:
set theSourceFolder to choose folder with prompt "Please select the folder containing the video files"
tell application "Finder"
    set theVideoFiles to the files of theSourceFolder
    repeat with currentVideoFile in theVideoFiles
        if name extension of currentFile is in {"mov", "mp4", "mkv", "avi"} then
            set videoFilePath to POSIX path of currentVideoFile
            -- Get the filename and extension of the video file
            set {filename, filenameExtension} to {name, name extension} of (info for videoFilePath without size)
            -- Extract the date from the filename
            set AppleScript's text item delimiters to {"-", " ", ";", "."}
            set filenameItems to text items of filename
            set yearText to item 2 of filenameItems
            set monthText to item 3 of filenameItems
            set dayText to item 4 of filenameItems
            set h to item 5 of filenameItems
            set m to item 6 of filenameItems
            set s to item 7 of filenameItems    
            -- Convert the date text to a date object
            set newCreateDate to monthText & "/" & dayText & "/" & yearText & " " & h & ":" & m & ":" & s
            -- Use the "SetFile" command-line tool to change the "date created" attribute
            do shell script "/usr/bin/SetFile -d '" & (newCreateDate as string) & "' " & quoted form of videoFilePath
        end if
    end repeat
end tell

-- Display a dialog box to confirm the update
display dialog "The 'date created' attribute of the video file has been updated." buttons {"OK"} default button 1

You could consider making it into a QuickAction, so that you can right-click on a folder, choose the quick action and perform the rename for all the video files. Example https://forums.macrumors.com/threads/batch-rename-of-files-to-folder-names.2365694/post-31627735
 

Aragornii

macrumors 6502a
Original poster
Jun 25, 2010
521
148
Thank you! it crashes on "POSIX path" with the following error

error "Can’t make «class docf» \"clip-2007-03-08 21;07;02.mp4\" of «class cfol» \"converted\" of «class cfol» \"Desktop\" of «class cfol» \"richard\" of «class cfol» \"Users\" of «class sdsk» of application \"Finder\" into the expected type." number -1700 from «class docf» "clip-2007-03-08 21;07;02.mp4" of «class cfol» "converted" of «class cfol» "Desktop" of «class cfol» "richard" of «class cfol» "Users" of «class sdsk»
 

bogdanw

macrumors 603
Mar 10, 2009
6,118
3,030
Sorry, there were two errors in the script above. This should work:
Code:
set theSourceFolder to choose folder with prompt "Please select the folder containing the video files"
tell application "Finder"
    set theVideoFiles to the files of theSourceFolder
    repeat with currentVideoFile in theVideoFiles
        if name extension of currentVideoFile is in {"mov", "mp4", "mkv", "avi"} then
            set videoFilePath to POSIX path of (currentVideoFile as string)
            -- Get the filename and extension of the video file
            set {filename, filenameExtension} to {name, name extension} of (info for videoFilePath without size)
            -- Extract the date from the filename
            set AppleScript's text item delimiters to {"-", " ", ";", "."}
            set filenameItems to text items of filename
            set yearText to item 2 of filenameItems
            set monthText to item 3 of filenameItems
            set dayText to item 4 of filenameItems
            set h to item 5 of filenameItems
            set m to item 6 of filenameItems
            set s to item 7 of filenameItems
            -- Convert the date text to a date object
            set newCreateDate to monthText & "/" & dayText & "/" & yearText & " " & h & ":" & m & ":" & s
            -- Use the "SetFile" command-line tool to change the "date created" attribute
            do shell script "/usr/bin/SetFile -d '" & (newCreateDate as string) & "' " & quoted form of videoFilePath
        end if
    end repeat
end tell
-- Display a dialog box to confirm the update
display dialog "The 'date created' attribute of the video file has been updated." buttons {"OK"} default button 1

If it doesn't, please post some examples of filenames you use so I can test it.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.