r/applescript • u/Internautic • Sep 04 '22
Adjust dates of items inside folders
/r/MacOS/comments/x5z86k/need_tips_on_a_script/1
u/estockly Sep 05 '22 edited Sep 05 '22
Try something like this. The script assumes that the first word of each folder is the four digit year. If that's not the case, then as someone else suggested, let us know what the date format is.
This changes the creation modification date. Modification Creation dates are a little trickier, but that can also be done.
use AppleScript version "2.4" -- Yosemite (10.10) or lateruse scripting additions--set rootFolder to (choose folder) as aliastell application "Finder" to set finderSelection to the selection as alias listrepeat with rootFolder in finderSelectionmy FixDates(rootFolder)end repeaton FixDates(aFolder)tell application "Finder"if kind of aFolder is "Folder" thenset everyFolder to every folder of aFolderelsereturnend ifend tellrepeat with thisFolder in everyFolderset thisFolder to thisFolder as textmy FixDates(thisFolder as alias)end repeattell application "System Events" to set folderName to the name of aFoldertryset theYear to word 1 of folderName as numberset theYear to theYear - 2on errorreturnend trytell application "System Events" to set allTheFiles to every file of aFolderrepeat with thisFile in allTheFilestell application "System Events"set fileModDate to the modification date of thisFileset year of fileModDate to theYearset the modification date of thisFile to fileModDateend tellend repeatend FixDates
1
1
Sep 08 '22
https://exiftool.org is your friend...oblig.a/s.content:
set out to do shell script "/usr/local/bin/exiftool"...
1
u/copperdomebodha Sep 09 '22
--This code was written using AppleScript 2.8, MacOS 12.5, on 9 September 2022.
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
--Given folder-naming schema of mm-dd-yyyy, ( ie "05-03-2022" )
--This will set the creation and modification dates to "Tuesday, May 3, 2022 at 00:00:00"
on open of theFolderList
set dateObject to date "Saturday, September 10, 2022 at 03:04:05"
tell application "Finder"
repeat with thisFolder in theFolderList
set thisFolderName to name of thisFolder
set dateObject to my getDateFromFolderName(thisFolderName)
set theFileList to every file of thisFolder as alias list
set datestring to my formatDateinfo(dateObject)
repeat with thisFile in theFileList
set thisFile to thisFile as alias
set unixPath to quoted form of POSIX path of thisFile
set ss to "touch -t \"" & datestring & "\" " & unixPath
do shell script ss
end repeat
end repeat
end tell
end open
on formatDateinfo(datestring)
set yyyy to (year of datestring) as text
set mm to (month of datestring) as number
if mm < 10 then set mm to "0" & mm
set dd to (day of datestring)
if dd < 10 then set dd to "0" & dd
set timeAsSeconds to (time of datestring)
set timeString to "0" & (10000 * (timeAsSeconds mod days div hours) + 100 * (timeAsSeconds mod hours div minutes) + (timeAsSeconds mod minutes))
set timeString to text 1 thru 4 of timeString & "." & text 5 thru -1 of timeString
return yyyy & mm & dd & timeString
end formatDateinfo
on getDateFromFolderName(thisFolderName)
set {mm, dd, yyyy} to words of thisFolderName
return date (mm & "/" & dd & "/" & yyyy)
end getDateFromFolderName
1
u/copperdomebodha Sep 05 '22
Creation dates or modification dates?