Monday, December 9, 2019

#1 2019-12-03 11:00:35 pm

MitchBVI
Member
Registered: 2006-02-14
Posts: 156

Setting Path to Download latest file

I have been trying everything I can think to find the latest download file. Assume it s simple error on my part.

In the script that follows I have set the path to the source folder and  the option in the tell block  does not work

Applescript:


set SourceFolder to "Mackintosh HD:Users:petermitchell:Dropbox:Investing:"

tell application "Finder"
   
   sort (get files of folder SourceFolder) by creation date
   set latestfile to (item1 of result) as alias
   
end tell

.

Thanks

Peter

Offline

 

#2 2019-12-03 11:31:46 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 768

Re: Setting Path to Download latest file

just replace item1 with item 1


Model: MacBook Pro
macOS Mojave -- version 10.14.4
Safari -- version 12.1
Firefox -- version 70.0

Offline

 

#3 2019-12-04 11:55:53 am

MitchBVI
Member
Registered: 2006-02-14
Posts: 156

Re: Setting Path to Download latest file

Thank you very much that was a dumb mistake, however it did not fix the problem. This is the message I now get

error "Finder got an error: Can’t get folder \"Mackintosh HD:Users:petermitchell:Dropbox:Investing:\"." number -1728 from folder "Mackintosh HD:Users:petermitchell:Dropbox:Investing:"


Unlike my original post I have checked very carefully to make sure the address is correct.

If I choose the folder rather than code it in  it seems to work, seems because I am returning the oldest file rather than the newest so another  issue.

Applescript:


choose folder with prompt "Get latest Folder"
set sourcefolder to result
tell application "Finder"
   
   sort (get files of folder sourcefolder) by creation date
   set latestfile to (item 1 of result) as alias
   
end tell
display dialog latestfile as string

so I am still doing something incorrectly thanks again.

This is the code if I choose the folder

Applescript:


choose folder with prompt "Find latest folder"
set sourcefolder to result

set sourcefolder to result
tell application "Finder"
   
   sort (get files of folder sourcefolder) by creation date
   set latestfile to (item 1 of result) as alias
   
end tell
display dialog latestfile as string

Peter

Offline

 

#4 2019-12-04 12:51:41 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3673

Re: Setting Path to Download latest file

I ran your two scripts.
Both did the job flawlessly.

As choose folder returns an alias, the instruction supposed to sort the list of files may be shortened to:
sort (get files of folder sourcefolder) by creation date

In the second script the instruction
set sourcefolder to result
is repeated twice which is at least useless.

In your first message the pathname started with "Mackintosh HD" isn't it "Macintosh HD" ?

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 4 décembre 2019  19:51:33

Offline

 

#5 2019-12-04 02:53:40 pm

t.spoon
Member
From:: BFE, Massachusetts
Registered: 2013-01-13
Posts: 464

Re: Setting Path to Download latest file

I just wanted to add that if your goal is to "find the latest download file," then you don't want to use "Creation Date" for the sort, because creation dates on files can be preserved when downloading them, depending on how the download occured. That is, it's possible to download an old file and have it show up in the downloads folder with the old modification date, thus your script will return something else as the file with most recent "date created."

Maybe you never download things such that their creation date is preserved, but I'd recommend using the Finder's "Date Added" field, which should always return the most recent thing added to the folder.

Last edited by t.spoon (2019-12-04 02:53:48 pm)


Hackintosh built February, 2012 |  Mac OS Sierra
GIGABYTE GA-Z68X-UD3H-B3 | Core i5 2500k | 16 GB DDR3 | GIGABYTE Geforce 1050 TI 4GB
250 GB Samsung 850 EVO | 4 TB RAID
Dell Ultrasharp U3011 | Dell Ultrasharp 2007FPb

Offline

 

#6 2019-12-04 03:24:35 pm

MitchBVI
Member
Registered: 2006-02-14
Posts: 156

Re: Setting Path to Download latest file

Again what a dumb mistake. However  following t.spoon comment I checked the dictionary to be sure and there is no "Date Added" for Finder. Don't know why my original script always returned the first dated item. But when I combine the two instructions it works correctly.

Applescript:


set sourcefolder to "Macintosh HD:Users:petermitchell:dropbox:investing:"
set sourcefolder to result
tell application "Finder"
   set latestFile to last item of (sort (get files of folder sourcefolder) by creation date) as alias
end tell
display dialog latestFile as string

This now works thanks again.

Offline

 

#7 2019-12-04 07:05:48 pm

t.spoon
Member
From:: BFE, Massachusetts
Registered: 2013-01-13
Posts: 464

Re: Setting Path to Download latest file

"Date Added" was a relatively new addition to Finder, and as far as I know, Finder hadn't gotten any Applescript updates since the date "Date Added" was added. Applescript doesn't really know about Finder tabs either... Finder's dictionary is just outdated.

You can access "date added" with Applescript using "do shell script" with mdfind, it's the kMDItemDateAdded field. But if you're getting the behavior you want, I wouldn't worry about it.

I think Safari downloads show up with Date Added and Date Modified set to the time the download's complete? So if that's where all these files come from, it will probably work for you. My Downloads folder gets things from more sources, and some definitely preserve existing modification dates on the files.

- t.spoon


Hackintosh built February, 2012 |  Mac OS Sierra
GIGABYTE GA-Z68X-UD3H-B3 | Core i5 2500k | 16 GB DDR3 | GIGABYTE Geforce 1050 TI 4GB
250 GB Samsung 850 EVO | 4 TB RAID
Dell Ultrasharp U3011 | Dell Ultrasharp 2007FPb

Offline

 

#8 2019-12-04 07:44:19 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6065

Re: Setting Path to Download latest file

t.spoon wrote:

You can access "date added" with Applescript using "do shell script" with mdfind, it's the kMDItemDateAdded field.



With the usual proviso that the volume supports Spotlight searches and that the database is up-to-date.

You can also do it with my FileManagerLib:

Applescript:

use AppleScript version "2.5" -- macOS 10.11 or later
use scripting additions
use script "FileManagerLib" version "2.3.3"

date info for (choose file)
--> {added_date:date "Friday, 8 November 2019 at 7:03:53 pm", last_access_date:date "Friday, 8 November 2019 at 7:03:53 pm", creation_date:date "Wednesday, 31 October 2018 at 9:31:47 pm", modification_date:date "Wednesday, 31 October 2018 at 9:33:30 pm"}


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#9 2019-12-04 07:54:42 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6065

Re: Setting Path to Download latest file

Or to answer the original question:

Applescript:

use AppleScript version "2.5" -- macOS 10.11 or later
use scripting additions
use script "FileManagerLib" version "2.3.3"

set theFolder to (choose folder)
set theFiles to objects of theFolder
set latestFile to item -1 of (sort objects theFiles sorted property added property)


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)