Monday, December 9, 2019

#1 2019-12-02 09:33:23 am

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

select items in variable

any help would be appreciated.

Applescript:

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

Last edited by sebrady (2019-12-02 09:33:50 am)

Offline

 

#2 2019-12-02 10:30:21 am

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

Re: select items in variable

Applescript:

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

Last edited by Yvan Koenig (2019-12-02 10:50:49 am)

Offline

 

#3 2019-12-02 10:44:04 am

CK
Member
From:: UK
Registered: 2018-11-04
Posts: 109

Re: select items in variable

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.

Applescript:

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

Last edited by CK (2019-12-02 11:51:15 am)

Offline

 

#4 2019-12-02 10:49:27 am

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

Re: select items in variable

May be useful to add :

Applescript:

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

Offline

 

#5 2019-12-02 11:02:23 am

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

Re: select items in variable

CK wrote:

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.  Obviously, we need to use it for the actual selection, but that's it.

Applescript:

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" to tell (make new Finder window to x) to select (items 1 thru 3 of z)



I don't understand the need for the list z.

Applescript:

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 :

Applescript:

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

Offline

 

#6 2019-12-02 11:58:59 am

CK
Member
From:: UK
Registered: 2018-11-04
Posts: 109

Re: select items in variable

Yvan Koenig wrote:

I don't understand the need for the list z.

Pfft, nor do I.  Ask the OP, not me.

Yvan Koenig wrote:

Or, shorter :

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 ?

Yvan Koenig wrote:

Applescript:

(*, 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*)

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 ?

Last edited by CK (2019-12-02 12:21:26 pm)

Offline

 

#7 2019-12-02 12:28:52 pm

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

Re: select items in variable

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.

Applescript:

tell application "Finder"
   the selection as alias list
end tell

Applescript:

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

Last edited by Yvan Koenig (2019-12-02 12:29:18 pm)

Offline

 

#8 2019-12-02 12:49:54 pm

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

Re: select items in variable

CK has best code but I will improve it little:

Applescript:


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

Last edited by KniazidisR (2019-12-02 01:14:42 pm)


Model: MacBook Pro
macOS Mojave -- version 10.14.4
Safari -- version 12.1
Firefox -- version 70.0

Offline

 

#9 2019-12-02 06:39:59 pm

CK
Member
From:: UK
Registered: 2018-11-04
Posts: 109

Re: select items in variable

Yvan Koenig wrote:

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.

Disagree.

Applescript:

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.

Yvan Koenig wrote:

It has the advantage to return the same kind of objects than choose file.

Advantage in what way ?

Yvan Koenig wrote:

For most complex tasks

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.

Offline

 

#10 2019-12-03 07:40:42 am

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

Re: select items in variable

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

Last edited by peavine (2019-12-03 11:05:11 am)


2018 Mac mini - macOS Catalina

Offline

 

#11 2019-12-03 08:27:45 am

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

Re: select items in variable

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

Offline

 

#12 2019-12-03 08:38:21 am

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

Re: select items in variable

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.

Applescript:


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

Offline

 

#13 2019-12-03 09:17:18 am

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

Re: select items in variable

On my side I would use :

Applescript:


# 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

Last edited by Yvan Koenig (2019-12-03 09:19:46 am)

Offline

 

#14 2019-12-03 10:03:37 am

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

Re: select items in variable

sebrady wrote:

I got it to work.


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

Applescript:

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:

Applescript:

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.

Last edited by peavine (2019-12-06 08:10:54 am)


2018 Mac mini - macOS Catalina

Offline

 

#15 2019-12-03 11:01:29 am

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

Re: 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:

Applescript:

# 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

Last edited by Yvan Koenig (2019-12-03 11:05:38 am)

Offline

 

#16 2019-12-04 09:10:51 am

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

Re: select items in variable

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

Applescript:

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

Applescript:

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

Last edited by peavine (2019-12-06 08:27:10 am)


2018 Mac mini - macOS Catalina

Offline

 

#17 2019-12-04 10:20:11 am

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

Re: select items in variable

@ peavine

may you apply this version to your folder ?

Applescript:

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

Offline

 

#18 2019-12-04 10:38:29 am

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

Re: select items in variable

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


2018 Mac mini - macOS Catalina

Offline

 

#19 2019-12-04 10:50:45 am

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

Re: select items in variable

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

Offline

 

#20 2019-12-04 10:57:33 am

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

Re: select items in variable

Yvan Koenig wrote:

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


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


2018 Mac mini - macOS Catalina

Offline

 

#21 2019-12-04 11:36:07 am

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

Re: select items in variable

@ peavine

Thank you 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.

Applescript:

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

Offline

 

#22 2019-12-04 11:59:06 am

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

Re: select items in variable

Yvan Koenig wrote:

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.



Yvan. Script Geek reported 0.676 second.


2018 Mac mini - macOS Catalina

Offline

 

#23 2019-12-04 12:22:39 pm

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

Re: select items in variable

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

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)