select items in variable

If the goal is to move the files, there is no need to select them.
Assuming that the two folders exist, just use:

# Two instructions which don't need to be in the Finder block
set x to (path to desktop as string) & "for sale"
set dest to (path to desktop as string) & "moved"
set z to {}

tell application "Finder"
	set y to files of folder x as alias list
	repeat with i from 1 to count y by 2 # Or by 3
		set end of z to item i of y
	end repeat
	# display dialog "Really want to move the files"
	move z to folder dest
	
end tell

Because of the large number of files the OP wants to process, I thought a script utilizing System Events might be faster. Given the OP’s further explanation of what he wants to accomplish, the only way I could envision this working is to add a sort command to the Finder tell block, as shown in the script below. To compare, I slightly modified Yvon’s script in post 13, and I’ve also included that below.

To time these scripts, I created a test folder with 300 files and ran both scripts using Script Geek. The results were:

System Events Script: 0.661 second

Finder Script: 0.417 second

Normally, System Events is significantly faster and more capable when dealing with a large number of files. In this particular case, because of the need for a sorted list, the Finder script is faster.

Just as an aside, I temporarily disabled the sort command and reran the System Events script. It did not select every other file as the OP Wants and instead selected blocks of files ranging from 2 to 6 consecutive selected files. The timing result reported by Script Geek was 0.270 second, reflecting the significant time it takes for the sort command to do its work.

–SYSTEM EVENTS SCRIPT–

set x to the "Save:Temp:" as alias
set z to {}

tell application id "com.apple.SystemEvents"
	set y to the path of every file in x whose visible = true
end tell

tell application id "com.apple.Finder"
	set y to (sort y by name) -- creation date rather than name might be a useful alternative
	repeat with i from 1 to (count y) by 2
		set the end of z to item i of y
	end repeat
	select z
end tell

–FINDER SCRIPT–

set x to "Save:Temp:"
set z to {}

tell application "Finder"
	set y to files of folder x as alias list
	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

@ peavine

may you apply this version to your folder ?

----------------------------------------------------------------
use AppleScript version "2.5"
use framework "Foundation"
use scripting additions
----------------------------------------------------------------

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

set fileManager to current application's NSFileManager's defaultManager()
set y to fileManager's contentsOfDirectoryAtURL:x includingPropertiesForKeys:{} options:(current application's NSDirectoryEnumerationSkipsHiddenFiles) |error|:(missing value)
set y to y as list
set z to {}
tell application id "com.apple.Finder"
	set y to (sort y by name)
	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

I assume that it would be faster than the two others.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 4 décembre 2019 17:17:57

Yvon. I tested the script with Script Geek, and the result, along with those reported above, are:

System Events: 0.661 second
ASObjC: 0.424 second
Finder: 0.417 second

Thanks

Just for info, since 1943/12/31, my first name is Yvan, not Yvon :wink:

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 4 décembre 2019 17:50:19

Thanks for pointing that out–I will get it correct in the future.

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