Saturday, July 4, 2020

#1 2020-05-02 03:15:14 am

ldicroce
Member
Registered: 2017-11-25
Posts: 200

Behind the syntax of files of folder

Hi all,
I don't understand why if I interrogate the Finder to extract filenames from files located into a folder, it works. But if I copy this files into a list then I get try to retrieve the filename I get an  error.
See code below:

Applescript:

set theFolder to choose folder with prompt "Choose a folder"
-- get information about contents
tell application "Finder"
   get files of theFolder
   (item 1 of theFolder)'s class --> document file
   get displayed name of item 1 of theFolder --> a single filename
   get displayed name of files of theFolder --> a list of filenames
   set myList to (files of theFolder) -- this list is equivalent to "get files of theFolder" as structure and contents
   (item 1 of myList)'s class --> document file
   get displayed name of item 1 of myList --> OK: a single filename
   get displayed name of items of myList -- ERROR, Why?
   get displayed name of every item of myList -- ERROR, Why?
   get displayed name of files of myList -- ERROR, Why?
end tell

Thanks

Last edited by ldicroce (2020-05-02 03:23:40 am)

Offline

 

#2 2020-05-02 03:55:10 am

LookToWindward
Member
Registered: 2020-04-28
Posts: 14

Re: Behind the syntax of files of folder

At a guess the displayed name is not the file name. The displayed name is the name of the of file without its extension, try just name, not display name.

Hope this helps
Dave

Offline

 

#3 2020-05-02 04:09:38 am

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

Re: Behind the syntax of files of folder

My understanding is that you are facing what is described in Applescript User Guide:

Filter
Specifies all objects in a container that match a condition, or test, specified by a Boolean expression.
The filter form specifies application objects only. It cannot be used to filter the AppleScript objects list (page 112), record (page 118), or text (page 123). A term that uses the filter form is also known as a whose clause.
Note: You can use the words where or that as synonyms for whose.
A filter reference form can often be replaced by a repeat statement, or vice versa. For example, the following
script closes every TextEdit window that isn’t named "Old Report.rtf":



Always my understanding, when we ask directly to the Finder, we urge it to search in a structure which it owns.
When we encapsulate the structure in an AppleScript's list we introduce an extraneous level of abstraction which fools the Finder.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) samedi 2 mai 2020 12:09:29

Last edited by Yvan Koenig (2020-05-02 04:22:37 am)

Offline

 

#4 2020-05-02 04:12:43 am

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

Re: Behind the syntax of files of folder

LookToWindward wrote:

At a guess the displayed name is not the file name. The displayed name is the name of the of file without its extension, try just name, not display name.

Hope this helps
Dave



Wrong, the behavior is the same when the displayed name contains the extension as it does on my machine.
In fact, logically, it's the same for every asked property.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) samedi 2 mai 2020 12:12:38

Offline

 

#5 2020-05-02 04:15:02 am

ldicroce
Member
Registered: 2017-11-25
Posts: 200

Re: Behind the syntax of files of folder

You are right.  But this is not the point.
I know I can retrieve the display names from myList the using a loop. But why is not working when I want to get all  display names at once? But it work if I retrieve it using:
get displayed name of files of theFolder

The structure and the content of:
"get files of theFolder"
is exactly as that of the myList, which is:
{document file "A.pdf" of folder "PDFs Join" of folder "Desktop" of folder "ldicroce" of folder "Users" of startup disk, document file "B.pdf" of folder "PDFs Join" of folder "Desktop" of folder "ldicroce" of folder "Users" of startup disk, document file "C.pdf" of folder "PDFs Join" of folder "Desktop" of folder "ldicroce" of folder "Users" of startup disk}

Last edited by ldicroce (2020-05-02 04:15:47 am)

Offline

 

#6 2020-05-02 04:19:56 am

ldicroce
Member
Registered: 2017-11-25
Posts: 200

Re: Behind the syntax of files of folder

Yvan Koenig wrote:


Always y understanding, when we ask directly to the Finder, we urge it to search in a structure which it owns.
When we encapsulate the structure in an AppleScript's list we introduce an extraneous level of abstraction which fools the Finder.

This might explain it. Thanks.

Offline

 

#7 2020-05-02 04:24:23 am

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

Re: Behind the syntax of files of folder

@ldicroce

Thank you, I corrected my typo roll

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) samedi 2 mai 2020 12:24:17

Offline

 

#8 2020-05-02 07:45:56 am

Marc Anthony
Member
From:: Dallas, TX
Registered: 2006-04-27
Posts: 949

Re: Behind the syntax of files of folder

Hi. The underlying problem is known as dereferencing, and it's not Finder-specific. If you want to obtain multiple properties from an object en masse, do it while the reference is active.

Applescript:

tell application "Finder" to set {theFiles, theNames} to my (choose folder with prompt "Choose a folder")'s files's {it, displayed name}

Offline

 

#9 2020-05-02 10:27:57 am

ldicroce
Member
Registered: 2017-11-25
Posts: 200

Re: Behind the syntax of files of folder

Interesting line of code!
Although on my MacBook is a bit slow ...
Thanks Marc Anthony.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)