select items in variable

@ peavine

Thank you :slight_smile:

I made an other attempt which doesnā€™t use the Finder to sort.
It rely upon FileManagerLib delivered by Shane Stanley.
Iā€™m afraid that the need to switch between POSIX paths and URL will be an efficient brake.

----------------------------------------------------------------
use AppleScript version "2.5"
use framework "Foundation"
use script "FileManagerLib" version "2.3.3"
use scripting additions
----------------------------------------------------------------

set x to ((path to desktop as text) & "for sale") as alias --Ā«class furlĀ»

# Grab the list of POSIX paths
set thePaths to objects of x result type paths list with searching subfolders without include folders and include invisible items
# Sort the list of POSIX path returning an array of URLs
set sortedURLs to sort objects thePaths sorted property name property sort type Finder like result type urls array with low to high
# Convert the array of URLs into a list of files (Ā«class furlĀ» objects)
set y to sortedURLs as list
set z to {}
tell application id "com.apple.Finder"
	repeat with i from 1 to (count y) by 2
		set end of z to item i of y
	end repeat
	select z
end tell

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 4 dƩcembre 2019 18:35:07

Yvan. Script Geek reported 0.676 second.

Thanks, itā€™s what I feared.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 4 dƩcembre 2019 19:22:21

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

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


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

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

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

I assume that you think to this kind of syntax:

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

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:


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

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.

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

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

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

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.

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

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

  1. 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

  1. 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:

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

What I see doesnā€™t match this description

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

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:

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

"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

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.

I donā€™t know what rules the ā€œgreater thanā€ test follows and Iā€™m not sure how that relates in this matter.

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.

(*

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

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.

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.

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