• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: finding newest file in a folder
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: finding newest file in a folder


  • Subject: Re: finding newest file in a folder
  • From: Christopher Nebel <email@hidden>
  • Date: Sun, 14 Mar 2004 13:04:48 -0800

On Mar 14, 2004, at 8:15 AM, Deivy Petrescu wrote:

At 5:06 PM +1100 3/14/04, John Cochrane wrote:

I am trying to get a reference to a file that I have just scanned in to a folder. ...

John, if you do not mind you can use a straight forward method, shell script.
This requires no repeat:

<script>
set d to POSIX path of (path to "docs" as Unicode text)
set AppleScript's text item delimiters to ASCII character 13
set k to text item 1 of (do shell script " ls -t -1 " & d) as Unicode text
set AppleScript's text item delimiters to {""}
display dialog k
</script>

If you're going to do this sort of thing, you should do it right. The "-1" is superfluous, since ls(1) is defined to output one item per line unless the device is a terminal, which "do shell script" isn't. Both coercions are also superfluous, since "POSIX path" is only accidentally a property of strings, and "do shell script" already returns Unicode text. There is no need to mess around with text item delimiters when what you want is lines, since "paragraph" elements will work just as well. Finally, the above doesn't quote the file path, so it will fail if the path contains spaces or other interesting punctuation. A more correct version would be this:

set d to POSIX path of (path to "docs")
set k to paragraph 1 of (do shell script "ls -t " & quoted form of d)
display dialog k

Of course, this returns a POSIX path; to convert it back into something useful outside the shell, say "POSIX file k".

Also, this and the more AppleScript-y versions mentioned all suffer from the same problem: they find the most recently modified *item*, which may not be a file. Correcting this in the Finder version merely requires changing "item" to "file". Correcting this in the shell version is less obvious; here's one way:

set k to paragraph 1 of (do shell script "ls -Ft " & quoted form of d & " | grep -v '/$')


--Chris Nebel
AppleScript Engineering
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.


  • Follow-Ups:
    • Re: finding newest file in a folder
      • From: steve harley <email@hidden>
    • Re: finding newest file in a folder
      • From: Deivy Petrescu <email@hidden>
References: 
 >finding newest file in a folder (From: John Cochrane <email@hidden>)
 >Re: finding newest file in a folder (From: Deivy Petrescu <email@hidden>)

  • Prev by Date: Re: What to do when strings don't match but should?
  • Next by Date: Re: "Save changes to script" when it terminates?
  • Previous by thread: Re: finding newest file in a folder
  • Next by thread: Re: finding newest file in a folder
  • Index(es):
    • Date
    • Thread