AppleScript名:2つの1D Listの共通項を返す v2 |
— Created 2014-11-18 by Takaaki Naganoya — 2014 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" –aList内に重複項目、bList内に重複項目が存在していた場合に、それが検出される可能性もある set aList to {1, 2, 3, 4, 5} set bList to {0, 1, 2, 4, 5, 6} set aRes to getSameItemsInLists(aList, bList) of me –> {2, 5, 1, 4} on getSameItemsInLists(aList as list, bList as list) –ASオブジェクトをCocoaオブジェクトに変換 set aArray to current application’s NSArray’s arrayWithArray:aList set bArray to current application’s NSArray’s arrayWithArray:bList — まとめる set allSet to current application’s NSMutableSet’s setWithArray:aArray allSet’s addObjectsFromArray:bArray –重複する要素のみ抜き出す set duplicateSet to current application’s NSMutableSet’s setWithArray:aArray duplicateSet’s intersectSet:(current application’s NSSet’s setWithArray:bArray) –重複部分だけを返す set resArray to duplicateSet’s allObjects() set resList to resArray as list return resList end getSameItemsInLists |
カテゴリー: list
1D Listから重複項目のみ抽出 v2
AppleScript名:1D Listから重複項目のみ抽出 v2 |
— Created 2017-11-07 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" –http://piyocast.com/as/archives/4955 property NSCountedSet : a reference to current application’s NSCountedSet set aList to {1, 2, 3, 4, 1, 2, 3, 4, 5, 1, 2, 3, 1, 2, 3, 8, 10, -2} set aRes to returnDuplicatesOnly(aList) of me –> {1, 2, 3, 4} on returnDuplicatesOnly(aList as list) set aSet to NSCountedSet’s alloc()’s initWithArray:aList set bList to (aSet’s allObjects()) as list set dupList to {} repeat with i in bList set aRes to (aSet’s countForObject:i) if aRes > 1 then set the end of dupList to (contents of i) end if end repeat return dupList end returnDuplicatesOnly |
2つの1D Listから重複項目のみ抽出。個別の重複項目のチェックつき
AppleScript名:2つの1D Listから重複項目のみ抽出。個別の重複項目のチェックつき |
— Created 2017-12-17 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" property NSCountedSet : a reference to current application’s NSCountedSet set aList to {1, 2, 3, 4, 5} set bList to {0, 1, 2, 4, 5, 6} –それぞれのListに重複項目が存在するかをチェック set aRes1 to returnDuplicatesOnly(aList) of me set bRes1 to returnDuplicatesOnly(bList) of me if {aRes1, bRes1} is not equal to {{}, {}} then return false –aListとbListを連結したListで重複が存在するかをチェック set aArray to current application’s NSArray’s arrayWithArray:aList set bArray to current application’s NSArray’s arrayWithArray:bList set cArray to aArray’s arrayByAddingObjectsFromArray:bArray set cRes to returnDuplicatesOnly(cArray) of me –> {5, 1, 2, 4} on returnDuplicatesOnly(aList as list) set aSet to NSCountedSet’s alloc()’s initWithArray:aList set bList to (aSet’s allObjects()) as list set dupList to {} repeat with i in bList set aRes to (aSet’s countForObject:i) if aRes > 1 then set the end of dupList to (contents of i) end if end repeat return dupList end returnDuplicatesOnly |
ASOCでSystemのアラートサウンド名称を取得して同時に鳴らす v2.2a(Array内のオブジェクトに一括指令)
使用中のコンピュータにインストールされているアラートサウンド(警告音)の名称を取得して、音を同時に鳴らすAppleScriptです。
NSArrayに入れたオブジェクトに対して一括でメッセージを送るという処理の実験でもあります。
AppleScript名:ASOCでSystemのアラートサウンド名称を取得して同時に鳴らす v2.2a(Array内のオブジェクトに一括指令) |
— Created 2015-11-06 by Takaaki Naganoya — Modified 2015-11-07 by Shane Stanley–Recovery the compatibility for OS X 10.10.x — Modified 2015-11-07 by Takaaki Naganoya–NSArrayからvalueForKeyで加工しつつ一気にArrayで値を返す内容を検証 — Modified 2015-11-07 by Shane Stanley–Cooler processing — 2015 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "AppKit" set nList to getSystemAlertSoundNames() of me –> {"Basso", "Blow", "Bottle", "Frog", "Funk", "Glass", "Hero", "Morse", "Ping", "Pop", "Purr", "Sosumi", "Submarine", "Tink"} set aArray to current application’s NSMutableArray’s new() repeat with i in nList (aArray’s addObject:(current application’s NSSound’s soundNamed:i)) end repeat aArray’s makeObjectsPerformSelector:"play" withObject:0 –同時に鳴らす –Get System Alert FileName List on getSystemAlertSoundNames() set aExt to "aiff" — no dot set aFol to "/System/Library/Sounds" set fList to getFileNameListFromPOSIXpath(aFol, aExt) of me return fList end getSystemAlertSoundNames –Get File Name List from POSIX path and file Extensions on getFileNameListFromPOSIXpath(aFol, aExt) set aFM to current application’s NSFileManager’s defaultManager() set aURL to current application’s |NSURL|’s fileURLWithPath:aFol set urlArray to aFM’s contentsOfDirectoryAtURL:aURL includingPropertiesForKeys:{} options:(current application’s NSDirectoryEnumerationSkipsHiddenFiles) |error|:(missing value) set thePred to current application’s NSPredicate’s predicateWithFormat:"pathExtension == [c]%@" argumentArray:{aExt} set anArray to urlArray’s filteredArrayUsingPredicate:thePred set bArray to (anArray’s valueForKeyPath:"lastPathComponent.stringByDeletingPathExtension") –Cool !! return bArray as list end getFileNameListFromPOSIXpath |
1Dリスト内の数値をすべて文字列化
AppleScript名:1Dリスト内の数値をすべて文字列化 |
— Created 2017-01-30 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set anArray to current application’s NSArray’s arrayWithArray:{1, 2, 3} set aVal to anArray’s valueForKey:"stringValue" –> (NSArray) {"1", "2", "3"} |
1Dリスト内のテキストの文字列長を取得
AppleScript名:1Dリスト内のテキストの文字列長を取得 |
— Created 2017-01-30 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set anArray to current application’s NSArray’s arrayWithArray:{"aaaa1", "sss2", "aaaa3"} set aVal to anArray’s valueForKeyPath:"length" –> (NSArray) {5, 4, 5} |
1Dリスト内の数値をすべて浮動小数点表現に
AppleScript名:1Dリスト内の数値をすべて浮動小数点表現に |
— Created 2017-01-30 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set anArray to current application’s NSArray’s arrayWithArray:{1, 2, 3} set aVal to anArray’s valueForKey:"doubleValue" –> (NSArray) {1.0, 2.0, 3.0} |
1Dリスト内の数値をすべて整数に
AppleScript名:1Dリスト内の数値をすべて整数に |
— Created 2017-01-30 16:01:49 +0900 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set anArray to current application’s NSArray’s arrayWithArray:{1.0, 0.0, 1.0, -1.0} set aVal to anArray’s valueForKey:"intValue" –> (NSArray) {1, 0, 1, -1} |
2Dリスト内の要素をすべて加算
AppleScript名:2Dリスト内の要素をすべて加算 |
— Created 2017-10-01 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set aList to {{1, 2, 3, 4, 5, 6, 4}, {1, 2, 3, 4, 5, 6}, {1, 2, 3, 4, 5, 16, 0, 0}} set aRes to getTotalValueFrom2DArray(aList) of me –> 77 –total value is 0 on getTotalValueFrom2DArray(aList) set anArray to current application’s NSArray’s arrayWithArray:aList set eRes to (anArray’s valueForKeyPath:"@unionOfArrays.self")’s valueForKeyPath:"@sum.self" return eRes as integer end getTotalValueFrom2DArray |
2Dリスト内の要素の平均値を求める
AppleScript名:2Dリスト内の要素の平均値を求める |
— Created 2017-10-01 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set aList to {{1, 2, 3, 4, 5, 6, 4}, {1, 2, 3, 4, 5, 6}, {1, 2, 3, 4, 5, 16, 0, 0}} –7,6, 8 items set aRes to getAverageValueFrom2DArray(aList) of me –> 4 –average value is 0 on getAverageValueFrom2DArray(aList) set anArray to current application’s NSArray’s arrayWithArray:aList set eRes to (anArray’s valueForKeyPath:"@unionOfArrays.self")’s valueForKeyPath:"@avg.self" return eRes as integer end getAverageValueFrom2DArray |
2Dリスト内の要素のすべての要素をカウント
AppleScript名:2Dリスト内の要素のすべての要素をカウント |
— Created 2017-10-01 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" –set aList to {{1, 2, 3, 4, 5, 6, 4}, {1, 2, 3, 4, 5, 6}, {1, 2, 3, 4, 5, 16, 0, 0}} set aList to {{{1, 2}, {3, 4}, {5, 6, 4}}, {{1, 2}, {3, 4, 5, 6}, {{1, 2}, {3, 4, 5}, {16, 0, 0}}}} set aRes to countEveryItemOf2DArray(aList) of me –> 21 on countEveryItemOf2DArray(aList) set bList to FlattenList(aList) of me return (length of bList) end countEveryItemOf2DArray –By Paul Berkowitz –2009年1月27日 2:24:08:JST –Re: Flattening Nested Lists on FlattenList(aList) set oldDelims to AppleScript’s text item delimiters set AppleScript’s text item delimiters to {"????"} set aString to aList as text set aList to text items of aString set AppleScript’s text item delimiters to oldDelims return aList end FlattenList |
2Dリスト内の要素のうち最大値を求める
AppleScript名:2Dリスト内の要素のうち最大値を求める |
— Created 2017-10-01 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set aList to {{1, 2, 3, 4, 5, 6, 4}, {1, 2, 3, 4, 5, 6}, {1, 2, 3, 4, 5, 16, 0, 0}} –7,6, 8 items set aRes to getMaxValueFrom2DArray(aList) of me –> 16 –max value is 16 on getMaxValueFrom2DArray(aList) set anArray to current application’s NSArray’s arrayWithArray:aList set eRes to (anArray’s valueForKeyPath:"@unionOfArrays.self")’s valueForKeyPath:"@max.self" return eRes as integer end getMaxValueFrom2DArray |
2Dリスト内の要素のうち最小値を求める
AppleScript名:2Dリスト内の要素のうち最小値を求める |
— Created 2017-10-01 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set aList to {{1, 2, 3, 4, 5, 6, 4}, {1, 2, 3, 4, 5, 6}, {1, 2, 3, 4, 5, 16, 0, 0}} –7,6, 8 items set aRes to getMinValueFrom2DArray(aList) of me –> 0 –min value is 0 on getMinValueFrom2DArray(aList) set anArray to current application’s NSArray’s arrayWithArray:aList set eRes to (anArray’s valueForKeyPath:"@unionOfArrays.self")’s valueForKeyPath:"@min.self" return eRes as integer end getMinValueFrom2DArray |
ASOCでテキストとリストの変換
AppleScript名:ASOCでテキストとリストの変換 |
— Created 2015-10-14 by Takaaki Naganoya — 2015 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set aList to {"あああああ", "いいいいいいいい", "ううううううううううう", "えええええええええええ", "おおおおおおおおおお"} set aRes to my retStrFromArrayWithDelimiter(aList, return) (* "あああああ いいいいいいいい ううううううううううう えええええええええええ おおおおおおおおおお" *) set bList to my parseByDelim(aRes, return) –> {"あああああ", "いいいいいいいい", "ううううううううううう", "えええええええええええ", "おおおおおおおおおお"} –リストを指定デリミタをはさんでテキスト化 on retStrFromArrayWithDelimiter(aList, aDelim) set anArray to current application’s NSArray’s arrayWithArray:aList set aRes to anArray’s componentsJoinedByString:aDelim return aRes as text end retStrFromArrayWithDelimiter on retArrowText(aList, aDelim) –自分のASでよく使うハンドラ名称なので、同じものを用意 return my retStrFromArrayWithDelimiter(aList, aDelim) end retArrowText –テキストを指定デリミタでリスト化 on parseByDelim(aData, aDelim) set aText to current application’s NSString’s stringWithString:aData set aList to aText’s componentsSeparatedByString:aDelim return aList as list end parseByDelim |
1Dリストを指定個数ごとにリスト化して2D化 v3.2
AppleScript名:1Dリストを指定個数ごとにリスト化して2D化 v3.2 |
use AppleScript version "2.4" use scripting additions use framework "Foundation" use bPlus : script "BridgePlus" –https://www.macosxautomation.com/applescript/apps/BridgePlus.html script spd property aList : {} end script –テストデータ作成 set aList of spd to {} repeat 100000 times set the end of aList of spd to (random number from 10000 to 9999) end repeat set a1Dat to current application’s NSDate’s timeIntervalSinceReferenceDate() –時間計測 set aGroupNum to 100000 set b to (current application’s SMSForder’s subarraysFrom:(aList of spd) groupedBy:aGroupNum |error|:(missing value)) as list set b1Dat to current application’s NSDate’s timeIntervalSinceReferenceDate() –時間計測 set c1Dat to b1Dat – a1Dat –> 0.688337981701 |
2Dリストから、指定カラムのデータを削除する v3
AppleScript名:2Dリストから、指定カラムのデータを削除する v3 |
— Created 2017-11-25 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" –http://piyocast.com/as/archives/5000 set aList to {{1, 2, 3}, {2, 3, 4}, {3, 4, 5}} set removeIndex to 3 –1 based index set bList to removeColumnFrom2DArray(aList, removeIndex) of me –> {{1, 2}, {2, 3}, {3, 4}} on removeColumnFrom2DArray(aList as list, removeIndex as integer) set newArray to {} set realIndex to removeIndex – 1 –index conversion from 1-based to 0-based repeat with i in aList set anArray to (current application’s NSMutableArray’s arrayWithArray:i) (anArray’s removeObjectAtIndex:realIndex) set the end of newArray to anArray as list of string or string –as anything end repeat return newArray end removeColumnFrom2DArray |
リスト中の指定アイテムを削除する(登場アイテム番号自動検索)
AppleScript名:リスト中の指定アイテムを削除する(登場アイテム番号自動検索) |
— Created 2017-04-11 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set aTargValue to 2 set anArray to current application’s NSMutableArray’s arrayWithArray:{5, 2, 1, 3, 4} set aInd to anArray’s indexOfObject:aTargValue if aInd = current application’s NSNotFound or (aInd as real > 9.99999999E+8) then return {} anArray’s removeObjectAtIndex:aInd return anArray as list –> {5, 1, 3, 4} |
2つの1D List同士の消し込み
AppleScript名:2つの1D List同士の消し込み |
— Created 2017-10-29 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set list1 to {5, 1, 2, 3, 1, 6, 2, 7, 8, 2, 6, 5, 4} set list2 to {6, 7, 8} set result1 to my removeItemsInList:list2 fromList:list1 –> {5, 3, 1, 4, 2} set result2 to removeItems for list2 from list1 –> {5, 3, 1, 4, 2} set result3 to removeItemsFrom(list1, list2) of me –> {5, 3, 1, 4, 2} –Objective-Cライクなパラメータ記述 on removeItemsInList:list2 fromList:list1 set set2 to current application’s NSSet’s setWithArray:list2 set set1 to current application’s NSMutableSet’s setWithArray:list1 set1’s minusSet:set2 return (set1’s allObjects()) as list end removeItemsInList:fromList: –無意味区による装飾 on removeItems for list2 from list1 return my removeItemsInList:list2 fromList:list1 end removeItems –Pure AS風のパラメータ記述 on removeItemsFrom(list1, list2) return my removeItemsInList:list2 fromList:list1 end removeItemsFrom |
指定要素のみを削除(predicateでfilter) v3
AppleScript名:指定要素のみを削除(predicateでfilter) v3 |
— Created 2017-11-03 by Takaaki Naganoya — Modified 2017-11-05 by Shane Stanley use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "AppKit" property NSPredicate : a reference to current application’s NSPredicate property NSArray : a reference to current application’s NSArray set aList to {"Apple", "Lemon", "Aple", "Lum", "Apple"} set aTarg to "Apple" –string set rList to removeElementsFrom1DArray(aList, aTarg) of me –> {"Lemon", "Aple", "Lum"} set bList to {1, 2, 3, 1, 5} set bTarg to 1 –number set rList to removeElementsFrom1DArray(bList, bTarg) of me –> {2, 3, 5} set cList to {1, 2, 3, 1, 5} set cTarg to {1, 2, 3} –list of number set rList to removeElementsFrom1DArray(cList, cTarg) of me –> {5} set dList to {"Apple", "Lemon", "Aple", "Lum", "Apple"} set dTarg to {"Apple", "Lemon"} –list of string set rList to removeElementsFrom1DArray(dList, dTarg) of me –> {"Aple", "Lum"} on removeElementsFrom1DArray(aList as list, aTarg as {number, string, list}) set anArray to NSArray’s arrayWithArray:aList set aClass to class of aTarg if aClass = list then set thePred to NSPredicate’s predicateWithFormat_("NOT SELF IN %@", aTarg) else set thePred to NSPredicate’s predicateWithFormat_("SELF != %@", aTarg) end if set bList to (anArray’s filteredArrayUsingPredicate:thePred) as list return bList end removeElementsFrom1DArray |
1D List中のアイテムが指定リストに入っていたら抽出
AppleScript名:1D List中のアイテムが指定リストに入っていたら抽出 |
— Created 2017-11-03 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" property NSPredicate : a reference to current application’s NSPredicate property NSArray : a reference to current application’s NSArray set aList to {"Apple", "Lemon", "Apple", "Water Melon"} set aTarg to {"Apple", "Lemon"} –データの表記ゆらぎ対策に使える? set anArray to NSArray’s arrayWithArray:aList set aPred to NSPredicate’s predicateWithFormat_("SELF IN %@", aTarg) set bRes to (anArray’s filteredArrayUsingPredicate:aPred) as list –> {"Apple", "Lemon", "Apple"} |