Simon Heimbuchner
Simon Heimbuchner

Reputation: 83

Applescript: Get modification date of selected file and output as text

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

Upvotes: 0

Views: 2291

Answers (3)

vadian
vadian

Reputation: 285069

This script renames any selected file according these 2 rules:

  1. If the file name starts with YYYY-MM-DD- it replaces this prefix with the date of the current modification date of the file.
  2. If the file does not start with 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

Upvotes: 2

dj bazzie wazzie
dj bazzie wazzie

Reputation: 3542

You can use «class isot» to convert a date to an iso datetime string. You only need to get characters 1 thru 10 from it to get only the iso date string.

tell application "Finder"
    repeat with anItem in (get selection)
        if class of anItem is not folder then
            set {fileName, modificationDate} to {name, modification date} of anItem
            set isoDate to text 1 thru 10 of (modificationDate as «class isot» as string)
            if text 11 of fileName is "-" then set fileName to text 11 thru -1 of fileName
            set newFileName to isoDate & "-" & fileName
            set name of contents of anItem to newFileName
        end if
    end repeat
end tell

Upvotes: 1

pbell
pbell

Reputation: 3095

Exemple bellow select a file and get the last modification's date of that file.

Of course you can use again your routine to format the date as you want :

set MyFile to choose file
tell application "Finder" to set DateUpdate to modification date of MyFile

set StringToday to todayISOformat(current date)-- get the string value of current date
set StringModif to todayISOformat(DateUpdate)-- get the string value of modification's date

on todayISOformat(LDate)
set y to text -4 thru -1 of ("0000" & (year of LDate))
set m to text -2 thru -1 of ("00" & ((month of LDate) as integer))
set d to text -2 thru -1 of ("00" & (day of LDate))
return y & "-" & m & "-" & d
end todayISOformat

Upvotes: 0

Related Questions