Tuesday, September 27, 2022

#1 2022-09-11 07:55:41 pm

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

indexesOfObjectsPassingTest method

Just for learning purposes, I wanted to understand the indexesOfObjectsPassingTest method and wondered if it might be applicable in the following script. If not, is there an example of when it might be used. Thanks.

Applescript:

use framework "Foundation"

set theString to "aa
mmxxmm
bb
nnxxnn
cc"

set searchString to "xx"
set theString to current application's NSString's stringWithString:theString
set theArray to (theString's componentsSeparatedByString:linefeed)
set theIndexes to theArray's indexesOfObjectsPassingTest:[missing code] -- the test is whether an array element contains "xx"

Last edited by peavine (2022-09-11 08:05:23 pm)


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#2 2022-09-12 12:58:39 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5563

Re: indexesOfObjectsPassingTest method

Hi peavine.

The parameter's a "block", according to the documentation. This has no ASObjC equivalent, so I don't think the method can be used in a script.


NG

Offline

 

#3 2022-09-12 07:33:31 am

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

Re: indexesOfObjectsPassingTest method

Thanks Nigel.


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#4 2022-09-15 01:34:25 am

technomorph
Member
Registered: 2017-12-14
Posts: 279

Re: indexesOfObjectsPassingTest method

The block is essentially a predicate.

You Could create a predicate.
Get a filteredArray from oroginalArray
Create a NSMutableIndexSet matchIdxs

Then loop thru the filteredArray.
Get aIndex via originalArray indexOfObject:
(As good practice id test the aIndex)
if (aIndex != NSNotfound) then
matchIdxs addIndex:aIndex
end if

The advantages of the block is you can do deeper
Evaluation on each obj and return true/false.
It will automatically add the index if true.
The block also has a STOP pointer variable so at
Any point you can choose to stop the further evaluation
Of any more objects in the original array.

I’ve shifted to mostly programming in Objective-C now.
And as I was learning both AscObjC blocks kept
Coming up.  Since AsObjC couldn’t really
Use them and they seemed a bit confusing, I tended to
Avoid them.   Slowly in Obj-C I Started to
Use the simplest ones like NSArray’s enumerate ItemsUsingBlock:
Lately I’ve really found useful NSPredicate’s predicateWithBlock:
super useful, for more complex filtering and preprocessing the
Evaluated object before my tests. And using another block inside that one
On a NSArray via enumateUsingBlock:
And if I find a match i can use the STOP on that enumerate block

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)