| AppleScript名:Camel Case化 |
| — Created 2015-12-24 by Shane Stanley use AppleScript version "2.4" use scripting additions use framework "Foundation" camelCase("this is a pen") of me –> "thisIsAPen." on camelCase(aPhrase) set firstWord to current application’s NSString’s stringWithString:(word 1 of aPhrase) set firstWord to firstWord’s |lowercaseString|() set restOfPhrase to current application’s NSString’s stringWithString:(text from word 2 to -1 of aPhrase) set restOfPhrase to restOfPhrase’s capitalizedString()’s stringByReplacingOccurrencesOfString:space withString:"" set newPhrase to firstWord’s stringByAppendingString:restOfPhrase return (newPhrase’s stringByFoldingWithOptions:(current application’s NSDiacriticInsensitiveSearch) locale:(missing value)) as text end camelCase |
文字エンコーディングを自動判別してファイル読み込み v1.2.1
指定のテキストファイル(日本語テキストを想定)を、文字エンコーディングを自動判定して読み込むAppleScriptです。

これまで、日本語テキストファイルの文字エンコーディングを自動判定するのにテキストエディタをAppleScriptからコントロールして使っていましたが、それだと並列処理するAppleScriptの中で使用できないし、テキストエディタがない環境では実行することができません(ダウンロードすればいいだけの話ですが)。
さまざまな日本語テキストエディタのソースコードをながめて、文字コードの自動判定ロジックがわかってきたので、AppleScriptで実装してみました。さらに、独自の「文字化け判定ロジック」を考えたので、それを併用しています。
Step 1 伝統的な判定(勝ち抜け方式)
伝統的な文字コードの判定を順次行います。この、判定を行う順序が重要です。最初にISO2022JPのチェック、ついでEUC、UTF-8、シフトJISと、順次コード判定を行います。勝ち抜け方式で、妥当な文字エンコーディングとみなしたらそこで評価を打ち切ります。
Step 2 総当たり判定(全部評価してから考える)
UTF-16BE/LE/無印Unicodeについては、それぞれの文字エンコーディングで実際に評価してみて、文字化けしないものを採用します。独自ロジックにより文字化け状態をプログラム的に判断するのと、そのエンコーディングで読み込んだ結果をNSLinguisticTaggerで評価し、「ja」(日本語)と判定するかどうかの確認をすべてのエンコーディングで行なっています。
以上の2ステップでだいたい日本語テキストのエンコーディング自動判定ができています。
もし、寿司屋の湯のみ茶碗のように漢字で魚の名前を羅列した日本語テキストがUTF-16BEとかで書かれていたりすると自動判定できない可能性もありますが、青空文庫の日本語テキストやWebに掲載されているテクニカルな記事(アルファベットやカタカナの使用比率が高い)でテストしてみたところ、問題なく判定できています(実際に魚の名前のテキストを作って読み込んでみましたが、とくに問題はありませんでした)。
だいたい、日本語として意味をなしているようなテキストであれば、文字コードの自動判定はできているように思います。ただ、世間は広いので、本Scriptのロジックで判定できないようなケースもあるかもしれません。そのような時にはコメント欄やフォーラムへの報告をお願いいたします(対応するかどうかは別として。知り合いのテキストエディタ作者の方は、無償で公開しているソフトに文句を言われて「すぐに対応しろ!」だのと散々言われたそうで、、、、、、、)。
| AppleScript名:文字エンコーディングを自動判別してファイル読み込み v1.2.1 |
| — Created 2014-12-28 by Takaaki Naganoya — Modified 2014-12-29 by Shane Stanley — Modified 2015-10-03 by Takaaki Naganoya use AppleScript version "2.5" use scripting additions use framework "Foundation" set aPath to POSIX path of (choose file) set aRes to readJapanesTextFileWithGuessingEncoding(aPath) of me set bRes to aRes as string –Read Japanese text with detecting its text encoding on readJapanesTextFileWithGuessingEncoding(aPOSIXpath as string) –ISO2022JP check set aNSData to current application’s NSData’s dataWithContentsOfFile:aPOSIXpath set aDataLength to aNSData’s |length|() if aDataLength > 1024 then set aDataLength to 1024 –0x1B check set anNSString to current application’s NSString’s stringWithString:(character id 27) — 0x1B set theData to anNSString’s dataUsingEncoding:(current application’s NSUTF8StringEncoding) set theRange to aNSData’s rangeOfData:theData options:0 range:(current application’s NSMakeRange(0, aDataLength)) –found 0x1B in aNSData if |length| of theRange = 1 and location of theRange < aDataLength then set aStr to (current application’s NSString’s alloc()’s initWithData:aNSData encoding:(current application’s NSISO2022JPStringEncoding)) –21 if aStr is not equal to missing value then return (aStr as text) — ISO2022JP end if –EUC set resValue to (current application’s NSString’s alloc()’s initWithData:aNSData encoding:(current application’s NSJapaneseEUCStringEncoding)) –log resValue if resValue is not equal to missing value then return (resValue as text) –UTF-8 set resValue to (current application’s NSString’s alloc()’s initWithData:aNSData encoding:(current application’s NSUTF8StringEncoding)) –log resValue if resValue is not equal to missing value then return (resValue as text) –SHift JIS set resValue to (current application’s NSString’s alloc()’s initWithData:aNSData encoding:(current application’s NSShiftJISStringEncoding)) –log resValue if resValue is not equal to missing value then return (resValue as text) –UTF-16BE/LE/無印Unicodeは多数決を取る set resValue1 to (current application’s NSString’s alloc()’s initWithData:aNSData encoding:(current application’s NSUTF16BigEndianStringEncoding)) as text –log resValue1 set sample1 to getTextSample(resValue1) of me set lang1 to specifyLanguageOfText(sample1) of me set para1 to length of (paragraphs of sample1) set words1 to length of (words of sample1) set resValue2 to (current application’s NSString’s alloc()’s initWithData:aNSData encoding:(current application’s NSUTF16LittleEndianStringEncoding)) as text –log resValue2 set sample2 to getTextSample(resValue2) of me set lang2 to specifyLanguageOfText(sample2) of me set para2 to length of (paragraphs of sample2) set words2 to length of (words of sample2) set resValue3 to (current application’s NSString’s alloc()’s initWithData:aNSData encoding:(current application’s NSUnicodeStringEncoding)) as text –log resValue3 set sample3 to getTextSample(resValue3) of me set lang3 to specifyLanguageOfText(sample3) of me set para3 to length of (paragraphs of sample3) set words3 to length of (words of sample3) –文字および文法的に見て「日本語」ならそれを返す if lang1 = "ja" then return resValue1 if lang2 = "ja" then return resValue2 if lang3 = "ja" then return resValue2 –文字化けしたときには、日本語の「Word」として認識されづらく、Paragraphも少ない(1とか)なので条件で除外する if para1 is not equal to 1 then if (words1 ≤ words2) or (words1 ≤ words3) then return resValue1 end if end if if para2 is not equal to 1 then if (words2 ≤ words1) or (words2 ≤ words3) then return resValue2 end if end if if para3 is not equal to 1 then if (words3 ≤ words1) or (words3 ≤ words2) then return resValue3 end if end if return false (* –おまけ(未確認) set resValue to (current application’s NSString’s alloc()’s initWithData:aNSData encoding:(current application’s NSWindowsCP1251StringEncoding)) if resValue is not equal to missing value then return resValue set resValue to (current application’s NSString’s alloc()’s initWithData:aNSData encoding:(current application’s NSWindowsCP1252StringEncoding)) if resValue is not equal to missing value then return resValue set resValue to (current application’s NSString’s alloc()’s initWithData:aNSData encoding:(current application’s NSWindowsCP1253StringEncoding)) if resValue is not equal to missing value then return resValue set resValue to (current application’s NSString’s alloc()’s initWithData:aNSData encoding:(current application’s NSWindowsCP1254StringEncoding)) if resValue is not equal to missing value then return resValue set resValue to (current application’s NSString’s alloc()’s initWithData:aNSData encoding:(current application’s NSWindowsCP1250StringEncoding)) if resValue is not equal to missing value then return resValue return false *) end readJapanesTextFileWithGuessingEncoding on specifyLanguageOfText(aStr) set aNSstring to current application’s NSString’s stringWithString:aStr set tagSchemes to current application’s NSArray’s arrayWithObjects:(current application’s NSLinguisticTagSchemeLanguage) set tagger to current application’s NSLinguisticTagger’s alloc()’s initWithTagSchemes:tagSchemes options:0 tagger’s setString:aNSstring set aLanguage to tagger’s tagAtIndex:0 |scheme|:(current application’s NSLinguisticTagSchemeLanguage) tokenRange:(missing value) sentenceRange:(missing value) return aLanguage as text end specifyLanguageOfText on getTextSample(aText) set aLen to length of aText if aLen < 1024 then set bLen to aLen else set bLen to 1024 end if return (text 1 thru bLen of aText) end getTextSample |
UniversalDetectorで文字コード判定
UniversalDetector.frameworkを呼び出して指定のテキストファイルの文字コードを判定するAppleScriptです。
日本語のテキストで実験してみたところ、UTF-16系以外は正常に判定できています。日本語以外の言語を対象にした場合には使い手がありそうですが、日本語を対象にした場合には、「文字エンコーディングを自動判別してファイル読み込み v1.2.1」のほうが便利です。
–> UniversalDetector.framework
| AppleScript名:UniversalDetectorで文字コード判定 |
| — Created 2015-10-03 by Takaaki Naganoya — 2015 Piyomaru Software use AppleScript version "2.5" use scripting additions use framework "Foundation" use framework "UniversalDetector" –https://github.com/JanX2/UniversalDetector set aPath to (POSIX path of (choose file)) set aStr to current application’s NSString’s stringWithString:aPath set aDetector to current application’s UniversalDetector’s new() aDetector’s analyzeContentsOfFile:aStr set aStr to current application’s NSString’s localizedNameOfStringEncoding:(aDetector’s encoding()) –> (NSString) "日本語(EUC)" –> (NSString) "日本語(ISO 2022-JP)" –> (NSString) "日本語(Shift JIS)" –> (NSString) "Unicode(UTF-8)" –> (NSString) "キリル文字(Windows)" –NG。本当はUTF-16 no BOM –> (NSString) "中国語(GB 18030)"–NG。本当はUTF-16BE –> (NSString) "Unicode(UTF-16)" set bStr to aDetector’s MIMECharset() –> (NSString) "EUC-JP" –> (NSString) "ISO-2022-JP" –> (NSString) "Shift_JIS" –> (NSString) "UTF-8" –> (NSString) "windows-1251"–NG –> (NSString) "gb18030"–NG –> (NSString) "UTF-16" set aNum to (aDetector’s confidence()) * 100 –> 100.0–"EUC-JP" –> 100.0–"ISO-2022-JP" –> 100.0–"Shift_JIS" –> 100.0–"UTF-8" –> 5.271286144853–UTF-16 no BOM –> 100.0–NGだが100%といっている –> 100.0– "UTF-16" return {aStr as string, bStr as string, aNum} |
base64エンコード、デコード v3
テキストのBase64エンコード/デコードを行うAppleScriptです。
このような基本的なデータ変換機能についてはAppleScript標準で持っていないため、Classic MacOS時代にはOSAX(Scripting Additions)で、Mac OS X時代になってからは他の言語処理系の機能を借りたり、Mac OS X用のOSAXを利用していたりしましたが、現在のmacOSではCocoaの機能を呼び出すのが一番手頃になってきました。
# サードパーティ製のOSAX(Scripting Additions)サポートはmacOS 10.14で廃止になりました。macOS標準搭載のStandard Additionsのみ利用可能です
| AppleScript名:base64エンコード、デコード v3 |
| — Created 2015-07-27 by Takaaki Naganoya — Updated 2015-07-28 by Shane Stanley use AppleScript version "2.4" use scripting additions use framework "Foundation" set aStr to "ぴよまるソフトウェアPiyomaru Software" set theNSString to current application’s NSString’s stringWithString:aStr set theNSData to theNSString’s dataUsingEncoding:(current application’s NSUTF8StringEncoding) set bStr to base64StringFromFileString(aStr) of me return bStr –> "44G044KI44G+44KL44K944OV44OI44Km44Kn44KiUGl5b21hcnUgU29mdHdhcmU=" –set aFilePath to POSIX path of (choose file) –base64StringFromFileAtPath(aFilePath) of me –Base 64 Decode on detaFromBase64String(aStr) set dataFrom64 to current application’s NSData’s alloc()’s initWithBase64EncodedString:aStr options:(current application’s NSDataBase64DecodingIgnoreUnknownCharacters) set aStr to current application’s NSString’s alloc()’s initWithData:dataFrom64 encoding:(current application’s NSUTF8StringEncoding) return aStr as text –stringではなくtext end detaFromBase64String –Base64 Encode on base64StringFromFileAtPath(aFilePath) set aDataFromFile to current application’s NSData’s dataWithContentsOfFile:aFilePath set aBase64EncStr to aDataFromFile’s base64EncodedStringWithOptions:(current application’s NSDataBase64Encoding64CharacterLineLength) return aBase64EncStr as text end base64StringFromFileAtPath –Base64 Encode on base64StringFromFileString(aString) set bString to current application’s NSString’s stringWithString:aString set aData to bString’s dataUsingEncoding:(current application’s NSUTF8StringEncoding) set aBase64EncStr to aData’s base64EncodedStringWithOptions:(current application’s NSDataBase64Encoding64CharacterLineLength) return aBase64EncStr as text end base64StringFromFileString |
デコードしたQRコードのメールデータの各フィールドを取り出す v2.2
| AppleScript名:デコードしたQRコードのメールデータの各フィールドを取り出す v2.2 |
| — Created 2016-12-12 by Shane Stanley — Modified 2016-12-14 by edama2 — Modified 2017-01-12 by Takaaki Naganoya use AppleScript version "2.4" use scripting additions use framework "Foundation" set aStr to "こんにちは、ぴよまるです MATMSG:TO:hiyoko@piyocast.com;SUB:たいとる;BODY:ほんぶん;; Takaaki Naganoya iPhoneから送信" set aDict to (parseStrByParamlabelAndTail(aStr, "MATMSG:", ":", ";") of me) set eMailAddrs to (aDict’s valueForKey:"TO") as string–> "hiyoko@piyocast.com" set aSubject to (aDict’s valueForKey:"SUB") as string –> "たいとる" set aBody to (aDict’s valueForKey:"BODY") as string –> "ほんぶん" on parseStrByParamlabelAndTail(aParamStr, aDataHeader, aParamLabel, aParamTail) set theScanner to current application’s NSScanner’s scannerWithString:aParamStr set aDict to current application’s NSMutableDictionary’s |dictionary|() —Skip over the data header set {theResult, theKey} to theScanner’s scanUpToString:aDataHeader intoString:(reference) if theResult as boolean = false then return false –Error: Data header is not present theScanner’s scanString:aDataHeader intoString:(missing value) repeat until (theScanner’s isAtEnd as boolean) — terminate check, return the result (aDict) to caller set {theResult, theKey} to theScanner’s scanUpToString:aParamLabel intoString:(reference) — skip over separator theScanner’s scanString:aParamLabel intoString:(missing value) set {theResult, theValue} to theScanner’s scanUpToString:aParamTail intoString:(reference) if theValue is missing value then set theValue to "" — skip over separator theScanner’s scanString:aParamTail intoString:(missing value) aDict’s setObject:theValue forKey:theKey end repeat return aDict end parseStrByParamlabelAndTail |
date pickerによる日付選択

| AppleScript名:date pickerによる日付選択 |
| — Created 2015-08-20 by Shane Stanley use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "AppKit" use script "BridgePlus" — https://www.macosxautomation.com/applescript/apps/BridgePlus.html if not (current application’s NSThread’s isMainThread()) as boolean then display alert "This script must be run from the main thread." buttons {"Cancel"} as critical error number -128 end if — create a view set theView to current application’s NSView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, 100, 200)) — create date picker set datePicker to current application’s NSDatePicker’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, 100, 100)) — set style: choices are NSTextFieldAndStepperDatePickerStyle, NSClockAndCalendarDatePickerStyle, or NSTextFieldDatePickerStyle datePicker’s setDatePickerStyle:(current application’s NSClockAndCalendarDatePickerStyle) — set elements: choices include NSHourMinuteDatePickerElementFlag, NSHourMinuteSecondDatePickerElementFlag, NSTimeZoneDatePickerElementFlag, NSYearMonthDatePickerElementFlag, and NSEraDatePickerElementFlag datePicker’s setDatePickerElements:((current application’s NSYearMonthDayDatePickerElementFlag) + (current application’s NSHourMinuteSecondDatePickerElementFlag as integer)) — set initial date datePicker’s setDateValue:(current application’s NSDate’s |date|()) — get the size it needs set theSize to datePicker’s fittingSize() –resize the picker and view accordingly theView’s setFrameSize:theSize datePicker’s setFrameSize:theSize — add the picker to the view theView’s setSubviews:{datePicker} — create an alert set theAlert to current application’s NSAlert’s alloc()’s init() — set up alert tell theAlert its setMessageText:"Pick a date" its setInformativeText:"Any date" its addButtonWithTitle:"OK" its addButtonWithTitle:"Cancel" its setAccessoryView:theView end tell — show alert in modal loop set returnCode to theAlert’s runModal() if returnCode = (current application’s NSAlertSecondButtonReturn) then error number -128 — retrieve date set theDate to ASify from (datePicker’s dateValue()) — or simply coerce to date in 10.11 –> date "2015年8月20日木曜日 19:43:58" |
リストから選択してアイテム番号を返す
AppleScriptの配列変数であるリスト型の変数(ただし、1次元)から選択し、選択した要素のアイテム番号(1はじまり)を数値で返すAppleScriptです。
AppleScriptに標準で用意されている「choose from list」コマンドは、選択した項目の文字列を返してきますが、その仕様だと困るケースがけっこうあります。
選択された要素の内容(文字列)ではなく、何項目(インデックス、アイテム番号)が選択されたかという情報を返してきてほしいところです。
そこで、ないなら作ってしまえばよいわけで、そういう仕様のルーチンを自分で作っておいたわけです。
| AppleScript名:リストから選択してアイテム番号を返す |
| set aList to {"red", "blue", "green", "white"} set aMes to "項目を選択してください" set aRes to retItemFromListByItemNo(aList, aMes) of me –> 3 (選択したアイテムの番号(1はじまり)が返る –リストから選択してアイテム番号を返す on retItemFromListByItemNo(aList, aMes) set aRes to choose from list aList with prompt aMes if aRes = false then return 0 set aRes to contents of item 1 of aRes set hitNum to 1 repeat with i in aList set j to contents of i if j is equal to aRes then exit repeat end if set hitNum to hitNum + 1 end repeat return hitNum end retItemFromListByItemNo |
ステータスバーのじっけん2

| AppleScript名:ステータスバーのじっけん2 |
| — Created 2017-03-03 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "AppKit" property aStatusItem : missing value on run init() of me end run on init() set aList to {"Piyomaru", "Software", "", "Takaaki", "Naganoya", "", "Quit"} set aStatusItem to current application’s NSStatusBar’s systemStatusBar()’s statusItemWithLength:(current application’s NSVariableStatusItemLength) aStatusItem’s setTitle:"🚗" aStatusItem’s setHighlightMode:true aStatusItem’s setMenu:(createMenu(aList) of me) end init on createMenu(aList) set aMenu to current application’s NSMenu’s alloc()’s init() set aCount to 1 repeat with i in aList set j to contents of i if j is not equal to "" then if j = "Quit" then set aMenuItem to (current application’s NSMenuItem’s alloc()’s initWithTitle:j action:"actionHandler:" keyEquivalent:"") –(aMenuItem’s setKeyEquivalentModifierMask:(current application’s NSControlKeyMask)) else set aMenuItem to (current application’s NSMenuItem’s alloc()’s initWithTitle:j action:"actionHandler:" keyEquivalent:"") end if else set aMenuItem to (current application’s NSMenuItem’s separatorItem()) end if (aMenuItem’s setTarget:me) (aMenuItem’s setTag:aCount) (aMenu’s addItem:aMenuItem) if j is not equal to "" then set aCount to aCount + 1 end if end repeat return aMenu end createMenu on actionHandler:sender set aTag to tag of sender as integer set aTitle to title of sender as string if aTitle is not equal to "Quit" then display dialog aTag as string else current application’s NSStatusBar’s systemStatusBar()’s removeStatusItem:aStatusItem end if end actionHandler: |
なろう系ルビタグを置換
| AppleScript名:なろう系ルビタグを置換 |
| — Created 2018-01-14 by Takaaki Naganoya — 2018 Piyomaru Software use AppleScript version "2.5" use scripting additions use framework "Foundation" property NSString : a reference to current application’s NSString property NSScanner : a reference to current application’s NSScanner property NSMutableArray : a reference to current application’s NSMutableArray property NSRegularExpressionSearch : a reference to current application’s NSRegularExpressionSearch property NSNumberFormatterRoundUp : a reference to current application’s NSNumberFormatterRoundUp set aStr to " 数多国ある西方諸国だが、元を辿ればとある一つの国へとつながっていた。それは幻晶騎士(シルエットナイト)の力により西方の地に覇をとなえた人類が作り上げた超巨大国家、その名を“ファダーアバーデン”という。 西方暦一二八九年の現在において西方諸国を構成する主要国家、“ジャロウデク王国”、“クシェペルカ王国”、“ロカール諸国連合”、“|孤独なる十一《イレブンフラッグス》”などの国々は、全てかの巨大国家が分裂してできた残滓なのである。" –set aStr to getEditorText() –"|○o○o○《XXXXX》" –> "XXXXX" set bRes to trimStrHeaderFromTo(aStr, "|", "《", "》") of me –"aaaaa○○○(XXXXX)" –> "XXXXX" set cStr to trimStrHeaderFromToForward(bRes, "(", ")") of me (* " 数多国ある西方諸国だが、元を辿ればとある一つの国へとつながっていた。それはシルエットナイトの力により西方の地に覇をとなえた人類が作り上げた超巨大国家、その名を“ファダーアバーデン”という。 西方暦一二八九年の現在において西方諸国を構成する主要国家、“ジャロウデク王国”、“クシェペルカ王国”、“ロカール諸国連合”、“イレブンフラッグス”などの国々は、全てかの巨大国家が分裂してできた残滓なのである。" *) –"|○o○o○《XXXXX》" –> "XXXXX" on trimStrHeaderFromTo(aParamStr, headerStr, fromStr, toStr) set theScanner to NSScanner’s scannerWithString:aParamStr set anArray to NSMutableArray’s array() repeat until (theScanner’s isAtEnd as boolean) set {theResult, theKey} to theScanner’s scanUpToString:headerStr intoString:(reference) theScanner’s scanString:fromStr intoString:(missing value) set {theResult, theValue} to theScanner’s scanUpToString:fromStr intoString:(reference) if theValue is missing value then set theValue to "" theScanner’s scanString:fromStr intoString:(missing value) anArray’s addObject:theValue end repeat if anArray’s |count|() = 0 then return aParamStr copy aParamStr to curStr repeat with i in (anArray as list) set curStr to repChar(curStr, i & fromStr, "") of me end repeat set curStr to repChar(curStr, toStr, "") of me return curStr end trimStrHeaderFromTo –"aaaaa○○○(XXXXX)" –> "XXXXX" on trimStrHeaderFromToForward(aParamStr, fromStr, toStr) set theScanner to NSScanner’s scannerWithString:aParamStr set anArray to NSMutableArray’s array() repeat until (theScanner’s isAtEnd as boolean) set {theResult, theKey} to theScanner’s scanUpToString:fromStr intoString:(reference) set curLoc to (theScanner’s scanLocation()) + 1 –scan back to different kind of character set prevKind to detectCharKindMain(text (curLoc – 1) of aParamStr) of me repeat with i from curLoc – 2 to 1 by -1 set aStr to text i of aParamStr set curKind to detectCharKindMain(aStr) of me if prevKind is not equal to curKind then exit repeat end if end repeat try set tmpStr to text (i + 1) thru curLoc of aParamStr theScanner’s scanString:fromStr intoString:(missing value) set {theResult, theValue} to theScanner’s scanUpToString:fromStr intoString:(reference) if theValue is missing value then set theValue to "" theScanner’s scanString:fromStr intoString:(missing value) anArray’s addObject:tmpStr end try end repeat if anArray’s |count|() = 0 then return aParamStr copy aParamStr to curStr repeat with i in (anArray as list) set curStr to repChar(curStr, i, "") of me end repeat set curStr to repChar(curStr, toStr, "") of me return curStr end trimStrHeaderFromToForward on repChar(aStr, targStr, repStr) set aString to current application’s NSString’s stringWithString:aStr set bString to aString’s stringByReplacingOccurrencesOfString:targStr withString:repStr set cString to bString as string return cString end repChar –文字種別判定 on detectCharKindMain(aStr) set s1Res to chkKanji(aStr) of me set s2Res to chkKatakana(aStr) of me set s3Res to chkHiragana(aStr) of me set s4Res to chkLineFeed(aStr) of me set s5Res to chkSpecialSign(aStr) of me set s6Res to chkAlphaNumeric(aStr) if s1Res = true then set curKind to "Kanji" else if s2Res = true then set curKind to "Katakana" else if s3Res = true then set curKind to "Hiragana" else if s4Res = true then set curKind to "Line Feed" else if s5Res = true then set curKind to "Sign" else if s6Res = true then set curKind to "Alpha Numeric" end if return curKind end detectCharKindMain on chkKanji(aChar) return detectCharKind(aChar, "[一-龠]") of me end chkKanji on chkHiragana(aChar) return detectCharKind(aChar, "[ぁ-ん]") of me end chkHiragana on chkKatakana(aChar) return detectCharKind(aChar, "[ァ-ヶ]") of me end chkKatakana on chkLineFeed(aChar) return aChar is in {string id 10, string id 13, string id 13 & string id 10} end chkLineFeed on chkSpecialSign(aChar) return aChar is in {"「", "」", "『", "』", "ー", "―", "〜", "~", "!", "?", "&", "/", "《", "》", "#", "…", "・", "♪", "。", "、", ".", "々", "“", "”", "*", "(", ")", "(", ")", " ", " ", "§", "【", "】", "■", "%", "≒"} end chkSpecialSign on chkAlphaNumeric(aChar) return detectCharKind(aChar, "[a-zA-Z0-9a-zA-Z0-9]") of me –半角全角英数字 end chkAlphaNumeric on detectCharKind(aChar, aPattern) set aChar to NSString’s stringWithString:aChar set searchStr to NSString’s stringWithString:aPattern set matchRes to aChar’s rangeOfString:searchStr options:(NSRegularExpressionSearch) if matchRes’s location() = (current application’s NSNotFound) or (matchRes’s location() as number) > 9.99999999E+8 then return false else return true end if end detectCharKind on getEditorText() tell application "CotEditor" if (count every document) = 0 then return false tell front document return contents end tell end tell end getEditorText |
指定写真をQRコード認識してエンコードされたデータをデコード

–> {“MEMORY:(メモ文字列)
NAME1:(姓名)
NAME2:(姓名読み)
TEL1:(電話番号1)
MAIL1:(メールアドレス1)
TEL2:(電話番号2)
MAIL2:(メールアドレス2)
MECARD:N:(姓),(名);SOUND:(姓読み),(名読み);TEL:(電話番号1);TEL:(電話番号2);EMAIL:(メールアドレス1);EMAIL:(メールアドレス2);NOTE:(メモ文字列) ;;”}
| AppleScript名:指定写真をQRコード認識してエンコードされたデータをデコード |
| use AppleScript version "2.4" use framework "Foundation" use framework "QuartzCore" use scripting additions –画像選択 set inputFile to choose file of type {"public.image"} –QRコード検出 set recogRes to qrcodeDetect(inputFile) of me –> (* {"http://www.su-gomori.com \nMEBKM:TITLE:スゴモリ;URL:http¥://www.su-gomori.com;;"} *) on qrcodeDetect(inputFile) –画像オープン set imageRef to openImageFile(inputFile) — 検出器のオプションを NSDictonary で作成 set optDic1 to current application’s NSDictionary’s dictionaryWithObject:(current application’s CIDetectorAccuracyHigh) forKey:(current application’s CIDetectorAccuracy) set faceDetector to current application’s CIDetector’s detectorOfType:(current application’s CIDetectorTypeQRCode) context:(missing value) options:optDic1 — QRコードの検出を行う際のオプションを NSDictonary で作成 set optDic2 to current application’s NSDictionary’s dictionaryWithObject:(current application’s CIDetectorImageOrientation) forKey:"Orientation" — QRコード検出を実行 set faceArray to faceDetector’s featuresInImage:imageRef options:optDic2 set fList to {} — 検出されたQRコードの位置とサイズをログに出力 repeat with i from 1 to (count of faceArray) set face to item i of faceArray set bRec to (face’s messageString()) as string –set cRec to retURLdecodedStrings(bRec) of me –URLエンコード対策 set the end of fList to bRec end repeat return fList end qrcodeDetect on openImageFile(imageFile) — imageFile: POSIX path 形式のファイルパス — aliasをURLに変換 set fileURL to current application’s |NSURL|’s fileURLWithPath:(POSIX path of imageFile) — CIImage を生成 return current application’s CIImage’s alloc()’s initWithContentsOfURL:fileURL end openImageFile on retURLencodedStrings(aText) set aStr to current application’s NSString’s stringWithString:aText set encodedStr to aStr’s stringByAddingPercentEncodingWithAllowedCharacters:(current application’s NSCharacterSet’s alphanumericCharacterSet()) return encodedStr as text end retURLencodedStrings on retURLdecodedStrings(aURLencodedStr) set aStr to current application’s NSString’s stringWithString:aURLencodedStr set aDecoded to aStr’s stringByRemovingPercentEncoding() return aDecoded as text end retURLdecodedStrings |
指定文字列からQRコード画像(PNG)をデスクトップに作成する(日本語を含むデータ)v2_画像拡大倍率指定

| AppleScript名:指定文字列からQRコード画像(PNG)をデスクトップに作成する(日本語を含むデータ)v2_画像拡大倍率指定 |
| — Created 2016-03-16 by Takaaki Naganoya — Modified 2017-01-15 by Takaaki Naganoya — 2016 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "AppKit" use framework "QuartzCore" –QRCodeを作成 set a to "MEMORY:(メモ文字列) NAME1:(姓名) NAME2:(姓名読み) TEL1:(電話番号1) MAIL1:(メールアドレス1) TEL2:(電話番号2) MAIL2:(メールアドレス2) MECARD:N:(姓),(名);SOUND:(姓読み),(名読み);TEL:(電話番号1);TEL:(電話番号2);EMAIL:(メールアドレス1);EMAIL:(メールアドレス2);NOTE:(メモ文字列) ;;" set aStr to current application’s NSString’s stringWithString:a set strData to aStr’s dataUsingEncoding:(current application’s NSShiftJISStringEncoding) –シフトJISにエンコード set qrFilter to current application’s CIFilter’s filterWithName:"CIQRCodeGenerator" qrFilter’s setValue:strData forKey:"inputMessage" qrFilter’s setValue:"H" forKey:"inputCorrectionLevel" set anImage to qrFilter’s outputImage() set convImg to convCIimageToNSImage(anImage) of me –NSImageを拡大(アンチエイリアス解除で) set resizedImg to my resizeNSImageWithoutAntlialias:convImg toScale:16.0 –デスクトップに保存 set aDesktopPath to (current application’s NSProcessInfo’s processInfo()’s environment()’s objectForKey:("HOME"))’s stringByAppendingString:"/Desktop/" set savePath to aDesktopPath’s stringByAppendingString:((current application’s NSUUID’s UUID()’s UUIDString())’s stringByAppendingString:".png") saveNSImageAtPathAsPNG(resizedImg, savePath) of me on convCIimageToNSImage(aCIImage) set aRep to current application’s NSBitmapImageRep’s alloc()’s initWithCIImage:aCIImage set tmpSize to aRep’s |size|() set newImg to current application’s NSImage’s alloc()’s initWithSize:tmpSize newImg’s addRepresentation:aRep return newImg end convCIimageToNSImage on convNSImageToCIimage(aNSImage) set tiffDat to aNSImage’s TIFFRepresentation() set aRep to current application’s NSBitmapImageRep’s imageRepWithData:tiffDat set newImg to current application’s CIImage’s alloc()’s initWithBitmapImageRep:aRep return newImg end convNSImageToCIimage –NSImageを指定パスにPNG形式で保存 on saveNSImageAtPathAsPNG(anImage, outPath) set imageRep to anImage’s TIFFRepresentation() set aRawimg to current application’s NSBitmapImageRep’s imageRepWithData:imageRep set pathString to current application’s NSString’s stringWithString:outPath set newPath to pathString’s stringByExpandingTildeInPath() set myNewImageData to (aRawimg’s representationUsingType:(current application’s NSPNGFileType) |properties|:(missing value)) set aRes to (myNewImageData’s writeToFile:newPath atomically:true) as boolean return aRes –成功ならtrue、失敗ならfalseが返る end saveNSImageAtPathAsPNG –NSImageを指定倍率で拡大(アンチエイリアス解除状態で)–By Shane Stanley on resizeNSImageWithoutAntlialias:aSourceImg toScale:imgScale set aSize to aSourceImg’s |size|() set aWidth to (aSize’s width) * imgScale set aHeight to (aSize’s height) * imgScale set aRep to current application’s NSBitmapImageRep’s alloc()’s initWithBitmapDataPlanes:(missing value) pixelsWide:aWidth pixelsHigh:aHeight bitsPerSample:8 samplesPerPixel:4 hasAlpha:true isPlanar:false colorSpaceName:(current application’s NSCalibratedRGBColorSpace) bytesPerRow:0 bitsPerPixel:0 set newSize to {width:aWidth, height:aHeight} aRep’s setSize:newSize current application’s NSGraphicsContext’s saveGraphicsState() set theContext to current application’s NSGraphicsContext’s graphicsContextWithBitmapImageRep:aRep current application’s NSGraphicsContext’s setCurrentContext:theContext theContext’s setShouldAntialias:false theContext’s setImageInterpolation:(current application’s NSImageInterpolationNone) aSourceImg’s drawInRect:(current application’s NSMakeRect(0, 0, aWidth, aHeight)) fromRect:(current application’s NSZeroRect) operation:(current application’s NSCompositeCopy) fraction:(1.0) current application’s NSGraphicsContext’s restoreGraphicsState() set newImg to current application’s NSImage’s alloc()’s initWithSize:newSize newImg’s addRepresentation:aRep return newImg end resizeNSImageWithoutAntlialias:toScale: |
Finder上で選択中の画像を横方向に連結


| AppleScript名:Finder上で選択中の画像を横方向に連結 |
| — Created 2017-11-21 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "QuartzCore" use framework "AppKit" use framework "MagicKit" –https://github.com/aidansteele/magickit property |NSURL| : a reference to current application’s |NSURL| property NSUUID : a reference to current application’s NSUUID property NSArray : a reference to current application’s NSArray property NSString : a reference to current application’s NSString property NSImage : a reference to current application’s NSImage property GEMagicKit : a reference to current application’s GEMagicKit property NSPNGFileType : a reference to current application’s NSPNGFileType property NSMutableArray : a reference to current application’s NSMutableArray property NSBitmapImageRep : a reference to current application’s NSBitmapImageRep property xGap : 10 –連結時の画像間のアキ(横方向) tell application "Finder" set aSel to selection as alias list if aSel = {} or aSel = "" then return end tell –選択した画像をArrayに入れる set imgList to NSMutableArray’s new() repeat with i in aSel set aPath to POSIX path of i –指定ファイルのUTIを取得して、画像(public.image)があれば処理を行う set aRes to (GEMagicKit’s magicForFileAtPath:aPath) set utiList to (aRes’s uniformTypeHierarchy()) as list if "public.image" is in utiList then set aNSImage to (NSImage’s alloc()’s initWithContentsOfFile:aPath) (imgList’s addObject:aNSImage) end if end repeat –KVCで画像の各種情報をまとめて取得 set sizeList to (imgList’s valueForKeyPath:"size") as list –NSSize to list of record conversion set maxHeight to ((NSArray’s arrayWithArray:sizeList)’s valueForKeyPath:"@max.height") as real set totalWidth to ((NSArray’s arrayWithArray:sizeList)’s valueForKeyPath:"@sum.width") as real set totalCount to ((NSArray’s arrayWithArray:sizeList)’s valueForKeyPath:"@count") as integer –出力画像作成 set tSize to current application’s NSMakeSize((totalWidth + (xGap * totalCount)), maxHeight) set newImage to NSImage’s alloc()’s initWithSize:tSize –順次画像を新規画像に上書き set xOrig to 0 repeat with i in (imgList as list) set j to contents of i set curSize to j’s |size|() set aRect to {xOrig, (maxHeight – (curSize’s height())), (curSize’s width()), (curSize’s height())} set newImage to composeImage(newImage, j, aRect) of me set xOrig to (curSize’s width()) + xGap end repeat –デスクトップにPNG形式でNSImageをファイル保存 set aDesktopPath to current application’s NSHomeDirectory()’s stringByAppendingString:"/Desktop/" set savePath to aDesktopPath’s stringByAppendingString:((NSUUID’s UUID()’s UUIDString())’s stringByAppendingString:".png") set fRes to saveNSImageAtPathAsPNG(newImage, savePath) of me –2つのNSImageを重ね合わせ合成してNSImageで返す on composeImage(backImage, composeImage, aTargerRect) set newImage to NSImage’s alloc()’s initWithSize:(backImage’s |size|()) copy aTargerRect to {x1, y1, x2, y2} set bRect to current application’s NSMakeRect(x1, y1, x2, y2) newImage’s lockFocus() set newImageRect to current application’s CGRectZero set newImageRect’s |size| to (newImage’s |size|) backImage’s drawInRect:newImageRect composeImage’s drawInRect:bRect newImage’s unlockFocus() return newImage end composeImage –NSImageを指定パスにPNG形式で保存 on saveNSImageAtPathAsPNG(anImage, outPath) set imageRep to anImage’s TIFFRepresentation() set aRawimg to NSBitmapImageRep’s imageRepWithData:imageRep set pathString to NSString’s stringWithString:outPath set newPath to pathString’s stringByExpandingTildeInPath() set myNewImageData to (aRawimg’s representationUsingType:(NSPNGFileType) |properties|:(missing value)) set aRes to (myNewImageData’s writeToFile:newPath atomically:true) as boolean return aRes –成功ならtrue、失敗ならfalseが返る end saveNSImageAtPathAsPNG |
Get the ISO 3166 country code from Time Zone
–> contryAndLocationKit.framework
| AppleScript名:Get the ISO 3166 country code from Time Zone |
| — Created 2016-04-10 by Takaaki Naganoya — 2016 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "contryAndLocationKit" –iospirit/NSTimeZone-ISCLLocation & BigZaphod/CLLocation-SunriseSunset –https://github.com/iospirit/NSTimeZone-ISCLLocation –https://github.com/BigZaphod/CLLocation-SunriseSunset set aTZ to (current application’s NSTimeZone’s localTimeZone())’s |description|() as string –>"Local Time Zone (Asia/Tokyo (JST) offset 32400)" set aCountry to ((current application’s NSTimeZone’s localTimeZone())’s ISO3166CountryCode()) as list of string or string –> "JP" –> missing value (error) |
1箇所から別の箇所の方位を求める
任意の緯度・経度情報から、別の箇所の緯度・経度情報の「方位」を計算するAppleScriptです。

北を0として、西に向かうとマイナス、東に向かうとプラスの値で角度(方位)を返します。この手の計算に必須のatan2関数がAppleScriptに標準装備されていないため、atan2の機能を提供するSatimage OSAXを実行環境にインストールしておく必要があります。
各種三角関数に関しては、直接計算する関数がなくても、あらかじめ計算しておいてテーブルから取得する程度でも精度的には問題のないケースが多いので、テーブルでデータを持っておくことも多々あります。
最近、Numbersで内蔵関数を増やすために利用している「cephes math library」あたりがCレベルではなくObjective-Cレベルから直接利用できるとよさそうなのですが、、、






| AppleScript名:1箇所から別の箇所の方位を求める |
| — Created 2017-04-27 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions –requires "Satimage osax" http://www.satimage.fr/software/en/downloads/downloads_companion_osaxen.html use framework "Foundation" use bPlus : script "BridgePlus" –https://www.macosxautomation.com/applescript/apps/BridgePlus.html –http://zurb.com/forrst/posts/Direction_between_2_CLLocations-uAo set coord1 to {latitude:35.73677496, longitude:139.63754457} –中村橋駅 set coord2 to {latitude:35.78839012, longitude:139.61241447} –和光市駅 set dirRes to calcDirectionBetweenTwoPlaces(coord1, coord2) of me –> -25.925429877542 (*) set coord2 to {latitude:35.78839012, longitude:139.61241447} –和光市駅 set dirRes to calcDirectionBetweenTwoPlaces(coord1, coord2) of me –> -25.925429877542 set coord2 to {latitude:35.7227821, longitude:139.63860897} –鷺宮駅 set coord2 to {latitude:35.73590542, longitude:139.62986745} –富士見台駅 set coord2 to {latitude:35.73785024, longitude:139.65339321} –練馬駅 set coord2 to {latitude:35.71026838, longitude:139.81215754} –東京スカイツリー on calcDirectionBetweenTwoPlaces(coord1, coord2) load framework set deltaLong to (longitude of coord2) – (longitude of coord1) set yComponent to bPlus’s sinValueOf:deltaLong set xComponent to (bPlus’s cosValueOf:(latitude of coord1)) * (bPlus’s sinValueOf:(latitude of coord2)) – (bPlus’s sinValueOf:(latitude of coord1)) * (bPlus’s cosValueOf:(latitude of coord2)) * (bPlus’s cosValueOf:deltaLong) set vList to {yComponent, xComponent} set radians to atan2 vList —Requires SatImage OSAX set degreeRes to (radToDeg(radians) of me) return degreeRes end calcDirectionBetweenTwoPlaces on radToDeg(aRadian) return aRadian * (180 / pi) end radToDeg |
都道府県リストから隣接都道府県を含む該当のコードを抽出する
膨大な位置情報の距離を計算するような場合に、対象の都道府県と隣接する都道府県に限定して距離計算を行うことで計算量を削減するためのAppleScriptです。
ある1点の位置情報に対して大量の地点との距離計算を行う処理は割とよくあります。自宅からの最寄駅の計算や、日本全国に点在している施設の最寄駅の計算などなどです。
たいていは、総当たりですべての計算対象との距離計算を行い、近い順でソートすることになります。自分の場合では日本国内の700か所ぐらいの(「戦場の絆」が設置されている)ゲームセンターの位置と日本全国の鉄道の駅(8,000か所程度)を比較して最寄駅を計算しました。
700 x 8,000 = 5,600,000回の距離計算を行うわけで、あまりに時間がかかるのでめまいがします(90分ぐらいかかりました)。1か所あたりの距離計算の所用時間は0.086秒程度なのでいちがいに遅いとはいえません(CocoaのCoreLocationの機能を使わないで、地球を球としてみなして近似的な距離計算を行えば速くなることでしょう)。
ここで、脳みそが頭に入っている人間であれば考えるわけです。もっと速く計算できる方法があるんじゃないか? と。
だいたい、北海道にある地点の最寄駅を求めるのに九州の最果てにある駅との距離計算を行う必要はありません。8,000箇所のすべての駅との距離計算を行う中には、膨大な「無意味な計算」が含まれています。この、「無意味な計算」を除外してあげれば、計算量が半分で済む場合には計算時間は半分になります。
そうやって考えていくと、都道府県レベルで隣接している地点については計算を行う必要がありますが、隣接していない都道府県にある駅との計算は行う意味がないことに気づきます。
そして実際に本テーブルを作成して計算に導入したところ、当初の90分が10分に短縮されました。さらに、データの持ち方を変えて最終的には5分ぐらいまで短縮できました。
「都道府県レベルで計算対象の除外ができて、高速処理を実現できたんだから、市区町村レベルの隣接情報を作って計算すれば、さらに高速化できるのでは?」
と考える人がいるかもしれません。ただ、その処理を実際に頭の中でシミュレーションしてみると、市区町村レベルでは「駅」が存在していないものもあったりで、日本国内に3,0000程度あるとみられるそのレベルの地方公共団体の隣接データの作成は大変です。たしかに高速化できるかもしれませんが、その成果は微々たるものになることでしょう(東京、名古屋、大阪などの大都市にかぎっては効果があるかも? その他の地域ではさほど効果はありません)。
対象は日本国内の都道府県。海外の住所情報でも同様に都道府県にコードを振って隣接情報をデータ化すれば同様の処理は可能です。
# BridgePlus Script Libraryを併用しなくてもすむ v2を追加掲載しておきました
| AppleScript名:都道府県リストから隣接都道府県を含む該当のコードを抽出する |
| — Created 2015-12-06 by Takaaki Naganoya — 2015 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use BridgePlus : script "BridgePlus" –https://www.macosxautomation.com/applescript/apps/BridgePlus.html property prefList : {{prefCode:1, prefName:"北海道", neighbors:{}}, {prefCode:2, prefName:"青森県", neighbors:{3, 5}}, {prefCode:3, prefName:"岩手県", neighbors:{2, 4, 5}}, {prefCode:4, prefName:"宮城県", neighbors:{3, 5, 6, 7}}, {prefCode:5, prefName:"秋田県", neighbors:{2, 3, 4, 6}}, {prefCode:6, prefName:"山形県", neighbors:{3, 4, 5, 7, 15}}, {prefCode:7, prefName:"福島県", neighbors:{4, 6, 8, 9, 10, 15}}, {prefCode:8, prefName:"茨城県", neighbors:{7, 9, 10, 11, 12}}, {prefCode:9, prefName:"栃木県", neighbors:{7, 8, 10, 11, 12}}, {prefCode:10, prefName:"群馬県", neighbors:{7, 9, 11, 15, 20}}, {prefCode:11, prefName:"埼玉県", neighbors:{8, 9, 10, 12, 13, 19, 20}}, {prefCode:12, prefName:"千葉県", neighbors:{8, 11, 13}}, {prefCode:13, prefName:"東京都", neighbors:{11, 12, 19, 14}}, {prefCode:14, prefName:"神奈川県", neighbors:{13, 19, 22}}, {prefCode:15, prefName:"新潟県", neighbors:{6, 7, 10, 16, 20}}, {prefCode:16, prefName:"富山県", neighbors:{15, 17, 20, 21}}, {prefCode:17, prefName:"石川県", neighbors:{16, 18, 21}}, {prefCode:18, prefName:"福井県", neighbors:{17, 21, 25, 26}}, {prefCode:19, prefName:"山梨県", neighbors:{11, 13, 14, 20, 22}}, {prefCode:20, prefName:"長野県", neighbors:{10, 11, 15, 16, 19, 21, 22, 23}}, {prefCode:21, prefName:"岐阜県", neighbors:{16, 17, 18, 20, 23, 24, 25}}, {prefCode:22, prefName:"静岡県", neighbors:{14, 19, 20, 23}}, {prefCode:23, prefName:"愛知県", neighbors:{20, 21, 22, 24}}, {prefCode:24, prefName:"三重県", neighbors:{21, 23, 25, 26, 29}}, {prefCode:25, prefName:"滋賀県", neighbors:{18, 21, 24, 26}}, {prefCode:26, prefName:"京都府", neighbors:{18, 24, 25, 27, 28, 29}}, {prefCode:27, prefName:"大阪府", neighbors:{26, 29, 28, 30}}, {prefCode:28, prefName:"兵庫県", neighbors:{26, 27, 31, 33}}, {prefCode:29, prefName:"奈良県", neighbors:{24, 25, 26, 27, 30}}, {prefCode:30, prefName:"和歌山県", neighbors:{24, 27, 29}}, {prefCode:31, prefName:"鳥取県", neighbors:{28, 33, 32, 34}}, {prefCode:32, prefName:"島根県", neighbors:{31, 34, 35}}, {prefCode:33, prefName:"岡山県", neighbors:{28, 31, 34}}, {prefCode:34, prefName:"広島県", neighbors:{33, 31, 32, 35}}, {prefCode:35, prefName:"山口県", neighbors:{32, 34}}, {prefCode:36, prefName:"徳島県", neighbors:{37, 39}}, {prefCode:37, prefName:"香川県", neighbors:{36, 38, 39}}, {prefCode:38, prefName:"愛媛県", neighbors:{37, 39}}, {prefCode:39, prefName:"高知県", neighbors:{36, 37, 38}}, {prefCode:40, prefName:"福岡県", neighbors:{44, 43, 41}}, {prefCode:41, prefName:"佐賀県", neighbors:{40, 42}}, {prefCode:42, prefName:"長崎県", neighbors:{41}}, {prefCode:43, prefName:"熊本県", neighbors:{40, 42, 44, 45, 46}}, {prefCode:44, prefName:"大分県", neighbors:{40, 43, 45}}, {prefCode:45, prefName:"宮崎県", neighbors:{43, 44, 46}}, {prefCode:46, prefName:"鹿児島県", neighbors:{43, 45}}, {prefCode:47, prefName:"沖縄県", neighbors:{}}} set aPref to 13 –Tokyo set aRes to my filterRecListByLabel2(prefList, "prefCode == [c]%@", {aPref}) set targList to neighbors of aRes & aPref –> {11, 12, 19, 14, 13}–Saitama, Chiba, yamanashi, Kanagawa, Tokyo –リストに入れたレコードを、指定の属性ラベルの値で抽出(predicateとパラメータを分離)し、1つのアイテムだけを返す on filterRecListByLabel2(aRecList as list, aPredicate as string, aParam) set aArray to current application’s NSArray’s arrayWithArray:aRecList set aPredicate to current application’s NSPredicate’s predicateWithFormat:aPredicate argumentArray:aParam set filteredArray to aArray’s filteredArrayUsingPredicate:aPredicate set bList to ASify from filteredArray as list set cList to first item of bList return cList end filterRecListByLabel2 |
| AppleScript名:都道府県リストから隣接都道府県を含む該当のコードを抽出する v2.scpt |
| — Created 2015-12-06 by Takaaki Naganoya — 2015 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" —BridgePlusを使わなくていいように書き換え property prefList : {{prefCode:1, prefName:"北海道", neighbors:{}}, {prefCode:2, prefName:"青森県", neighbors:{3, 5}}, {prefCode:3, prefName:"岩手県", neighbors:{2, 4, 5}}, {prefCode:4, prefName:"宮城県", neighbors:{3, 5, 6, 7}}, {prefCode:5, prefName:"秋田県", neighbors:{2, 3, 4, 6}}, {prefCode:6, prefName:"山形県", neighbors:{3, 4, 5, 7, 15}}, {prefCode:7, prefName:"福島県", neighbors:{4, 6, 8, 9, 10, 15}}, {prefCode:8, prefName:"茨城県", neighbors:{7, 9, 10, 11, 12}}, {prefCode:9, prefName:"栃木県", neighbors:{7, 8, 10, 11, 12}}, {prefCode:10, prefName:"群馬県", neighbors:{7, 9, 11, 15, 20}}, {prefCode:11, prefName:"埼玉県", neighbors:{8, 9, 10, 12, 13, 19, 20}}, {prefCode:12, prefName:"千葉県", neighbors:{8, 11, 13}}, {prefCode:13, prefName:"東京都", neighbors:{11, 12, 19, 14}}, {prefCode:14, prefName:"神奈川県", neighbors:{13, 19, 22}}, {prefCode:15, prefName:"新潟県", neighbors:{6, 7, 10, 16, 20}}, {prefCode:16, prefName:"富山県", neighbors:{15, 17, 20, 21}}, {prefCode:17, prefName:"石川県", neighbors:{16, 18, 21}}, {prefCode:18, prefName:"福井県", neighbors:{17, 21, 25, 26}}, {prefCode:19, prefName:"山梨県", neighbors:{11, 13, 14, 20, 22}}, {prefCode:20, prefName:"長野県", neighbors:{10, 11, 15, 16, 19, 21, 22, 23}}, {prefCode:21, prefName:"岐阜県", neighbors:{16, 17, 18, 20, 23, 24, 25}}, {prefCode:22, prefName:"静岡県", neighbors:{14, 19, 20, 23}}, {prefCode:23, prefName:"愛知県", neighbors:{20, 21, 22, 24}}, {prefCode:24, prefName:"三重県", neighbors:{21, 23, 25, 26, 29}}, {prefCode:25, prefName:"滋賀県", neighbors:{18, 21, 24, 26}}, {prefCode:26, prefName:"京都府", neighbors:{18, 24, 25, 27, 28, 29}}, {prefCode:27, prefName:"大阪府", neighbors:{26, 29, 28, 30}}, {prefCode:28, prefName:"兵庫県", neighbors:{26, 27, 31, 33}}, {prefCode:29, prefName:"奈良県", neighbors:{24, 25, 26, 27, 30}}, {prefCode:30, prefName:"和歌山県", neighbors:{24, 27, 29}}, {prefCode:31, prefName:"鳥取県", neighbors:{28, 33, 32, 34}}, {prefCode:32, prefName:"島根県", neighbors:{31, 34, 35}}, {prefCode:33, prefName:"岡山県", neighbors:{28, 31, 34}}, {prefCode:34, prefName:"広島県", neighbors:{33, 31, 32, 35}}, {prefCode:35, prefName:"山口県", neighbors:{32, 34}}, {prefCode:36, prefName:"徳島県", neighbors:{37, 39}}, {prefCode:37, prefName:"香川県", neighbors:{36, 38, 39}}, {prefCode:38, prefName:"愛媛県", neighbors:{37, 39}}, {prefCode:39, prefName:"高知県", neighbors:{36, 37, 38}}, {prefCode:40, prefName:"福岡県", neighbors:{44, 43, 41}}, {prefCode:41, prefName:"佐賀県", neighbors:{40, 42}}, {prefCode:42, prefName:"長崎県", neighbors:{41}}, {prefCode:43, prefName:"熊本県", neighbors:{40, 42, 44, 45, 46}}, {prefCode:44, prefName:"大分県", neighbors:{40, 43, 45}}, {prefCode:45, prefName:"宮崎県", neighbors:{43, 44, 46}}, {prefCode:46, prefName:"鹿児島県", neighbors:{43, 45}}, {prefCode:47, prefName:"沖縄県", neighbors:{}}} set aPref to 13 –Tokyo set aRes to my filterRecListByLabel2(prefList, "prefCode == [c]%@", {aPref}) set targList to neighbors of aRes & aPref –> {11, 12, 19, 14, 13}–Saitama, Chiba, yamanashi, Kanagawa, Tokyo –リストに入れたレコードを、指定の属性ラベルの値で抽出(predicateとパラメータを分離)し、1つのアイテムだけを返す on filterRecListByLabel2(aRecList as list, aPredicate as string, aParam) set aArray to current application’s NSArray’s arrayWithArray:aRecList set aPredicate to current application’s NSPredicate’s predicateWithFormat:aPredicate argumentArray:aParam set filteredArray to aArray’s filteredArrayUsingPredicate:aPredicate set bList to filteredArray as list set cList to first item of bList return cList end filterRecListByLabel2 |
2点間の距離を求める v3
Core Locationを用いた地球上の2点間の距離計算のAppleScriptです。
地球は楕円体であるため、その楕円率を考慮して距離計算する必要があります。Core Locationには楕円率を考慮した計算が行われていることを期待しています。
完全球体として2点間の距離を計算すると近似値になってしまうものの、(東京近郊の2点の計算時)実際の計算結果を比較すると、4.2kmの距離計算時に1メートル程度の誤差としてしか表れてこないため、計算精度をさほど求められない用途においては、地球を完全球体として扱った近似計算でも十分に有用なケースがあります。
東京-鹿児島間で距離計算したときに、
A. 楕円率を考慮して計算:960392.341424メートル
B. 完全球体として近似計算:960581.2302612メートル
誤差(A.- B.):189メートル
ぐらいの誤差です。日本国内の1点からの最寄りの施設を計算するぐらいなら完全球体として計算しても大丈夫でしょう。処理時間とか計算対象の座標数を考慮し、用途に応じて選択するとよいでしょう。
追記:
気になったので、CoreLocationで距離を求めるタイプ(First)と、完全球体で近似計算を行うタイプ(Second)のルーチンで1万回ループで実行して、実行時間を求めてみました。
MacBookPro10,1, macOS Version 10.14.6 (Build 18G6006), 10000 iterations
First Run Total Time Average
First 0.005 11.614 0.001
Second 0.001 2.958 0.000
Ratio (excluding first run): 3.93:1
結果は、予想どおりCoreLocationで行うルーチン(本Script)の方が約4倍低速でした。ただし、実際の処理に組み込んでこの通りにならないケースもあるので、実際の処理で計測してどちらを用いるべきかを判断することになるでしょう。

| AppleScript名:2点間の距離を求める v3 |
| — Created 2015-03-03 by Shane Stanley use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "CoreLocation" set aPlace to current application’s CLLocation’s alloc()’s initWithLatitude:35.737072 longitude:139.637826 set bPlace to current application’s CLLocation’s alloc()’s initWithLatitude:35.753108 longitude:139.595123 set distanceInMetres to aPlace’s distanceFromLocation:bPlace –> 4252.71123319014 |
(GET)Yahoo! 住所ジオコーダAPIを呼び出す
Yahoo!の住所ジオコーダAPIを呼び出して、住所情報から緯度、経度を取得するAppleScriptです。
実行のためには、Yahoo!に利用登録を行い、API Keyを取得してretAccessKey()ハンドラ内に書いておいてください。これは、サンプル用にわかりやすさを優先したためで、実際にはKeychainにAPI Keyを入れておいて問い合わせ、Script中にAPI Keyを直接書かないのが望ましいところです。
GoogleやYahoo!のWeb APIベース、AppleのOS内蔵住所ジオコーダなどさまざまですが、個人的にはYahoo!の住所ジオコーダをよく使っています。ただし、指定の住所がかならずしも一発で緯度・経度情報に変換できるわけでもないので、エラー時には後ろから1文字ずつ削除して再試行しています。
| AppleScript名:(GET)Yahoo! 住所ジオコーダAPIを呼び出す |
| — Created 2016-11-25 by Takaaki Naganoya — 2016 Piyomaru Software use AppleScript version "2.5" use scripting additions use framework "Foundation" –http://developer.yahoo.co.jp/webapi/map/openlocalplatform/v1/geocoder.html set anAddress to "東京都墨田区押上1-1−2" set aResList to addressGeoCoderByYahoo(anAddress) of me –> {"35.66042757", "139.72918139"} on addressGeoCoderByYahoo(addrStr) set reqURLStr to "http://geo.search.olp.yahooapis.jp/OpenLocalPlatform/V1/geoCoder" set aKey to retAccessKey() of me set aRec to {query:addrStr, output:"json", appid:aKey, datum:"tky"} –日本測地系 set aURL to retURLwithParams(reqURLStr, aRec) of me set aRes to callRestGETAPIAndParseResults(aURL) of me set aRESCode to responseCode of aRes if aRESCode is not equal to 200 then return false set aRESHeader to responseHeader of aRes set aJSONres to (json of aRes) set anAdd1 to (aJSONres’s valueForKeyPath:"Feature")’s firstObject() set aGPSstr to (anAdd1’s valueForKeyPath:"Geometry.Coordinates") set {aLon, aLat} to separateStrByAMark(aGPSstr, ",") of me return {aLat, aLon} end addressGeoCoderByYahoo –GET methodのREST APIを呼ぶ on callRestGETAPIAndParseResults(aURL) set aRequest to current application’s NSMutableURLRequest’s requestWithURL:(current application’s |NSURL|’s URLWithString:aURL) aRequest’s setHTTPMethod:"GET" aRequest’s setCachePolicy:(current application’s NSURLRequestReloadIgnoringLocalCacheData) aRequest’s setHTTPShouldHandleCookies:false aRequest’s setTimeoutInterval:60 aRequest’s setValue:"application/json" forHTTPHeaderField:"Accept" –CALL REST API set aRes to current application’s NSURLConnection’s sendSynchronousRequest:aRequest returningResponse:(reference) |error|:(missing value) –Parse Results set resList to aRes as list set bRes to contents of (first item of resList) set resStr to current application’s NSString’s alloc()’s initWithData:bRes encoding:(current application’s NSUTF8StringEncoding) set jsonString to current application’s NSString’s stringWithString:resStr set jsonData to jsonString’s dataUsingEncoding:(current application’s NSUTF8StringEncoding) set aJsonDict to current application’s NSJSONSerialization’s JSONObjectWithData:jsonData options:0 |error|:(missing value) –Get Response Code & Header set dRes to contents of second item of resList if dRes is not equal to missing value then set resCode to (dRes’s statusCode()) as number set resHeaders to (dRes’s allHeaderFields()) as record else set resCode to 0 set resHeaders to {} end if return {json:aJsonDict, responseCode:resCode, responseHeader:resHeaders} end callRestGETAPIAndParseResults on retURLwithParams(aBaseURL, aRec) set aDic to current application’s NSMutableDictionary’s dictionaryWithDictionary:aRec set aKeyList to (aDic’s allKeys()) as list set aValList to (aDic’s allValues()) as list set aLen to length of aKeyList set qList to {} repeat with i from 1 to aLen set aName to contents of item i of aKeyList set aVal to contents of item i of aValList set the end of qList to (current application’s NSURLQueryItem’s queryItemWithName:aName value:aVal) end repeat set aComp to current application’s NSURLComponents’s alloc()’s initWithString:aBaseURL aComp’s setQueryItems:qList set aURL to (aComp’s |URL|()’s absoluteString()) as text return aURL end retURLwithParams on retAccessKey() return "xxXxxxXxXXxxxXXXXXXXXXXXXXXxXXXxxxXXxXXxxXXxxxXXXxxXXXX-" –Yahoo! API Key end retAccessKey on urlencodeStr(aStr) set aString to current application’s NSString’s stringWithString:aStr set aString to (aString’s stringByAddingPercentEncodingWithAllowedCharacters:(current application’s NSCharacterSet’s URLQueryAllowedCharacterSet())) as text return aString end urlencodeStr –リストに入れたレコードを、指定の属性ラベルの値で抽出 on filterRecListByLabel1(aRecList as list, aPredicate as string) set aArray to current application’s NSArray’s arrayWithArray:aRecList set aPredicate to current application’s NSPredicate’s predicateWithFormat:aPredicate set filteredArray to aArray’s filteredArrayUsingPredicate:aPredicate set bList to filteredArray as list return bList end filterRecListByLabel1 –テキストを指定記号を元に分割する on separateStrByAMark(aStr as string, aMark as string) set aMarkLen to length of aMark set aOffset to offset of "," in aStr set aPart to text 1 thru (aOffset – 1) of aStr set bPart to text (aOffset + aMarkLen) thru -1 of aStr return {aPart, bPart} end separateStrByAMark |
(GET)Yahoo! 逆住所ジオコーダAPIを呼び出す
Yahoo!の逆住所ジオコーダAPIを呼び出して、緯度、経度から住所情報を取得するAppleScriptです。
実行のためには、Yahoo!に利用登録を行い、API Keyを取得してretAccessKey()ハンドラ内に書いておいてください。これは、サンプル用にわかりやすさを優先したためで、実際にはKeychainにAPI Keyを入れておいて問い合わせ、Script中にAPI Keyを直接書かないのが望ましいところです。
| AppleScript名:(GET)Yahoo! 逆住所ジオコーダAPIを呼び出す |
| — Created 2016-11-20 by Takaaki Naganoya — 2016 Piyomaru Software use AppleScript version "2.5" use scripting additions use framework "Foundation" –http://developer.yahoo.co.jp/webapi/map/openlocalplatform/v1/reversegeocoder.html set aLat to 35.74 as string set aLon to 139.6 as string set aResList to reverseGeoCoderByYahoo(aLat, aLon) of me –> {"東京都", "練馬区"} on reverseGeoCoderByYahoo(aLat, aLon) set reqURLStr to "http://reverse.search.olp.yahooapis.jp/OpenLocalPlatform/V1/reverseGeoCoder" set aKey to retAccessKey() of me set aRec to {lat:aLat, lon:aLon, output:"json", appid:aKey, datum:"tky"} set aURL to retURLwithParams(reqURLStr, aRec) of me set aRes to callRestGETAPIAndParseResults(aURL) of me set aRESCode to responseCode of aRes if aRESCode is not equal to 200 then return false set aRESHeader to responseHeader of aRes set aJSONres to (json of aRes) set anAddress to (aJSONres’s valueForKeyPath:"Feature.Property")’s firstObject() set anElement to anAddress’s valueForKeyPath:"AddressElement" set aPrefecture to first item of (my filterRecListByLabel1(anElement, "Level == ’prefecture’")) –> {Kana:"とうきょうと", Name:"東京都", Level:"prefecture", Code:"13"} set aCity to first item of (my filterRecListByLabel1(anElement, "Level == ’city’")) –> {Kana:"ねりまく", Name:"練馬区", Level:"city", Code:"13120"} set aPref to |Name| of aPrefecture set aCt to |Name| of aCity return {aPref, aCt} end reverseGeoCoderByYahoo –GET methodのREST APIを呼ぶ on callRestGETAPIAndParseResults(aURL) set aRequest to current application’s NSMutableURLRequest’s requestWithURL:(current application’s |NSURL|’s URLWithString:aURL) aRequest’s setHTTPMethod:"GET" aRequest’s setCachePolicy:(current application’s NSURLRequestReloadIgnoringLocalCacheData) aRequest’s setHTTPShouldHandleCookies:false aRequest’s setTimeoutInterval:60 aRequest’s setValue:"application/json" forHTTPHeaderField:"Accept" –CALL REST API set aRes to current application’s NSURLConnection’s sendSynchronousRequest:aRequest returningResponse:(reference) |error|:(missing value) –Parse Results set resList to aRes as list set bRes to contents of (first item of resList) set resStr to current application’s NSString’s alloc()’s initWithData:bRes encoding:(current application’s NSUTF8StringEncoding) set jsonString to current application’s NSString’s stringWithString:resStr set jsonData to jsonString’s dataUsingEncoding:(current application’s NSUTF8StringEncoding) set aJsonDict to current application’s NSJSONSerialization’s JSONObjectWithData:jsonData options:0 |error|:(missing value) –Get Response Code & Header set dRes to contents of second item of resList if dRes is not equal to missing value then set resCode to (dRes’s statusCode()) as number set resHeaders to (dRes’s allHeaderFields()) as record else set resCode to 0 set resHeaders to {} end if return {json:aJsonDict, responseCode:resCode, responseHeader:resHeaders} end callRestGETAPIAndParseResults on retURLwithParams(aBaseURL, aRec) set aDic to current application’s NSMutableDictionary’s dictionaryWithDictionary:aRec set aKeyList to (aDic’s allKeys()) as list set aValList to (aDic’s allValues()) as list set aLen to length of aKeyList set qList to {} repeat with i from 1 to aLen set aName to contents of item i of aKeyList set aVal to contents of item i of aValList set the end of qList to (current application’s NSURLQueryItem’s queryItemWithName:aName value:aVal) end repeat set aComp to current application’s NSURLComponents’s alloc()’s initWithString:aBaseURL aComp’s setQueryItems:qList set aURL to (aComp’s |URL|()’s absoluteString()) as text return aURL end retURLwithParams on retAccessKey() return "xxXxxxXxXXxxxXXXXXXXXXXXXXXxXXXxxxXXxXXxxXXxxxXXXxxXXXX-" –Yahoo! API Key end retAccessKey on urlencodeStr(aStr) set aString to current application’s NSString’s stringWithString:aStr set aString to (aString’s stringByAddingPercentEncodingWithAllowedCharacters:(current application’s NSCharacterSet’s URLQueryAllowedCharacterSet())) as text return aString end urlencodeStr –リストに入れたレコードを、指定の属性ラベルの値で抽出 on filterRecListByLabel1(aRecList as list, aPredicate as string) set aArray to current application’s NSArray’s arrayWithArray:aRecList set aPredicate to current application’s NSPredicate’s predicateWithFormat:aPredicate set filteredArray to aArray’s filteredArrayUsingPredicate:aPredicate set bList to filteredArray as list return bList end filterRecListByLabel1 |
Notification
macOS 10.10より、標準のScripting環境(スクリプトエディタ)上でCocoa-bridgeが使えるようになったので、Cocoa系のevent(Notification)が使えるようになったことを利用したサンプルです。
AppleScriptは基本的に、アプリケーションの機能や設定値を呼び出すためのもので、ごく一部の限られたアプリケーション以外からは「アプリケーション側からAppleScript(の所定のハンドラ)を呼び出す」方向のイベント呼び出しは利用できませんでした。
しかし、Cocoaのnotificationが利用できるようになったことで、多彩なイベント受信が行えるようになりました。
iTunes(or Music.app)の再生曲変更のnotificationを受信してやたらとTwitterなどに流そうとする入門Scripterがいるのですが(どこかの伝統芸か何かなんだろうか?)、そこはそんなに楽しいとも思えないし、本来のパワーを発揮していないとは思うのですが、見かけたらそっと触れないようにしておくことにしています。
| AppleScript名:Notification |
| — Created 2017-10-13 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "AppKit" property NSDistributedNotificationCenter : a reference to current application’s NSDistributedNotificationCenter on run NSDistributedNotificationCenter’s defaultCenter()’s addObserver:me selector:"statusChanged:" |name|:"com.apple.iTunes.playerInfo" object:(missing value) end run on statusChanged:sender tell application id "com.apple.iTunes" try tell current track set anAlbum to album set aName to name set anArtist to album artist set aRating to rating end tell on error return end try end tell display notification aName end statusChanged: |
指定Bundle IDのプロセス存在確認(ASOC)v3
| AppleScript名:指定Bundle IDのプロセス存在確認(ASOC)v3 |
| — Created 2015-02-08 by Takaaki Naganoya — Created 2017-02-26 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "AppKit" set aRes to chkAppProcesByBundleID("com.apple.iWork.Keynote") –> true on chkAppProcesByBundleID(aBundleID as string) set procArray to current application’s NSWorkspace’s sharedWorkspace’s runningApplications()’s valueForKeyPath:"bundleIdentifier" return (procArray’s containsObject:aBundleID) as boolean end chkAppProcesByBundleID |







