• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: whose class is folder -- and Filtering AppleScript List Objects
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: whose class is folder -- and Filtering AppleScript List Objects


  • Subject: Re: whose class is folder -- and Filtering AppleScript List Objects
  • From: Christopher Stone <email@hidden>
  • Date: Sat, 07 Apr 2018 01:53:05 -0500

On 04/06/2018, at 16:49, Jean-Christophe Helary
<email@hidden <mailto:email@hidden>>
wrote:
> lI'm having issues understanding basic filtering it seems:
>
> tell application "Finder"
>       class of item 1 of (selection as list)
> end tell
>
> this returns folder (if I select a folder, that is)


Hey Jean-Christophe,

Yes.  That simply retrieves a list item by position and gets its class.

> but if I do
>
> tell application "Finder"
>       item of (selection as list) whose class is folder
> end tell
>
> I get a "can't get item of {...}" -1,728 error...

The selection (list) is essentially a dead object as soon as you get it – just
a list of values – and filter forms that work on application objects do not
work on lists.

----------------------------------------------------------------

# Select two or more items in the Finder and run this

tell application "Finder"
        set finderSelectionList to selection as alias list
end tell

set item 2 of finderSelectionList to 0

aliases of finderSelectionList

----------------------------------------------------------------

From the Applescript Language Guide:

Filter

Specifies all objects in a container that match a condition, or test, specified
by a Boolean expression.

The filter form specifies application objects only. It cannot be used to filter
the AppleScript objects list (page 112), record (page 118), or text (page 124).
A term that uses the filter form is also known as a whose clause.


This doesn't mean you can't filter a list of AppleScript objects, but you can't
use a whose clause – and you can't do fancy things like every text of theList
where it contains “filter”.

I'm not sure if/where the list object filter form is documented, but the things
that work (as I recall) are normal AppleScript objects:

----------------------------------------------------------------

# Run this:

tell application "Finder" to ¬
        set itemList to items 1 thru 3 of (path to documents folder) as alias
list

set contents of (a reference to item 1 of itemList) to (item 1 of itemList) as
«class furl»

set end of itemList to 100
set end of itemList to 0.3132697
set end of itemList to "I'm a test!"
set end of itemList to {"I'm a list", "with two items"}
set end of itemList to {recLabel:"I'm a Record"}

set a to every «class furl» of itemList
set b to every alias of itemList
set c to every integer of itemList
set d to every list of itemList
set e to every real of itemList
set f to every record of itemList
set g to every text of itemList
set h to {text, integers, reals} of itemList

----------------------------------------------------------------

I often use this faculty to filter out items in a list in-place.

----------------------------------------------------------------

# Using the list above:

repeat with listItem in itemList
    if (class of listItem) is not alias then
        set contents of listItem to 0
    end if
end repeat

set filteredList to every alias of itemList

-------------------------------------------------------------------------------------------

--
Take Care,
Chris

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

References: 
 >whose class is folder... (From: Jean-Christophe Helary <email@hidden>)

  • Prev by Date: Re: whose class is folder...
  • Next by Date: Re: Display the state of a process
  • Previous by thread: Re: whose class is folder...
  • Next by thread: Display the state of a process
  • Index(es):
    • Date
    • Thread