Hi gang, I’m new to scripting and happy to have found these boards.
I’m trying to write a script that changes the creation date of a file to the file’s content creation date (i.e., the “Content created” date from its Spotlight metadata, visible under Get Info → More Info).
After a little research I’ve come up with a script that uses the mdls shell to get a file’s content creation date, as follows:
set tFile to (choose file)
set tPath to quoted form of (POSIX path of tFile)
set tName to name of (info for tFile)
set MD to do shell script "mdls -name kMDItemContentCreationDate " & tPath
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "= ("
set tDates to text item 1 of MD
set AppleScript's text item delimiters to ")"
set tDates to (text item 1 of tDates) as text
set AppleScript's text item delimiters to tid
set dateInfo to text 30 thru -1 of tDates
display dialog "The Content Creation Date of the file \"" & tName & "\" is as follows:" & return & return & dateInfo with title "Content Creation Date Result"
At this point I’m looking to set the file’s creation date to the date returned in the above script (which is in the form yyyy-mm-dd hh:mm:ss +hhhh).
i was doing a similar task to yours; so i modified your original as this; i use the ‘mdls’/‘awk’/‘xargs’/‘date’ shell command to accomplish the iso to apple date xformation - and the ‘awk’ can be removed in favor of ‘-raw’ but i use it for other stuff too.
property metaDate : "kMDItemContentCreationDate" -- the spotlight metadata date item you want
set tFile to (choose file)
set tPath to quoted form of (POSIX path of tFile) -- this could be done 'inline'
tell application "System Events" to copy the (name of tFile) to tName -- replaces ''info'' command
-- set tName to name of (info for tFile) -- info has been deprecated
(* the rest is unnecessary
set MD to do shell script "mdls -name kMDItemContentCreationDate " & tPath
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "= ("
set tDates to text item 1 of MD
set AppleScript's text item delimiters to ")"
set tDates to (text item 1 of tDates) as text
set AppleScript's text item delimiters to tid
set dateInfo to text 30 thru -1 of tDates
*)
tell current application to copy (do shell script ("mdls -name " & metaDate & " " & tPath & " | awk -F ' ' '{print $3,$4};' | xargs -0 -I indate date -j -f '%Y-%m-%d %H:%M:%S' indate +'%x %r'")) to dateInfo
display dialog "The Content Creation Date of the file \"" & tName & "\" is as follows:" & return & return & dateInfo with title "Content Creation Date Result"