tell application "Finder"
set z to ""
set cc to 1
set x to (path to desktop) & "for sale" as string
set y to every item in folder x as string
repeat 5 times
--set end of z to (item cc of y as alias) & return -- {}
set z to z & ", " & (item cc of folder x as string) -- ""
set cc to cc + 1
end repeat
select items 1 thru 5 of folder x -- this works
select items 1 thru 3 of z -- this does NOT work, cant select the items in z
end tell
tell application "Finder"
set z to ""
set cc to 1
set x to (path to desktop) & "for sale" as string
set y to every item in folder x as string # Not used below
repeat 5 times # CAUTION, assumes that the folder contain atv least 5 files/folders
--set end of z to (item cc of y as alias) & return -- {}
set z to z & ", " & (item cc of folder x as string) -- ""
set cc to cc + 1
end repeat
log z (*, SSD 1000:Users:**********:Desktop:for sale:sample copie 2.scpt, SSD 1000:Users:**********:Desktop:for sale:sample copie 3.scpt, SSD 1000:Users:**********:Desktop:for sale:sample copie 4.scpt, SSD 1000:Users:**********:Desktop:for sale:sample copie 5.scpt, SSD 1000:Users:**********:Desktop:for sale:sample copie.scpt*)
select items 1 thru 5 of folder x -- this works # you try to select items embedded in a folder so it works
select items 1 thru 3 of z -- this does NOT work, cant select the items in z # you try to select items from a string so logically it fails.
end tell
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 2 décembre 2019 17:30:05
Firstly, let’s get you out of the habit of using Finder for file system operations while you’re still relatively new to AppleScript. It’ll save you a lot of grief later if you use System Events. Obviously, we need to use it for the actual selection, but that’s it.
tell app id "com.apple.SystemEvents"
set x to the alias "~/Desktop/" as alias
set y to the path of every item in x whose visible=true
end tell
set z to {}
repeat with cc from 1 to 5
set the end of z to item cc of y
end repeat
tell app id "com.apple.Finder"
set u to reveal (items 1 thru 3 of z)
activate
end tell
return u
tell application "Finder"
set x to (path to desktop) & "for sale" as string
set y to every item in folder x as alias list
set z to every item in folder x as string
set w to every item in folder x
select items 1 thru 5 of folder x -- this works # you try to select items embedded in a folder so it works
log (get class of y) (*list*)
select items 1 thru 3 of y -- Here we try to select 3 aliases, logically it works
log (get class of w) (*list*)
select items 2 thru 4 of w -- Here we try to select 3 references upon files, logically it works
log (get class of z) (*text*)
select items 1 thru 3 of z -- Here we try to select 3 items in a string object, logically it fails
end tell
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 2 décembre 2019 17:40:29
tell application id "com.apple.SystemEvents"
set x to the alias "~/Desktop/for sale" as alias
set y to the path of every item in x whose visible = true
end tell
log y (*SSD 1000:Users:**********:Desktop:for sale:sample copie 2.scpt, SSD 1000:Users:**********:Desktop:for sale:sample copie 3.scpt, SSD 1000:Users:**********:Desktop:for sale:sample copie 4.scpt, SSD 1000:Users:**********:Desktop:for sale:sample copie 5.scpt, SSD 1000:Users:**********:Desktop:for sale:sample copie.scpt, SSD 1000:Users:**********:Desktop:for sale:sample.scpt*)
tell application id "com.apple.Finder" to tell (make new Finder window to x) to select (items 1 thru 3 of y)
--> {document file "sample copie 2.scpt" of folder "for sale" of folder "Desktop" of folder "**********" of folder "Users" of startup disk of application "Finder", document file "sample copie 3.scpt" of folder "for sale" of folder "Desktop" of folder "**********" of folder "Users" of startup disk of application "Finder", document file "sample copie 4.scpt" of folder "for sale" of folder "Desktop" of folder "**********" of folder "Users" of startup disk of application "Finder"}
Or, shorter :
tell application id "com.apple.SystemEvents"
set x to the alias "~/Desktop/for sale" as alias
set y to the path of every item in x whose visible = true
end tell
log y (*SSD 1000:Users:**********:Desktop:for sale:sample copie 2.scpt, SSD 1000:Users:**********:Desktop:for sale:sample copie 3.scpt, SSD 1000:Users:**********:Desktop:for sale:sample copie 4.scpt, SSD 1000:Users:**********:Desktop:for sale:sample copie 5.scpt, SSD 1000:Users:**********:Desktop:for sale:sample copie.scpt, SSD 1000:Users:**********:Desktop:for sale:sample.scpt*)
tell application id "com.apple.Finder" to select (items 1 thru 3 of y)
--> {document file "sample copie 2.scpt" of folder "for sale" of folder "Desktop" of folder "**********" of folder "Users" of startup disk of application "Finder", document file "sample copie 3.scpt" of folder "for sale" of folder "Desktop" of folder "**********" of folder "Users" of startup disk of application "Finder", document file "sample copie 4.scpt" of folder "for sale" of folder "Desktop" of folder "**********" of folder "Users" of startup disk of application "Finder"}
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 2 décembre 2019 18:01:47
Yes, but the OP already demonstrated he knows about this, and is already able to do it. I elected to take the specific method that the OP was trying to get to work but couldn’t, and to get it to work for him. I’m sure there’s a reason he might wish to compile a separate list rather than selecting directly from y.
One reason might be the convenience of having a list of selected items already coerced to an appropriate data type. The values returned by commands select and reveal or by the selection property all return (partially) dereferenced lists, so coercing those to something other than Finder’s painful hierarchy of item references would need to be done on a per item basis using a repeat loop. Who has the time for that ?
Why do you redact the name of your home folder ? There’s no security or privacy risk in it being known. Or is it something really rude and/or embarrassing ?
I hate the Finder but when we need to work upon the first level of a folder contents, it seems that the simpler way to delve with list of items is the Finder urged to return an alias list.
tell application "Finder"
the selection as alias list
end tell
set theFolder to (path to desktop as text) & "for sale"
tell application "Finder"
every file of folder theFolder as alias list
end tell
It has the advantage to return the same kind of objects than choose file.
For most complex tasks I would not use Finder or System Events but would use ASObjC which is really faster. The only drawback is that the required code is a bit verbose but several libraries are available to spare lot of instructions.
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 2 décembre 2019 19:27:39
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
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:
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
# 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
----------------------------------------------------------------
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