I need a scipt that looks into a predefined folder. For this we could assume the “folder” was my dekstop. I need the script to upload the NEWEST file on the desktop to a website via Fetch. But my problem is getting this “newest” item. I searched the forum & saw some references to “the newest file” - but the threads didn’t help me.
I’d like to test said script by substituting “copy” or “move” or “rename” instead of the uploading. But this shouldn’t be tough to swap out that single command in the end. I just think testing will be faster with another command in place. To me the hard part is actually having the script identify this “Newest File” - not performing whatever action I need to perform.
set desk_top to path to desktop as Unicode text
tell application "Finder"
set desk_all to (every file of folder desk_top)
set a to desk_all's item 1
repeat with b in desk_all
if (b's creation date) > (a's creation date) then set a to b
end repeat
end tell
Ciao, this might help you to get the newest file of a folder to be specified by you:
set myFolder to alias "Path:to:your:folder:"
-- or set myFolder to choose folder
tell application "Finder"
set folderContents to every file of myFolder
set newestFile to item 1 of folderContents as alias
repeat with aFile in folderContents
if modification date of aFile > modification date of newestFile then
set newestFile to aFile as alias
end if
end repeat
-- do something with newestFile
end tell
Saluti
Farid
PS: while I was still fiddling around with the ScriptEditor Craig had already posted a solution
Where exactly does it “return” the newest file to? I ran the script, it runs w/no problems, but it displays no text in the result window at the bottom of the script pane. Should it?
Thanks.
here an other attempt - maybe (probably?) faster, when searching in larger directories:
set theFolder to path to desktop
set thePath to POSIX path of theFolder
set sortedItems to (do shell script "ls -t " & quoted form of thePath)
tell application "Finder"
repeat with theFile in paragraphs of sortedItems
if class of item theFile of desktop is not folder then
exit repeat
end if
end repeat
end tell
set newestFile to (thePath & theFile) as POSIX file
I had another idea for a solution avoiding repeat loop & Finder:
This version ignores aliases:
set theFolder to POSIX path of (path to desktop)
set newFile to theFolder & (do shell script "ls -tF " & quoted form of theFolder & " | grep -v -e'[/%|@]$' | head -n1")
try
set result to alias ((newFile as POSIX file) as string)
on error
set result to alias (((text 1 thru -2 of newFile as string) as POSIX file) as string)
end try
This version resolves aliases:
set theFolder to POSIX path of (path to desktop)
set newFile to theFolder & (do shell script "ls -tF " & quoted form of theFolder & " | grep -v -e'[/%|]$' | head -n1")
try
set result to alias ((newFile as POSIX file) as string)
on error
set result to alias (((text 1 thru -2 of newFile as string) as POSIX file) as string)
end try
or if executables and aliases can be omitted:
set theFolder to POSIX path of (path to desktop)
set newFile to theFolder & (do shell script "ls -tF " & quoted form of theFolder & " | grep -v -e'[/%|@*]$' | head -n1")
set result to alias ((newFile as POSIX file) as string)
NOTE: This solutions fail when the newest file has a file extension ending with one of these special characters:
/ % |@ *
I suggest to use the Finder’s sort command,
it avoids any looping
regards
Stefan
set theFolder to path to desktop
tell application "Finder"
set fileList to every document file of theFolder -- or which class you prefer
sort fileList by modification date -- or creation date
end tell
set newest_file to last item of fileList -- last item is newest
This script does not work for me. It results the last document (in alphabetical order).
This little modification fixed it here:
set theFolder to path to desktop
tell application "Finder"
set fileList to every document file of theFolder -- or which class you prefer
set newest_file to first item of (sort fileList by modification date) -- or creation date
end tell
But for use in large (1000+ items) folders it’s significantly slower than a shell script (ls) solution