"The Folders" in Metadata Lib

The syntax for search in Metadata Lib is:

set theFolder to choose folder
set theMetadata to perform search in folders {theFolder} predicate string "kMDItemFSName ENDSWITH[c] '.txt'"

and the definition of “in folders” is:
A list of files, aliases, POSIX paths or NSURLs of folders to search in.

If I run this script, everything is fine:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
use script "Metadata Lib"
set MyDoi to "10.1038/ng.1009"
set theFolders to {alias "MacBook HD:Users:ldicroce:Desktop:for_ERC:Single Cell:"}
set {a1, a2, b1, b2, c2} to {"kMDItemDescription", MyDoi, "kMDItemFinderComment", MyDoi, ".pdf"}

set theMetadata to perform search in folders {theFolders} predicate string "(%K CONTAINS[c] %@ OR %K CONTAINS[c] %@) AND kMDItemFSName ENDSWITH[c] %@" search arguments {a1, a2, b1, b2, c2}

But this one, triggers an error

set MyDoi to "10.1038/ng.1009"
set theFolders to {alias "MacBook HD:Users:ldicroce:Desktop:for_ERC:Single Cell:", alias "MacBook HD:Users:ldicroce:Desktop:for_ERC:To_sort:"}
set {a1, a2, b1, b2, c2} to {"kMDItemDescription", MyDoi, "kMDItemFinderComment", MyDoi, ".pdf"}

set theMetadata to perform search in folders {theFolders} predicate string "(%K CONTAINS[c] %@ OR %K CONTAINS[c] %@) AND kMDItemFSName ENDSWITH[c] %@" search arguments {a1, a2, b1, b2, c2}

The error is:
Can’t get POSIX path of {alias “MacBook HD:Users:ldicroce:Desktop:for_ERC:Single Cell:”, alias “MacBook HD:Users:ldicroce:Desktop:for_ERC:To_sort:”}.

Any suggestion on what I am doing wrong?
(I also tried with posix paths or files. Same error.
Thanks
L.

@ldicroce

As your value theFolders is defined as a list (logical according to the plural s) my understanding is that the calling instruction would be:

set theMetadata to perform search in folders theFolders predicate string "(%K CONTAINS[c] %@ OR %K CONTAINS[c] %@) AND kMDItemFSName ENDSWITH[c] %@" search arguments {a1, a2, b1, b2, c2}

Maybe Shane will give more precisions but in first case you pass {{ an alias }}. It’s not clean but the lib is able to grab the unique alias. In fact I’m surprised to read that it works.
In second case you pass {{ an alias, an other alias }} and this time the lib is logically fooled.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) samedi 21 décembre 2019 10:47:39

You were right: it works without the brackets. My fault ! The error is not triggered anymore. Although in the “Metadata Lib Read Me.rtf” associated with this library both examples (with and without brackets) are included.

@ldicroce

My understanding is that there is a typo in the instruction

set theFolders to {"/Users/ldicroce/Desktop/for_ERC:Single Cell", "/Users/ldicroce/Desktop/for_ERC/To_sort"} 

I guess that the first POSIX path would be : “/Users/ldicroce/Desktop/for_ERC/Single Cell”

Common typo when somebody converts an Hfs path into a POSIX one by hand;)

The first path pointed to a folder which doesn’t exist so it’s not surprising that the script returned nothing for it.
In fact I’m surprised by the fact that the script doesn’t issue an error in such case.
I will look in the code of the library but I assume that Shane will be able to tell us if we are facing a deliberate behavior or an oddity.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) samedi 21 décembre 2019 12:01:45

I just saw while you were post it.
Thanks a lot Yvan.

just one more thing:

this works with one folder location but NOT with 2 locations

set theFolder to choose folder with multiple selections allowed
set theMetadata to perform search in folders {theFolder} predicate string "%K ENDSWITH[c] %@" search arguments {"kMDItemFSName", ".pdf"}

this works with 1, 2 or more locations. Brackets removed!

set theFolder2 to choose folder with multiple selections allowed
set theMetadata2 to perform search in folders theFolder2 predicate string "%K ENDSWITH[c] %@" search arguments {"kMDItemFSName", ".pdf"}

It’s always what I described in my first message

When you select only one folder, choose folder return { an alias } which is accepted later as {{ an alias }} in case 1
but in the same case {{ an alias } ,{ an other alias } } is rejected.

In case 2 the library receive { an alias } or {{ an alias } ,{ an other alias } } whis are valid parameters.

When you use choose folder the only case where enclosing the result in { } is when you don’t postfix the instruction with ‘{ }’. In this case the result is an alias.

If you are not sure of what is treated use:
if class of passedObject is not list then set passedObject to { passedObject}
or
if class of passedObject is not list then set passedObject to passedObject as list

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) samedi 21 décembre 2019 12:45:03

I don’t believe they are – at least, not for the in folders parameter. But the dictionary is clear: the parameter requires a list.

It’s a quirk of AppleScript. Effectively the script gets to a line like this:

set x to POSIX path of {alias "Path:to:File"}

But instead of throwing an error, AppleScript ignore the brackets – as long as there’s only one item in the list.

I guess I was confused by these two side-by-side examples:

set theFolder to choose folder
set theMetadata to perform search in folders {theFolder} predicate string "kMDItemFSName ENDSWITH[c] '.txt'"

The result is a list of POSIX paths. If you want to exclude results from subfolders, you would use:

set theFolder to choose folder
set theMetadata to perform search just in theFolder predicate string "kMDItemFSName ENDSWITH[c] '.txt'"

Thanks for clarifying this to me!
L

Thank you Shane. Now there is no need for me to try again to study the code of the library.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) samedi 21 décembre 2019 13:58:11