NSIndexSetを作成し、そこから各インデックス(数値)を取り出すAppleScriptです。
{3, 4, 5, 6, 7}
から構成されるNSIndexSetを作成して処理すると、
{3, 4, 5, 6, 7}
が返ってきます。
NSTableViewで複数行選択時の行インデックスを取得する際に、NSIndexSetで結果が返ってきたので、ChatGPTに書かせてみました。
本Script内でNSIndexSet内の各インデックス数値に対してオフセット加算ができるようにしているのは、このNSTableViewで1行目が0と返ってくる仕様のためで、AppleScriptと合わせるために+1のオフセット加算を行う必要があったためこのようになっています。
初回から動くコードは返してこなかったものの、何回かやりとりしている間に、書き直せばなんとかなりそうだったので、引き継いで手で書いてみました。CocoaのAPI呼び出しはプログラミングではなくパズルみたいなものなので、ChatGPTに向いているといえば向いているのですが、AppleScriptObjCがBlocks構文を呼べないとかいった前提条件が認識されていないようなので、「手で描き直した方が速い」ということに。
NSNotFoundについては、Apple側も真面目に実装している風ではない(これを返すことが徹底されているわけではない、と)ので、あてにできないと感じています。
AppleScript名:NSIndexSetを作成し、各index要素を取り出す.scptd |
— – Created by: Takaaki Naganoya – Created on: 2025/03/30 — – Copyright © 2025 Piyomaru Software, All Rights Reserved — use AppleScript version "2.4" — Yosemite (10.10) or later use framework "Foundation" use scripting additions — サンプルのNSIndexSetを作成(3, 4, 5, 6, 7 を含む) set indexSet to current application’s NSIndexSet’s indexSetWithIndexesInRange:(current application’s NSMakeRange(3, 5)) set aList to getEachIndexFromIndexSet(indexSet, 0) of me –> {3, 4, 5, 6, 7} — インデックスを取得する処理 on getEachIndexFromIndexSet(indexSet, offsetNum) set indexList to {} set currentIndex to indexSet’s firstIndex() — 最初のインデックスを取得 repeat while currentIndex ≠ (current application’s NSNotFound) — 取得したインデックスをAppleScriptのリストに追加 if currentIndex > 9.99999999E+8 then exit repeat set end of indexList to ((currentIndex as number) + offsetNum) — 次のインデックスを取得 set currentIndex to indexSet’s indexGreaterThanIndex:currentIndex end repeat return indexList end getEachIndexFromIndexSet |
AppleScript名:NSIndexSetを作成し、各index要素を取り出す(書き換え後).scptd |
— – Created by: Takaaki Naganoya – Created on: 2025/03/30 — – Copyright © 2025 Piyomaru Software, All Rights Reserved — use AppleScript version "2.4" — Yosemite (10.10) or later use framework "Foundation" use scripting additions — サンプルのNSIndexSetを作成(3, 4, 5, 6, 7 を含む) set indexSet to current application’s NSIndexSet’s indexSetWithIndexesInRange:(current application’s NSMakeRange(3, 5)) set aList to getEachIndexFromIndexSet(indexSet, 0) of me –> {3, 4, 5, 6, 7} — インデックスを取得する処理 on getEachIndexFromIndexSet(indexSet, offsetNum) set indexList to {} set currentIndex to indexSet’s firstIndex() — 最初のインデックスを取得 — 取得したインデックスをAppleScriptのリストに追加 repeat while currentIndex ≤ 9.99999999E+8 set end of indexList to ((currentIndex as number) + offsetNum) — 次のインデックスを取得 set currentIndex to indexSet’s indexGreaterThanIndex:currentIndex end repeat return indexList end getEachIndexFromIndexSet |