selecting files by modification date

I’m trying to get a script to select (and then open) the most recent file in a particular folder. I thought something like this might work:
tell application “Finder”
activate
set view of folder “x” of startup disk to modification date
select file 1 of folder “x” of startup disk open selection end tell
The results are distinctly odd. A bias towards alphabetical order but sometimes the selection follows no obvious logic. I’d much appreciate any ideas!

Hi,
Using the “Finder” sort command is the easiest to me.

set fold_ref to (choose folder) 
tell application "Finder" 
 set item_list to (every item of fold_ref) 
 set item_list to (sort item_list by modification date) 
 set newest_item to item 1 of item_list 
 open newest_item 
end tell  

gl, Kel.

Hi Duncan,
Running AS version 1.3.7 here. I haven’t read about any of these commands and references being different in 1.5.5. After the line:
set item_list to (every item of fold_ref)
try adding ‘as list’. If item_list is not a list then you’ll get that error. Another way you could get that error is if the items of the list aren’t proper references for that command and I can’t see how that could happen. For instance, if you had a list of strings then it wouldn’t work.
Sorry I couldn’t help more, Kel.

: Hi,
: Using the “Finder” sort command is the easiest to me.
:
: set fold_ref to (choose folder) tell application “Finder”
: set item_list to (every item of fold_ref) set item_list to
: (sort item_list by modification date) set newest_item to item 1 of
: item_list open newest_item end tell
: gl, Kel.
Thanks very much, Kel.
It looks to be an elegant script but, probably because of something I’m doing stupidly, when I run it (with folder chosen) I get an error message: “Finder got an error: Invalid key form”, with “sort item_list by modification date” highlighted. Maybe it’s the version of Applescript I’m running, 1.5.5. Any thoughts?
All the best,
Duncan

Hi Duncan,
A reference to a folder ends with a colon (“:”). For instance:
folder “Macintosh HD:Stuff:”
if I had a folder named “Stuff” and it was located on my startup disk.
What I do when I have problems with syntax is try to isolate the problem and work my way up. Like I would start with:
set the_folder to (choose folder)
Then, copy the result from the result window and paste it here if i just wanted to use the reference:

tell app "Finder" 
set item_list to (every item of (paste the reference here)) 
end tell 
Then look at the result in the Script Editor. Then: 
tell app "Finder" 
set item_list to (every item of (paste the reference here)) 
set sorted_list to (sort item_list by modification date) 
end tell

Then, look at the result. Etc.
I think the colon was the problem plus your not setting the variable to a reference but a string. What was happening is this:
set fold_ref to “Stuff”
tell app “Finder”
set item_list to (every item of fold_ref)
here, you would get this:
{“S”, “t”, “u”, “f”, “f”}
Then, in the next line:
sort {“s”, “t”, “u”, “f”, “f”}
The command needs references not strings.
Hope this helps,
Kel.

I wrote this simple script to help you get references of files or folders:

try 
 display dialog "Would you like a path to a file or folder?" buttons {"File", "Folder", "Cancel"} default button ¬ 
  "Cancel" giving up after 600 
 set the_button to button returned of result 
 if the_button is "File" then 
  set the_path to (choose file with prompt "File path to clipboard:") as string 
 else if the_button is "Folder" then 
  set the_path to (choose folder with prompt "Folder path to clipboard:") as string 
 end if 
 set the clipboard to """ & the_path & """ 
on error err_message 
 display dialog err_message giving up after 10 
end try

It asks you to choose a file or folder. Then it pastes the path to the item onto the cliboard. Try not to just use the path but a reference like:
folder “Macintosh HD:Applications:iTunes:”
or:
alias “Macintosh HD:Applications:iTunes:”
or in the finder ‘tell’ block you can use “Finder” references:
folder “iTunes” of folder “Applications” of disk “Macintosh HD”
Hope this helps too.

Hi again Kel,
No luck. Same error message. I wonder if I’m failing to identify the folder properly. At first, where you have the line, set fold-ref to (choose folder), I was putting the name of the folder between the brackets, i.e. (“stuff”) – and I also tried “stuff” alone, dispensing with the brackets. Then it occurred that I should probably set out the path, as in “stuff” of startup disk, or, folder “stuff” of startup disk. But it definitely doesn’t like that.
Re your suggestion that possibly the items on the list not being proper references, I don’t think that’s the case. I’m running the script on its own, aimed — I hope — at a named target folder that has a bunch of files in it. I simply did a copy and paste of your sample script into Script Editor and inserted the folder name.
If at this point you figure you’ve given it your best, I certainly don’t blame you! I’ll keep tinkering.
All the best,
Dduncan

I’m trying the following code-

tell application "Finder"
   set home_path to path to "cusr" from user domain
   set fold1_ref to (folder "work" of folder "FAH" of folder home_path)
   set item_list to (every item of fold1_ref)
   set sorted_list to (sort item_list by modification date)
   set newest_item to item 1 of sorted_list
end tell
tell application "TextEdit"
   activate
   open newest_item
end tell

– upon build and run, the following error gets spewed at me:
Finder got an error: {document file “file.txt” of folder “work” of folder “FAH” of folder “michael” of folder “Users” of startup disk} doesn’t understand the sort message. (-1708)
That directory only has that one file in it (currently) and when I comment out the sort by modification date line, it works (TextEdit opens the file)
What am I doing wrong here?
: tell app “Finder” set item_list to
: (every item of (paste the reference here)) end tell Then look at the
: result in the Script Editor. Then: tell app “Finder” set
: item_list to (every item of (paste the reference here)) set sorted_list
: to (sort item_list by modification date) end tell Then, look at the
: result. Etc. I think the colon was the problem plus your not setting the
: variable to a reference but a string. What was happening is this: set
: fold_ref to “Stuff” tell app “Finder” set item_list
: to (every item of fold_ref) here, you would get this: {“S”,
: “t”, “u”, “f”, “f”} Then, in the
: next line: sort {“s”, “t”, “u”,
: “f”, “f”} The command needs references not strings.
: Hope this helps, Kel.

Hi Michael,
To this line add the ‘as list’ at the end:
set sorted_list to (sort item_list by modification date) as list
Your system may be different from mine and maybe it was returning a reference instead of a one item list in the line before this. On my computer:
set item_list to (every item of the_folder)
always sets item_list to a ‘list’ so you might check out what kind of result you are getting in the Script Editor up to that line.
Hope this helps, Kel.

: Hi Michael,
: To this line add the ‘as list’ at the end: set sorted_list to (sort item_list
: by modification date) as list
: Your system may be different from mine and maybe it was returning a reference
: instead of a one item list in the line before this. On my computer: set
: item_list to (every item of the_folder)
Here is the real problem- from the dictionary:
sort: (NOT AVAILABLE YET) Return the specified object(s) in a sorted list
sort reference – a list of finder objects to sort
by property – the property to sort the items by (name, index, date, etc.)
[Result: reference] – the sorted items in their new order
Seems that on OS X sort isn’t functional yet :frowning:

Booooo.
Sorry, Michael.

Hi Kel,
You’ve been busy since I last checked! Your first script is working beautifully now, thanks to you walking me through it — and making me realize how valuable the “the result” window is. Your second script, the one with the dialog box, is a beauty.
Thanks again,
— Duncan

: Here is the real problem- from the dictionary: sort: (NOT AVAILABLE YET)
: Return the specified object(s) in a sorted list
: sort reference – a list of finder objects to sort
: by property – the property to sort the items by (name, index, date, etc.)
: [Result: reference] – the sorted items in their new order
: Seems that on OS X sort isn’t functional yet :frowning:
If I understand your goal (to open the file with the latest modification date
using TextEdit), maybe this will work.

set targetFolder to choose folder
tell application "Finder" to set fileList to creation date of every file of targetFolder
set finalDate to item 1 of fileList
repeat with i in fileList
	if i is greater than finalDate then set finalDate to contents of i
end repeat
tell application "Finder" to set targetFile to every file of targetFolder whose creation date is finalDate
set targetFile to targetFile as alias
tell application "TextEdit"
	activate
	open targetFile
end tell

During testing with with OS 9.1, AppleScript 1.6 and Tex-Edit Plus, it
worked quite nicely. :slight_smile:
– Rob J

: If I understand your goal (to open the file with the latest modification date
: using TextEdit), maybe this will work.
: – Begin Script –
: set targetFolder to choose folder tell application “Finder” to set
: FileList to creation date of every file of targetFolder
: set finalDate to item 1 of FileList repeat with i in FileList if i is
: greater than finalDate then set finalDate to contents of i end repeat
: tell application “Finder” to set targetFile to every file of
: targetFolder whose creation date is finalDate set targetFile to
: targetFile as alias
: tell application “TextEdit” activate open targetFile end tell
: – End Script –
: During testing with with OS 9.1, AppleScript 1.6 and Tex-Edit Plus, it
: worked quite nicely.
[Sigh]
Of course there are a couple of problems with the script above.
Problem #1 is that I used creation date instead of modification date.
Problem #2 is that line 2 will fail if the target folder contains only one
file. Here’s the modified version to overcome these issues:
– Begin Script v1.0.1 –

set targetFolder to choose folder 
tell application "Finder" to set fileList to modification date of every file of targetFolder as list
set finalDate to item 1 of fileList 
repeat with i in fileList 
if i is greater than finalDate then set finalDate to contents of i 
end repeat
tell application "Finder" to set targetFile to every file of targetFolder whose modification date is finalDate 
set targetFile to targetFile as alias
tell application "TextEdit" 
activate 
open targetFile 
end tell

Sorry 'bout that!
Rob J

What I actually ended up doing, since all I need is the most recently modified log file in the directory is using the shell.
ls -t |grep logfile |head -1
-=- That solves another issue, too- If a file is added to a directory or deleted from the directory using something other than the Finder, the Finder doesn’t seem to always know about it until the directory is actually opened in the finder- causing it to give false (old) information to the script asking for the info.
Either Apple needs to fix that or provide a command to tell the finder to update its database for that directory.
So in the mean time, i have to get the info I’m looking for through do shell script “blah”
I’d rather do it through AppleScript, that seems cleaner if possible- but until sort is fixed for OS X and I can be assures that the Finder will give accurate info, I have to have AppleScript call the unix side.

: What I actually ended up doing, since all I need is the most recently
: modified log file in the directory is using the shell.
: ls -t |grep logfile |head -1
: -=- That solves another issue, too- If a file is added to a directory or
: deleted from the directory using something other than the Finder, the
: Finder doesn’t seem to always know about it until the directory is
: actually opened in the finder- causing it to give false (old) information
: to the script asking for the info.
: Either Apple needs to fix that or provide a command to tell the finder to
: update its database for that directory.
: So in the mean time, i have to get the info I’m looking for through do shell
: script “blah”
: I’d rather do it through AppleScript, that seems cleaner if possible- but
: until sort is fixed for OS X and I can be assures that the Finder will
: give accurate info, I have to have AppleScript call the unix side.
I don’t have OS X, so I don’t know a lot about it but…
Finder, under OS 9, has an update command. Here’s the entry from the dictionary:
update: Update the display of the specified object(s) to match their on-disk representation
update reference – the item to update
So, in my example script, the first Finder tell block would change to:
– Begin Modified Code –
tell application “Finder”
update targetFolder – targetFolder is an alias in this case
set FileList to modification date of every file of targetFolder as list
end tell
– End Modified Code –
I’ve used the update command on a couple of occasions where a script
was moving along in a quick progression of steps and the Finder
wasn’t keeping up and it solved problems similar to what you describe.
Rob J