Question

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
Was it helpful?

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

OTHER TIPS

Dick got it, but I just fixed something and made it so it labels the file instead of a popup.

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 theResult to item 1 of inputList as alias
set theResultDate to item 1 of dateList
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 item i of inputList as alias
        set theResultDate to item i of dateList
    end if
end repeat

--Display Result…

--display alert "Most recently modified file in selection:" message "" & theResult & "
--" & theResultDate
tell application "Finder" to set label index of (theResult as alias) to 6

This will label it green, if you want a different color fiddle around with the index number 1-8, they're apparently not in order. Finder is also apparently smart enough to not count the selections in other open windows.

Thanks!

And finally, to make it useful as a right-click item, open Automator, make a Service, select at the top to use this on files/folders, drag Run Applescript in there, paste script, save. Now it will be available on right click. One downside is it seems the files need to stay selected until something is labeled. So no clicking while it's working.

You are making it more complicated than it needs to be:

tell application "Finder" to reveal item 1 of (sort (get selection) by modification date)

There is a bug in 10.7 and 10.8 that can make all of the suggested scripts almost unusable depending on the way they are run. If you open a new Finder window and select some files, tell application "Finder" to selection returns files selected in some window behind the frontmost window (or an empty list).

One workaround is to switch focus to another application and back:

activate app "SystemUIServer"
tell application "Finder"
    activate
    set label index of item 1 of (sort selection by modification date) to 6
end tell

You could also create an Automator service with a Run AppleScript action like this:

on run {input, parameters}
    tell application "Finder"
        sort (input as alias list) by modification date
        set label index of item 1 of result to 6
    end tell
end run
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top