select items in variable

CK has best code but I will improve it little:


tell application id "com.apple.SystemEvents"
	set x to alias "~/Desktop/"
	set y to path of every item in x whose visible is true
end tell

set z to items 1 thru 5 of y
tell application id "com.apple.Finder"
	activate
	select items 1 thru 3 of z
end tell

delay 5 -- only to make the result visible 5 seconds

Disagree.

tell app id "com.apple.SystemEvents" to return the path of every file of the folder "/path/to/folder" whose visible = true

That’s not excruciatingly difficult.

Advantage in what way ?

You must have astoundingly complex needs. I like ASObjC, but right tool for the right job and all that… Finder ain’t never the right tool.

The end goal of the OP’s script appears to be to select the first 3 or 5 items contained in the target folder as returned by Finder. If that’s the case, I would wonder if you can use System Events as a direct replacement for Finder because System Events does not return files in the same order as Finder.

EDIT AFTER READING YVON’S POST BELOW

This issue was discussed before and I can’t recall the order in which System Events returns files. So, I ran a test.

I created five lettered text files on my desktop, and I created them in the following order:

c.txt
d.txt
a.txt
e.txt
b.txt

Finder returned them in the following order:

a.txt
b.txt
c.txt
d.txt
e.txt

System Events returned them in the following order:

c.txt
e.txt
a.txt
b.txt
d.txt

Good point.
With my sample folder which contain only files, the two apps returned the files in the same order.
As I’m curious, I made a try with a folder containing files and folders and this time the returned lists were different.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 3 décembre 2019 15:27:16

I got it to work.
I needed to name all the files like this. 0001.jpg, 0002.jpg, 0003.jpg
I take time lapse photography, usually I have 300 files.

if the time lapse is too slow, this script will highlight every 2nd or 3rd file, so the time lapse speeds up.

just drag the selected files to another folder.

below is the code.


tell application "Finder"
	set z to {}
	set cc to 1
		
	set x to (path to desktop) & "test" as string
	set file_count to count files in folder x
	
	try
		repeat file_count times
			set z to z & (item cc of folder x as string) -- ""
			set cc to cc + 2
		end repeat
	end try
	
	select every item in z
	
end tell

On my side I would use :


# Two instructions which don't need to be in the Finder block
set x to (path to desktop as string) & "test" # "for sale"
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
	
	set selection to z
	
end tell

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 3 décembre 2019 16:12:38

sebrady. There is an error in your script, which arises from the following line:

repeat file_count times

The way your script is written, and assuming it selects every second file, the file_count variable should be one-half the number of files in the target folder, as in the following:

repeat (file_count div 2) times

The reason your script works as written is because the above error is suppressed by a try statement.

FWIW, Yvan’s script in post 13 handles all of this well.

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