Saturday, July 4, 2020

#1 2020-05-03 10:02:45 am

sebrady
Member
Registered: 2005-09-13
Posts: 36

select items in variable

here are 2 examples, 1 will select the items in a variable, the other does not work,


Applescript:

-- this works
tell application "Finder"
   set a to (every item in window 1 whose name contains "export mix")
   select a
end tell

Applescript:


tell application "Finder"
   set x to ""
   repeat with an_item in window 1
       if name extension of an_item is "mxf" then
           set x to x & an_item & return
       else
           if name of an_item contains "export mix" then
               set x to x & an_item & return
           end if
       end if
   end repeat
       select x -- this does not work
end tell

Offline

 

#2 2020-05-03 10:28:07 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 530

Re: select items in variable

Try the following. Your script created a string which the Finder select command did not understand.

Applescript:

tell application "Finder"
   set x to {}
   repeat with an_item in window 1
       if name extension of an_item is "mxf" then
           set end of x to an_item
       else
           if name of an_item contains "export mix" then
               set end of x to an_item
           end if
       end if
   end repeat
   select x
end tell

Last edited by peavine (2020-05-04 09:15:00 am)


2018 Mac mini - macOS Catalina

Offline

 

#3 2020-05-03 11:00:51 am

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

Re: select items in variable

KniazidisR wrote:

When the result of the condition is the same, you do not need to use ELSE scheme. It would be more logical to use the OR scheme:

Applescript:


tell application "Finder"
   set x to {}
   repeat with an_item in window 1
       set nameExt to name extension of an_item
       if (nameExt is "mxf") or (nameExt contains "export mix") then set end of x to an_item
   end repeat
   select x
end tell



I assume that you think to this kind of syntax:

Applescript:

tell application "Finder"
   set x to {}
   repeat with an_item in window 1
       if (name extension of an_item is "mxf") or name of an_item contains "export mix" then
           set end of x to an_item
       end if
   end repeat
   select x
end tell

I'm really not sure that it would be faster than the code posted by peavine.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 3 mai 2020 19:00:46

Offline

 

#4 2020-05-03 11:33:18 am

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

Re: select items in variable

I deleted my previous post because it contained some wrong statements. I always liked not only fast, but also short concise scripts. The speed is same with other posted here scripts:

Applescript:


tell application "Finder"
   set x to {}
   repeat with an_item in window 1
       tell an_item to if (name extension is "mxf") or (name contains "export mix") then ¬
           set end of x to an_item
   end repeat
   select x
end tell

Last edited by KniazidisR (2020-05-03 11:37:45 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#5 2020-05-03 11:43:47 am

sebrady
Member
Registered: 2005-09-13
Posts: 36

Re: select items in variable

Thanks for your suggestions.

I tried a lot of different things, found EXACTLY what I was looking for. I have to select certain files in certain order. Here is the order alphabetically.

05-04-20 Bumper EM v1.mp4
05-04-20 Bumper EM v1.mxf  (select 1st)
05-04-20 Bumper EM v1.omf
05-04-20 Bumper export mix.wav   (select 2nd)
05-04-20 NDP EM v1.mp4
05-04-20 NDP EM v1.mxf    (select 3rd)
05-04-20 NDP EM v1.omf
05-04-20 NDP export mix.wav.   (select 4th)
05-04-20 OPEN EM v1.mp4
05-04-20 OPEN EM v1.mxf    (select 5th)
05-04-20 OPEN EM v1.omf
05-04-20 open export mix.wav    (select 6th)


here is the code that selects in the above order.

Applescript:

set x to {item 2 of window 1, item 4 of window 1, item 6 of window 1, item 8 of window 1, item 10 of window 1, item 12 of window 1}
   select x

Offline

 

#6 2020-05-03 01:10:13 pm

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

Re: select items in variable

Bingo, one more time, helpers are supposed to be sooth sayers able to guess what the asker fail to describe.
Try :

Applescript:

tell application "Finder"
   set theItems to every item in window 1 as alias list -- correctly return a list of aliases
   set theItems to (sort theItems by name) -- as alias list -- with or without the coercion, return a list of Finder's references
   
   set x to ""
   repeat with an_item in theItems
       if name extension of an_item is "mxf" then
           set x to x & an_item & return
       else
           if name of an_item contains "export mix.wav" then -- I wanted to use ends with but I'm not sure that the available ending dot (4th selected) is just a typo
               set x to x & an_item & return
           end if
       end if
   end repeat
   -- log x
   select x
end tell

You will get:
{reference to 05-04-20 Bumper EM v1.mxf, reference to  05-04-20 Bumper export mix.wav, reference to  05-04-20 NDP EM v1.mxf, reference to  05-04-20 NDP export mix.wav, reference to  05-04-20 OPEN EM v1.mxf, reference to   05-04-20 open export mix.wav}

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 3 mai 2020 21:09:15

Offline

 

#7 2020-05-03 05:32:49 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 530

Re: select items in variable

sebrady wrote:

Thanks for your suggestions.

I tried a lot of different things, found EXACTLY what I was looking for. I have to select certain files in certain order.



sebrady. I'm glad you found something that works for you but, just in general, your script is not a reliable one. The reason is that the Finder does not always return the files in a folder in the same order they are displayed, even when sorted by name. So, "item 2 of window 1" could easily refer to the first displayed file in the Finder window.

Anyways, as long as you do not create, delete, or rename any files in the folder--and with a few other somewhat unlikely exceptions--your script will probably work as you want. It just shouldn't IMO be relied on for anything important.

Last edited by peavine (2020-05-03 07:11:50 pm)


2018 Mac mini - macOS Catalina

Offline

 

#8 2020-05-04 03:56:28 am

sebrady
Member
Registered: 2005-09-13
Posts: 36

Re: select items in variable

peavine wrote:

sebrady. I'm glad you found something that works for you but, just in general, your script is not a reliable one.



so much for me to learn. I will use this everyday with week, will let you know if it goes off the rails.

Offline

 

#9 2020-05-04 10:37:57 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 530

Re: select items in variable

peavine wrote:

The reason is that the Finder does not always return the files in a folder in the same order they are displayed, even when sorted by name. So, "item 2 of window 1" could easily refer to the first displayed file in the Finder window.



I was curious if my statement is still true under Catalina and decided to put it to the test with four files in a folder under three scenarios:

1) Displayed in Finder window with sort by name column enabled in ascending order

new Text File 1.txt
new Text File 3.txt
New Text File 20.txt
New Text File.txt

2) Returned by Finder tell statement using "every file in"

New Text File 20.txt
New Text File.txt
new Text File 1.txt
new Text File 3.txt

3) Returned by Finder sort command

new Text File 1.txt
new Text File 3.txt
New Text File 20.txt
New Text File.txt

Nigel's explanation for this sorting behavior is:

...the indexing order is stricter than in a Finder sort-by-name. Case isn't ignored and numeric strings are treated lexically, not numerically



https://macscripter.net/viewtopic.php?pid=198481


2018 Mac mini - macOS Catalina

Offline

 

#10 2020-05-04 11:16:46 am

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

Re: select items in variable

peavine wrote:


Nigel's explanation for this sorting behavior is:

...the indexing order is stricter than in a Finder sort-by-name. Case isn't ignored and numeric strings are treated lexically, not numerically



https://macscripter.net/viewtopic.php?pid=198481




What I see doesn't match this description

Applescript:

tell application "Finder"
   set theItems to every item in window 1 as alias list -- correctly return a list of aliases
   set theItems2 to (sort theItems by name) -- as alias list -- with or without the coercion, return a list of Finder's references
end tell
return {theItems, linefeed, linefeed, theItems2}
(*
{{alias "…:Doc 2.numbers", alias "…:doc 01.numbers", alias "…:doc 02.numbers", alias "…:doc 04.numbers", alias "…:doc 1.numbers", alias "…:doc 2 .numbers", alias "…:doc 20.numbers", alias "…:doc 3.numbers", alias "…:doc 30.numbers", alias "…:doc 4.numbers"}, "
", "
", {document file "doc 1.numbers" of folder …, document file "doc 01.numbers" of folder …, document file "doc 2 .numbers" of folder …, document file "doc 02.numbers" of folder …, document file "Doc 2.numbers" of folder …, document file "doc 3.numbers" of folder …, document file "doc 4.numbers" of folder …, document file "doc 04.numbers" of folder …, document file "doc 20.numbers" of folder …, document file "doc 30.numbers" of folder …}}
*)

(*
In the window sorted by name the list is
{"doc 1.numbers", "doc 01.numbers", "doc 2 .numbers", "doc 02.numbers", "Doc 2.numbers", "doc 3.numbers", "doc 4.numbers", "doc 04.numbers", "doc 20.numbers", "doc 30.numbers"}
*)

As far as I know, if the numerical components were treated lexically, I would get
{"doc 01.numbers", "doc 02.numbers", "doc 04.numbers", "doc 1.numbers", "doc 2 .numbers", "Doc 2.numbers", "doc 20.numbers", "doc 3.numbers", "doc 30.numbers""
"doc 4.numbers"}

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 4 mai 2020 19:16:30

Offline

 

#11 2020-05-04 06:53:38 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 530

Re: select items in variable

Yvan. In retrospect I'm not sure what Nigel meant by "Finder sort-by-name." That issue aside, I expanded my testing and found the following:

Files returned by Finder in a script:
* Considers case
* Numeric strings treated lexically

Files shown in a Finder window:
* Does not consider case
* Numeric strings treated numerically

Files after Finder sort command:
* Does not consider case
* Numeric strings treated numerically

The important point seems to be that the order of files returned by Finder in a script will, in many instances, be different from the file order shown in a Finder window. The fix, or perhaps workaround, is to use the Finder sort command, as you did in your script above.

As regards the following from your post:

As far as I know, if the numerical components were treated lexically, I would get
{"doc 01.numbers", "doc 02.numbers", "doc 04.numbers", "doc 1.numbers", "doc 2 .numbers", "Doc 2.numbers", "doc 20.numbers", "doc 3.numbers", "doc 30.numbers""
"doc 4.numbers"}



I don't think you would see this because the one file that begins with a "D" would appear first because of its case.

Last edited by peavine (2020-05-04 07:32:38 pm)


2018 Mac mini - macOS Catalina

Offline

 

#12 2020-05-05 04:17:04 am

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

Re: select items in variable

peavine wrote:


As regards the following from your post:

As far as I know, if the numerical components were treated lexically, I would get
{"doc 01.numbers", "doc 02.numbers", "doc 04.numbers", "doc 1.numbers", "doc 2 .numbers", "Doc 2.numbers", "doc 20.numbers", "doc 3.numbers", "doc 30.numbers""
"doc 4.numbers"}



I don't think you would see this because the one file that begins with a "D" would appear first because of its case.



I got this ordered list when I sorted in Numbers.
and if I run

Applescript:

"doc 2 .numbers" < "Doc 2.numbers"

I get : true
My understanding is that you missed the space inserted after the "2" in the name "doc 2 .numbers"
As my system isn't case sensitive, I can't have a file named "doc 2.numbers" and a file named "Doc 2.numbers" in the same folder.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 5 mai 2020 12:16:38

Offline

 

#13 2020-05-05 07:37:25 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 530

Re: select items in variable

Yvan. I had trouble following your script, so I rewrote the files returned line-by-line. Let me know if I got this wrong.

In your script, Finder returned the following files with "every item". This seems properly sorted if I am correct that this is sorted both considering case and with numeric strings treated lexically.

Doc 2.numbers
doc 01.numbers
doc 02.numbers
doc 04.numbers
doc 1.numbers
doc 2 .numbers
doc 20.numbers
doc 3.numbers
doc 30.numbers
doc 4.numbers

If I understand correctly, this is how you believe the list should be sorted but this does not seem to consider case. Also, I don't understand in what respect the last space in "doc 2 .numbers" is significant.

doc 01.numbers
doc 02.numbers
doc 04.numbers
doc 1.numbers
doc 2 .numbers
Doc 2.numbers
doc 20.numbers
doc 3.numbers
doc 30.numbers
doc 4.numbers

The ASLG contains the following, so perhaps our differing results are related to this. My results pertain to the English language only.

To determine the ordering of two text objects, AppleScript uses the collation order set in the Language pane of International preferences. A text object is greater than (comes after) another text object based on the lexicographic ordering of the user’s language preference.



I don't know what rules the "greater than" test follows and I'm not sure how that relates in this matter.

Last edited by peavine (2020-05-05 07:46:26 am)


2018 Mac mini - macOS Catalina

Offline

 

#14 2020-05-05 09:08:15 am

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

Re: select items in variable

I sent you a personal message with a link to an archive.

You will be able to check what I posted here.

A sort code compare the passed object to determine which one is greater than the other.
In an alphabetical sort the strings are sorted smaller first, greater last.

Here is a script showing different sorting rules at work upon my original list.

Applescript:

(*

https://forum.latenightsw.com/t/bridge-plus-question/1451/3

Sort comparison

*)



use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"

(* Tests with punctuation, digits, and Latin characters. Results best viewed in Script Debugger's "Desc" mode. *)

set anArray to current application's class "NSArray"'s arrayWithArray:({"Doc 2.numbers", "doc 01.numbers", "doc 02.numbers", "doc 04.numbers", "doc 1.numbers", "doc 2 .numbers", "doc 20.numbers", "doc 3.numbers", "doc 30.numbers", "doc 4.numbers"})


anArray's sortedArrayUsingSelector:("compare:")
log result as list (*Doc 2.numbers, doc 01.numbers, doc 02.numbers, doc 04.numbers, doc 1.numbers, doc 2 .numbers, doc 20.numbers, doc 3.numbers, doc 30.numbers, doc 4.numbers*)
--> Sorted by Unicode number. ("(" < "-" < digits < upper < "[" < "_" < lower < "{".)

anArray's sortedArrayUsingSelector:("caseInsensitiveCompare:")
log result as list (*doc 01.numbers, doc 02.numbers, doc 04.numbers, doc 1.numbers, doc 2 .numbers, Doc 2.numbers, doc 20.numbers, doc 3.numbers, doc 30.numbers, doc 4.numbers*)
--> Simile, but case-insensitively. Diacriticals still considered. ("(" < "-" < digits < "[" < "_" < letters < "{".)
-- Where strings are identical apart from case, it's apparent that the sort is stable.

anArray's sortedArrayUsingSelector:("localizedCompare:")
log result as list (*doc 01.numbers, doc 02.numbers, doc 04.numbers, doc 1.numbers, doc 2 .numbers, Doc 2.numbers, doc 20.numbers, doc 3.numbers, doc 30.numbers, doc 4.numbers*)
--> (en_GB) Case-insensitive and diacriticals ignored! Punctuation < digits. ("_" < "-" "(" < "[" < "{" < digits < letters.)
-- Where strings are identical apart from case or diacriticals, diacriticals are considered first, then lower < upper.

anArray's sortedArrayUsingSelector:("localizedCaseInsensitiveCompare:")
log result as list (*doc 01.numbers, doc 02.numbers, doc 04.numbers, doc 1.numbers, doc 2 .numbers, Doc 2.numbers, doc 20.numbers, doc 3.numbers, doc 30.numbers, doc 4.numbers*)
--> (en_GB) As "localisedCompare:" except that case is ignored in otherwise identical strings, which are sorted stably.

anArray's sortedArrayUsingSelector:("localizedStandardCompare:") -- the Finder's rule
log result as list (*doc 1.numbers, doc 01.numbers, doc 2 .numbers, doc 02.numbers, Doc 2.numbers, doc 3.numbers, doc 4.numbers, doc 04.numbers, doc 20.numbers, doc 30.numbers*)
--> (en_GB) As "localizedCompare:" except that numeric sequences are sorted by the number values represented.

Is it clear now ?

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 5 mai 2020 16:36:09

added a simple and inefficient way to sort

Applescript:

set aList to {"Doc 2.numbers", "doc 01.numbers", "doc 02.numbers", "doc 04.numbers", "doc 1.numbers", "doc 2 .numbers", "doc 20.numbers", "doc 3.numbers", "doc 30.numbers", "doc 4.numbers"}
repeat 2 times
   repeat with i from 1 to (count aList) - 1
       if item i of aList > item (i + 1) of aList then
           set bof to item i of aList
           set item i of aList to item (i + 1) of aList
           set item (i + 1) of aList to bof
       end if
   end repeat
end repeat
aList

The comment (* Tests with punctuation, digits, and Latin characters. Results best viewed in Script Debugger's "Desc" mode. *) is a relief of the original script borrowed from Late Night's forum.

With the strings passed, localization has no impact because no diacritical are used.

Last edited by Yvan Koenig (2020-05-05 11:28:19 am)

Offline

 

#15 2020-05-05 09:46:28 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 530

Re: select items in variable

Yvan Koenig wrote:

Is it clear now ?


Yvan. I have no knowledge of ASObjC, so there's no way for me to understand your script. I'll accept your statement that the numerical components of the files are not treated lexically.


2018 Mac mini - macOS Catalina

Offline

 

#16 2020-05-05 11:21:41 am

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

Re: select items in variable

There is no need to know ASObjC to run the given script.
I carefully inserted log instructions so you would be able to see what is returned by every sort instruction. You would be able to check that what I inserted as comments are the real results.
I sent you a personal message with a link allowing you to see a screenshot of what I got and a Numbers spreadsheet showing what gives the sort algorithm used by this application.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 5 mai 2020 19:17:45

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)