Trouble getting contents of folder

I’m trying to get a list of the contents of a folder, and am interested in the second to last file (the last is a .plist i’m not interested in, I just want the last jpg)

Currently I have this script:

set myFolder to "Macintosh HD:Users:Derek:Pictures:Photo Booth"
set myContents to {""}

tell application "Finder"
	set myContents to (entire contents of (choose folder)) as list
end tell

set numItems to count myContents
display dialog item (numItems - 1) of myContents as string

and this works fine - it gives me the 2nd to last file in the folder.

The problem, however, comes when I try to use the following code where “Choose Folder” is replaced with the file path myFolder

set myFolder to "Macintosh HD:Users:Derek:Pictures:Photo Booth"
set myContents to {""}

tell application "Finder"
	set myContents to (entire contents of (myFolder)) as list
end tell

set numItems to count myContents
display dialog item (numItems - 1) of myContents as string

It’s troublesome to me that the SAME folder works when selected using the “choose folder” option, but when the path is coded in, the script no longer yields the proper result!


Also, I’m very new to this, so if anyone knows a better/more efficient way to get the last .jpg file in a certain folder, let me know!

After more digging and trial and error, I found a solution!

Whether this is the most efficient and/or best I have no idea, but it gets the job done!

set theItems to {}
set myFolder to "Macintosh HD:Users:Derek:Pictures:Photo Booth:"

tell application "Finder"
	set theItems to (name of every file where name extension is "jpg") of folder myFolder as list
end tell

set numItems to count theItems
set picFile to myFolder & last item of theItems as string

display dialog picFile

tell application "Preview" to open file picFile

anyone is welcome to offer suggestions to help me improve my scripting!

Thanks,

Derek

Hi,

both coercions are not necessary.
The result of the where/whose filter is always a list, if no item matches the condition, the result is an empty list.
The file path is a string and the list item is also a string, so the concatenated result is always a string.

Hello.

I notice you use the term string :slight_smile: I will do so to, because text doesn’t sound much like a string

  • and writing utf16 string sounds to technical.

Best Regards

McUsr

Thanks for your replies,

I have modified my script slightly, and have noticed an issue with my logic. I would like to be able to have the list I create be by order of “last modified.” Is there any way to easily achieve this without altering my code too much?

I would keep using as text instead of as string. There isn’t a difference, but text sounds better.

The reason (I believe) why your script doesn’t work is because all folder paths need to end with a colon. That’s how AppleScript knows it’s a folder.

Hello.

It seems to me that you found the answer yourself here. It would be nice if you confined your self to one thread per topic which is much more important than one topic per thread :).

You are doing great, so I made a on liner of the Finder block for you. It is not very readable though.


property theItems : {}
set myFolder to "Macintosh HD:Users:Derek:Pictures:Photo Booth:"
tell application "Finder" to set picFile to myFolder &  (get name of last item in (sort (every file in folder myFolder whose name extension is "jpg") by modification date)) as text
tell application "Preview" to open file picFile

Best Regards

McUsr