I've been using the Apple Automator App lately to help me keep my file structures and file names in order.
I've found a nifty script that outputs today's date in YYYY-MM-DD format and replaces the selected text. Which is cool, since my basic data structure for files and folders is YYYY-MM-DD-name-of-project.
Now, I'd like to be able to rename mentioned date to the the last modification's date in order to keep track of when I edited what. (essentially allowing me to just select the previous date, hit a keyboard shortcut to call for the service script and overwrite it.)
I've tried some approaches, but I can't seem to get it done.
Help would be very much appreciated! Thank you so much in advance. (I hope this is not a duplicate.)
Cheers!
In case you need that script I was talking about:
on todayISOformat()
set theDate to current date
set y to text -4 thru -1 of ("0000" & (year of theDate))
set m to text -2 thru -1 of ("00" & ((month of theDate) as integer))
set d to text -2 thru -1 of ("00" & (day of theDate))
return y & "-" & m & "-" & d
end todayISOformat
on run {input, parameters}
return todayISOformat()
end run
This script renames any selected file according these 2 rules:
YYYY-MM-DD-
it replaces this prefix with the date of the current modification date of the file.YYYY-MM-DD-
it puts the current modification date of the file in front of the file name.Folders are not considered.
tell application "Finder"
repeat with anItem in (get selection)
if class of anItem is not folder then
set {currentFileName, modificationDate} to {name, modification date} of anItem
set formattedModificationDate to my formatDate(modificationDate)
if text 11 of currentFileName is "-" then
set newFileName to formattedModificationDate & text 11 thru -1 of currentFileName
else
set newFileName to formattedModificationDate & "-" & currentFileName
end if
set name of contents of anItem to newFileName
end if
end repeat
end tell
on formatDate(aDate)
tell aDate to tell 100000000 + day * 1000000 + (its month) * 10000 + year as string ¬
to return text -4 thru -1 & "-" & text 4 thru 5 & "-" & text 2 thru 3
end formatDate
Update:
The script just checks for the 11th character to be a hyphen. If a check for the whole YYYY-MM-DD-
pattern is needed you could use this handler
on validateYYYYMMDD(aDateString) -- returns true on success
try
tell aDateString
(text 1 thru 4) as integer
(text 6 thru 7) as integer
(text 9 thru 10) as integer
return text 5 is "-" and text 8 is "-" and text 11 is "-"
end tell
on error
return false
end try
end validateYYYYMMDD
or in case you have installed SatImage.osax which provides regex search without shell calls
on validateYYYYMMDD(aDateString) -- returns true on success
try
find text "^\\d{4}-(\\d{2}-){2}" in aDateString with regexp
return true
on error
return false
end try
end validateYYYYMMDD
To use the handlers replace the line
if text 11 of currentFileName is "-" then
with
if my validateYYYYMMDD(currentFileName) then