AppleScript名:配列をソートする(1D) |
— Created 2015-09-02 by Takaaki Naganoya — 2015 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set anArray to current application’s NSArray’s arrayWithObjects_(1, 2, 3) –> (NSArray) {1, 2, 3} –ソートする set sortRes1 to anArray’s sortedArrayUsingSelector:"compare:" –逆順の場合には、sortDescriptorを指定する必要がある –> (NSArray) {1, 2, 3} –compare: –caseInsensitiveCompare: –localizedCompare: –localizedCaseInsensitiveCompare: –localizedStandardCompare: |
カテゴリー: list
1Dリスト中の最も近い値を返す
AppleScript名:1Dリスト中の最も近い値を返す |
— Created 2017-11-11 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.5" use scripting additions use framework "Foundation" –http://piyocast.com/as/archives/4971 property NSArray : a reference to current application’s NSArray set aList to {50, 30, 20, 15, 10, 5, 0} set targNum to retNearestNumInList(7, aList) of me –> 5 set targNum to retNearestNumInList(100, aList) of me –> 50 on retNearestNumInList(aNum, aList as list) set anNSArray to NSArray’s arrayWithArray:aList set bList to (anNSArray’s sortedArrayUsingSelector:"compare:") as list set aPreNum to 0 repeat with i in bList set j to contents of i if j ≥ aNum then exit repeat end if set aPreNum to j end repeat return aPreNum end retNearestNumInList |
範囲指定して配列を作成 v1.1a
AppleScript名:範囲指定して配列を作成 v1.1a |
— Created 2017-10-30 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use aBplus : script "BridgePlus" –https://www.macosxautomation.com/applescript/apps/BridgePlus.html load framework set aindexSet to current application’s NSIndexSet’s indexSetWithIndexesInRange:(current application’s NSMakeRange(5, 100)) set aList to (current application’s SMSForder’s arrayWithIndexSet:aindexSet) as list –> {5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104} |
連番数値リスト中で欠落アイテム(Gap)を抽出 v2
AppleScript名:連番数値リスト中で欠落アイテム(Gap)を抽出 v2 |
— Created 2017-12-06 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use bPlus : script "BridgePlus" –https://www.macosxautomation.com/applescript/apps/BridgePlus.html property SMSForder : a reference to current application’s SMSForder property NSIndexSet : a reference to current application’s NSIndexSet property NSMutableSet : a reference to current application’s NSMutableSet property NSMutableArray : a reference to current application’s NSMutableArray load framework set aList to {1, 2, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, 16, 17, 18, 20} set aRes to calcGaps(aList) of me –> {19, 13} on calcGaps(aList as list) set nArray to (NSMutableArray’s arrayWithArray:aList) set maxRes to (nArray’s valueForKeyPath:"@max.self")’s intValue() set minRes to (nArray’s valueForKeyPath:"@min.self")’s intValue() –最小値から最大値までの連番リスト作成 set theIndexSet to NSIndexSet’s indexSetWithIndexesInRange:{minRes, maxRes} set theList to (SMSForder’s arrayWithIndexSet:theIndexSet) as list –補集合 set aSet to NSMutableSet’s setWithArray:theList set bSet to NSMutableSet’s setWithArray:nArray aSet’s minusSet:bSet return aSet’s allObjects() as list end calcGaps |
隣接Rangeの検出じっけん
AppleScript名:隣接Rangeの検出じっけん |
— Created 2017-12-12 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set aRange to current application’s NSMakeRange(10, 80) –original range set bRange to current application’s NSMakeRange(9, 81) –entended range (to detect neighbor) set cRange to current application’s NSMakeRange(89, 30) –target range set aRes to current application’s NSIntersectionRange(bRange, cRange) |
NSRangeの基礎的な処理
AppleScript名:NSRangeの基礎的な処理 |
— Created 2017-12-12 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set aRange to current application’s NSMakeRange(0, 100) –> {location:0, length:100} set maxRange to current application’s NSMaxRange(aRange) –最大値 –> 100 set rangeStr to (current application’s NSStringFromRange(aRange)) as string –> "{0, 100}" set bRange to current application’s NSRangeFromString(rangeStr) –> {location:0, length:100} set cRange to current application’s NSMakeRange(50, 80) set cRes to current application’s NSEqualRanges(aRange, cRange) –> false set dRes to current application’s NSEqualRanges(aRange, bRange) –> true set eRes to current application’s NSIntersectionRange(aRange, cRange) –> {location:50, length:50} set fRes to current application’s NSLocationInRange(50, aRange) –> true set gRes to current application’s NSLocationInRange(120, aRange) –> false set hRes to current application’s NSUnionRange(aRange, cRange) –> {location:0, length:130} |
NSRangeの拡張、隣接検出
AppleScript名:NSRangeの拡張、隣接検出 |
— Created 2018-1-12 by Takaaki Naganoya — 2018 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" –http://piyocast.com/as/archives/5118 set aRange to current application’s NSMakeRange(10, 80) –original range –> {location:10, |length|:80} set bRange to extendRange(aRange) of me –Extended range –> {location:9, |length|:81} set cRange to current application’s NSMakeRange(90, 30) –compare target range –> {location:90, |length|:30} set aRes to current application’s NSIntersectionRange(aRange, cRange) –> {location:0, |length|:0}–Not adjacent set bRes to current application’s NSIntersectionRange(bRange, cRange) –> {location:90, |length|:1}–Adjacent –NSRangeの開始点とサイズを拡張 on extendRange(aRange) set aLoc to location of aRange set aLen to |length| of aRange if aLoc > 0 then set aLoc to aLoc – 1 end if set aLen to aLen + 2 set bRange to current application’s NSMakeRange(aLoc, aLen) return bRange end extendRange |
MaxRangeを取得する
AppleScript名:MaxRangeを取得する |
use AppleScript version "2.4" use scripting additions use framework "Foundation" set aRange to current application’s NSMakeRange(10, 80) –original range set aMaxX to current application’s NSMaxRange(aRange) –> 90 –set aMinX to current application’s NSMinRange(aRange)–最小はない |
TextEditの文章のうち赤くマークされた行をカウントする
macOS標準装備のテキストエディットで編集中の文章のうち、赤くマークされた行をカウントするAppleScriptです。
RGB値で抽出してカウントします。30年前からやっている原始レベルの処理です。値が同じ箇所を抽出するだけならバカでもできます。
いま現在AppleScriptで実現しているのは、「赤っぽい色」の箇所を抽出する「カラードメイン処理」です。RGB値だと値が1つでも違っていたら別物として判定されてしまいますが、カラードメイン処理では「赤系統の色」を計算して判定できるものです。
→ TextEditの文章のうち赤っぽい色でマークされた箇所をピックアップする v2
→ TextEditの文章のうち赤っぽい色でマークされた行をカウントする v2
→ TextEdit本文色に応じて青っぽい色は男性の音声で、赤っぽい色は女性の音声で読み上げ
AppleScript名:TextEditの文章のうち赤くマークされた行をカウントする |
set rNum to 0
tell application "TextEdit" tell text of document 1 set pCount to (count paragraphs) repeat with i from 1 to pCount set aCol to color of paragraph i if aCol = {65535, 0, 0} then –{r, g, b} set rNum to rNum + 1 end if end repeat end tell end tell return {pCount, rNum} |