Blocks in AppleScriptObjc

Can someone provide the syntax, if it is possible, to use the NSIndexSet enumeration

  • (void)enumerateIndexesUsingBlock:(void (^)(NSUInteger idx, BOOL *stop))block;

set anIndexedSet to NSMutableIndexSet’s new()
– add index ranges
set e to anIndexedSet’s (its enumerateIndexesUsingBlock:???)
repeat with x in e’s allObjects()

end repeat
Specifically how to code the “block”

The index set only provide enumerators with blocks

You can’t access blocks via AppleScriptObjC. NSIndexSet is somewhere it’s a particular pain.

But you can still iterate through an indexSet like this:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

set anIndexedSet to current application's NSMutableIndexSet's new()
-- add index ranges
tell anIndexedSet to addIndexesInRange:({0, 5})
tell anIndexedSet to addIndexesInRange:({20, 5})
tell anIndexedSet to addIndex:(15)

set x to anIndexedSet's firstIndex()
-- set z to anIndexedSet's lastIndex()
repeat (count anIndexedSet) times -- or until (x > z), when x ≈ NSNotFound.
	-- Do something with x.
	-- say x

	set x to anIndexedSet's indexGreaterThanIndex:(x)
end repeat

Yes, it’s just a bit slower.

Thank you for your replies. I suspected that ‘blocks’ were and issue, I wanted to double check.

The loop from first to last index checking for ‘index set’ is a good workaround, especially if the set of numbers is in a limited range, which in my case is true.