Search code examples
applescriptworkfloworganization

Applescript: Highlight most recent file from group


I'd like to know how you'd go about creating an action where you could highlight a group of files and get the modification date from them, then have it highlight/select/label the file with the most recent date.

UPDATE: I want to do it on Applescript because I've gotten further in that. Here's what I have so far

set dateList to {}
tell application "Finder"
    set inputList to get selection
    repeat with i from 1 to count (inputList)
        set end of dateList to get modification date of item i of inputList
    end repeat
end tell

dateList

--Compare section...

set boolList to {}
set j to 1
repeat with i from 1 to count (dateList)
    if i is (count (dateList)) then
        set j to 0
    end if
    set end of boolList to item i of dateList > item (i + j) of dateList
end repeat

boolList

Solution

  • Looking at your existing applescript code this should sort any files you've selected by last modified date and return the latest result into a dialog box for you:

    set dateList to {}
    tell application "Finder"
        set inputList to get selection
        repeat with i from 1 to count (inputList)
            set end of dateList to get modification date of item i of inputList
        end repeat
    end tell
    
    --Compare section...
    
    set modDate to item 1 of dateList
    repeat with i from 1 to count (inputList)
        if dateList's item i > modDate then
            set modDate to dateList's item i
            set theResult to displayed name of item i of inputList
            set theResultDate to item i of dateList
        end if
    end repeat
    
    --Display Result…
    
    display alert "Most recently modified file in selection:" message "" & theResult & "
    " & theResultDate