set ifolder to "path:to:folder" as alias
tell application "Finder" to set allitems to every item of ifolder whose name extension is "zip"
set a to item 1 of allitems
set theinfo to creation date of (info for a as alias) as text
–>>“Thursday, October 15, 2009 7:39:44 AM”
How do i compare the dates and time?
Is there an alternative way of doing this?
Will
The shortest code is probably just to have Finder sort them and take the first or last item from the list (yes, item -1 means the last item):
set ifolder to aliasToSomeFolder
tell application "Finder" to set allitems to sort (every item of ifolder whose name extension is "zip") by creation date -- most recent first
set a to item 1 of allitems
set theinfo to creation date of (info for a as alias) as text
I have a vague impression that some people have had problems with Finder’s sort though. You can always do the selection manually:
set ifolder to aliasToSomeFolder
tell application "Finder" to set allitems to every item of ifolder whose name extension is "zip"
set mostrecentdate to missing value
set mostrecent to missing value
repeat with a in allitems
using terms from application "Finder"
set adate to creation date of a
end using terms from
if mostrecentdate is missing value or ¬
mostrecentdate < adate then
set mostrecent to a
set mostrecentdate to adate
end if
end repeat
set a to mostrecent
set theinfo to creation date of (info for a as alias) as text
The sort will always do more comparisons than the manual picking (sort is O(N log N) or maybe O(N^2) comparisons; manual picking is O(N)). But the select+sort is O(1) AppleEvents and the manual picking is O(N) AppleEvents. The overhead of the AppleEvents probably swamps the higher algorithmic complexity of the sort (though, theoretically if there are enough files, the manual method would be faster).
set ifolder to "path:to:folder" as alias
tell application "Finder"
set allitems to sort (every item of ifolder whose name extension is "zip") by creation date
end tell
set a to item 1 of allitems
if mostrecentdate is missing value or ¬
mostrecentdate < adate then
set mostrecent to a
set mostrecentdate to adate
end if
This was what I was looking for. I see, Applescript understands how to compare dates/times.
Its my downloads folder. I have 10 to 20 files at a time. I move them all to the proper folder when the number grows. So I hope, I can use any method. Thanks for the nice info, though.
using terms from application "Finder"
set adate to creation date of a
end using terms from
Finder often fails when the items it deals with are numerous and required to be processed sequentially.
May I know, why did you choose the above instead of
set adate to creation date of (info for a as alias)
I do not do much Finder scripting, so I do not really keep track of what is reliable and what is not. I went with Finder because the value of a will already be a Finder reference object and I knew that I could directly ask for its creation date property. If you have experienced problems with this particular bit of Finder scripting, then feel free to go with your alternative.
It does seem strange to me though that Finder would not be able to reliably provide individual item’s creation dates though. Such doubt would make me also reconsider using Finder to do the initial filter-on-extension, too.
Perhaps System Events would prove more reliable:
set aliasToSomeFolder to alias POSIX file result
set ifolder to aliasToSomeFolder
tell application "System Events" to set allitems to every disk item of folder (POSIX path of ifolder) whose name extension is "zip"
set mostrecentdate to missing value
set mostrecent to missing value
repeat with a in allitems
using terms from application "System Events"
set adate to creation date of a
end using terms from
if mostrecentdate is missing value or ¬
mostrecentdate < adate then
set mostrecent to contents of a
set mostrecentdate to adate
end if
end repeat
set a to mostrecent
set theinfo to creation date of (info for a as alias) as text
I have come across some people advocating System Events in place of Finder for typical file operations. I have never given the issue much attention though, because I do most file manipulation stuff in Terminal either manually or by writing a UNIX-y script (sh, bash, Perl, etc.).