AppleScript名:shellの出力結果をスペースでparseする |
— Created 2016-03-11 by Takaaki Naganoya — 2016 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set a to "3 0x4248387 1920×1200 0 0 -1920 -1200 0 [main]" set aStr to (current application’s NSString’s stringWithString:a) –> (NSString) "3 0x4248387 1920×1200 0 0 -1920 -1200 0 [main]" set aLine to (aStr’s componentsSeparatedByString:" ") –> (NSArray) {"3", "", "0x4248387", "", "", "", "", "1920×1200", "", "", "", "", "", "0", "", "", "", "", "0", "", "-1920", "", "-1200", "", "", "", "", "", "0", "", "", "", "[main]"} ( aLine’s removeObject:"")–> (NSArray) {"3", "0x4248387", "1920×1200", "0", "0", "-1920", "-1200", "0", "[main]"} set bList to aLine as list –> {"3", "0x4248387", "1920×1200", "0", "0", "-1920", "-1200", "0", "[main]"} |
タグ: 10.12savvy
&と=で区切られたテキストをrecordに(NSScanner 2)
AppleScript名:&と=で区切られたテキストをrecordに(NSScanner 2) |
— Created 2016-12-12 by Shane Stanley use AppleScript version "2.4" use scripting additions use framework "Foundation" set aParamStr to "access_token=XXxxx(XXxXXXXXxxXxxXXx))&expires=86399" set aDict to (parseStrByAmpAndEqual(aParamStr) of me) as record –> {expires:"86399", access_token:"XXxxx(XXxXXXXXxxXxxXXx))"} on parseStrByAmpAndEqual(aParamStr) set theScanner to current application’s NSScanner’s scannerWithString:aParamStr log (theScanner’s scanLocation()) set aDict to current application’s NSMutableDictionary’s |dictionary|() repeat — terminate check, return the result (aDict) to caller set {theResult, theKey} to theScanner’s scanUpToString:"=" intoString:(reference) if theResult as boolean is false then return aDict log (theScanner’s scanLocation()) — skip over separator theScanner’s scanString:"=" intoString:(missing value) set {theResult, theValue} to theScanner’s scanUpToString:"&" intoString:(reference) log (theScanner’s scanLocation()) — skip over separator theScanner’s scanString:"&" intoString:(missing value) aDict’s setObject:theValue forKey:theKey log (theScanner’s scanLocation()) end repeat end parseStrByAmpAndEqual |
&と=で区切られたテキストをrecordに 改
AppleScript名:&と=で区切られたテキストをrecordに 改 |
— Created 2016-12-12 by Shane Stanley — Modified 2016-12-14 by edama2 use AppleScript version "2.4" use scripting additions use framework "Foundation" –http://piyocast.com/as/archives/4358 set aParamStr to "access_token=XXxxx(XXxXXXXXxxXxxXXx))&expires=86399&name=" set aDict to (parseStrByAmpAndEqual(aParamStr) of me) –> {expires:"86399", |name|:"", access_token:"XXxxx(XXxXXXXXxxXxxXXx))"} on parseStrByAmpAndEqual(aParamStr) set theScanner to current application’s NSScanner’s scannerWithString:aParamStr set aDict to current application’s NSMutableDictionary’s |dictionary|() repeat until (theScanner’s isAtEnd as boolean) — terminate check, return the result (aDict) to caller set {theResult, theKey} to theScanner’s scanUpToString:"=" intoString:(reference) — skip over separator theScanner’s scanString:"=" intoString:(missing value) set {theResult, theValue} to theScanner’s scanUpToString:"&" intoString:(reference) if theValue is missing value then set theValue to "" –>追加 — skip over separator theScanner’s scanString:"&" intoString:(missing value) aDict’s setObject:theValue forKey:theKey end repeat return aDict as record end parseStrByAmpAndEqual |
テキストの一部を取り出すじっけんASOC
AppleScript名:テキストの一部を取り出すじっけんASOC |
— Created 2017-10-12 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set aStr to current application’s NSString’s stringWithString:"0123456789" set aRes to (aStr’s substringToIndex:1) as string –> "0" set bRes to (aStr’s substringFromIndex:4) as string –> "456789" set cRes to (aStr’s substringWithRange:(current application’s NSMakeRange(3, 6))) as string –> "345678" |
文字列から開始文字列と終了文字列に囲まれた内容を削除
AppleScript名:文字列から開始文字列と終了文字列に囲まれた内容を削除 |
— Created 2016-12-12 by Shane Stanley — Modified 2016-12-14 by edama2 use AppleScript version "2.4" use scripting additions use framework "Foundation" –http://piyocast.com/as/archives/4358 set aStr to "thru (throughも可)" set aRes to (trimStrFromTo(aStr, "(", ")") of me) –> "thru " set bStr to "thru " set bRes to (trimStrFromTo(bStr, "(", ")") of me) –> "thru " on trimStrFromTo(aParamStr, fromStr, toStr) set theScanner to current application’s NSScanner’s scannerWithString:aParamStr set anArray to current application’s NSMutableArray’s array() repeat until (theScanner’s isAtEnd as boolean) — terminate check, return the result (aDict) to caller set {theResult, theKey} to theScanner’s scanUpToString:fromStr intoString:(reference) — skip over separator theScanner’s scanString:fromStr intoString:(missing value) set {theResult, theValue} to theScanner’s scanUpToString:toStr intoString:(reference) if theValue is missing value then set theValue to "" –>追加 — skip over separator theScanner’s scanString:toStr 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, fromStr & i & toStr, "") of me end repeat return curStr end trimStrFromTo –文字置換 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 |
テキストを指定文字でparseする
AppleScript名:テキストを指定文字でparseする |
— Created 2018-02-03 20:21:16 +0900 by Takaaki Naganoya — 2018 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set a to current application’s NSString’s stringWithString:"4;1;2008-03-09 18:57:48;2008-03-09 09:57:48;" set b to a’s componentsSeparatedByString:";" –> (NSArray) {"4", "1", "2008-03-09 18:57:48", "2008-03-09 09:57:48", ""} |
与えられたテキストからフライト情報を抽出 v2
AppleScript名:与えられたテキストからフライト情報を抽出 v2 |
— Created 2015-08-21 by Shane Stanley — Modified 2015-08-21 by Takaaki Naganoya — Modified 2015-08-22 by Shane Stanley use AppleScript version "2.4" use scripting additions use framework "Foundation" set theString to "UA460 SFO to YVR [Flight] 6/12/2013 United Airlines(UA) #460 dep SFO 7:57pm PDT arr YVR 10:14pm PDT; Ticket #0162360127882, Ticket #0162360127883; conf #K5XBXY; Note:, Seats:—/30A , Seats:—/30B " set theDates to (extractTransitInfoFromNaturalText(theString)) –> {{Flight:"460"}} on extractTransitInfoFromNaturalText(aString) set anNSString to current application’s NSString’s stringWithString:aString set {theDetector, theError} to current application’s NSDataDetector’s dataDetectorWithTypes:(current application’s NSTextCheckingTypeTransitInformation) |error|:(reference) set theMatches to theDetector’s matchesInString:anNSString options:0 range:{0, anNSString’s |length|()} set theResults to theMatches’s valueForKey:"components" return theResults as list end extractTransitInfoFromNaturalText |
与えられたテキストからdate objectを抽出 v2
AppleScript名:与えられたテキストからdate objectを抽出 v2 |
— Created 2015-08-21 by Shane Stanley — Modified 2015-08-22 by Shane Stanley use AppleScript version "2.4" use scripting additions use framework "Foundation" use script "BridgePlus" set theString to "Sunny September 4 Fri., September 1 Fri. Sept. 2 Fri Sep 3 Sep. 4 Sept. 9 9/8 9/7/15 09/06/2015 9/5/2015 2015/9/5 2015年9月5日 2015年9月5日(土) 2015.9.5 " set theDates to ASify from (my getDatesIn:theString) –> {date "2015年9月4日金曜日 12:00:00", date "2015年9月1日火曜日 12:00:00", date "2015年9月2日水曜日 12:00:00", date "2015年9月3日木曜日 12:00:00", date "2015年9月4日金曜日 12:00:00", date "2015年9月9日水曜日 12:00:00", date "2009年7月15日水曜日 12:00:00", date "2015年9月6日日曜日 12:00:00", date "2015年9月5日土曜日 12:00:00", date "2015年9月5日土曜日 12:00:00", date "2015年9月5日土曜日 12:00:00", date "2015年9月5日土曜日 12:00:00"} on getDatesIn:aString set anNSString to current application’s NSString’s stringWithString:aString set {theDetector, theError} to current application’s NSDataDetector’s dataDetectorWithTypes:(current application’s NSTextCheckingTypeDate) |error|:(reference) set theMatches to theDetector’s matchesInString:anNSString options:0 range:{0, anNSString’s |length|()} set theResults to theMatches’s valueForKey:"date" return theResults as list end getDatesIn: |
与えられたテキストから電話番号を抽出 v2
AppleScript名:与えられたテキストから電話番号を抽出 v2 |
— Created 2015-08-21 by Shane Stanley — Modified 2015-08-21 by Takaaki Naganoya — Modified 2015-08-22 by Shane Stanley use AppleScript version "2.4" use scripting additions use framework "Foundation" set theString to "長野谷隆昌 (Takaaki Naganoya) maro@piyocast.com http://piyocast.com/as 2015年8月21日〜23日 080-1111-2222 東京都練馬区中村橋1-2-3 " set theDates to (extractPhoneNumberFromNaturalText(theString)) –> {"080-1111-2222"} on extractPhoneNumberFromNaturalText(aString) set anNSString to current application’s NSString’s stringWithString:aString set {theDetector, theError} to current application’s NSDataDetector’s dataDetectorWithTypes:(current application’s NSTextCheckingTypePhoneNumber) |error|:(reference) set theMatches to theDetector’s matchesInString:anNSString options:0 range:{0, anNSString’s |length|()} set theResults to theMatches’s valueForKey:"phoneNumber" return theResults as list end extractPhoneNumberFromNaturalText |
与えられたテキストから住所を抽出 v2
AppleScript名:与えられたテキストから住所を抽出 v2 |
— Created 2015-08-21 by Shane Stanley — Modified 2015-08-21 by Takaaki Naganoya — Modified 2015-08-22 by Shane Stanley use AppleScript version "2.4" use scripting additions use framework "Foundation" set theString to "長野谷隆昌 (Takaaki Naganoya) maro@piyocast.com http://piyocast.com/as 2015年8月21日〜23日 東京都練馬区中村橋1-2-3 " set theDates to (extractAddressFromNaturalText(theString)) –> {{State:"東京都", Street:"中村橋1-2-3", City:"練馬区"}} on extractAddressFromNaturalText(aString) set anNSString to current application’s NSString’s stringWithString:aString set {theDetector, theError} to current application’s NSDataDetector’s dataDetectorWithTypes:(current application’s NSTextCheckingTypeAddress) |error|:(reference) set theMatches to theDetector’s matchesInString:anNSString options:0 range:{0, anNSString’s |length|()} set theResults to theMatches’s valueForKey:"addressComponents" return theResults as list end extractAddressFromNaturalText |
与えられたテキストからリンクURLを抽出 v3
AppleScript名:与えられたテキストからリンクURLを抽出 v3 |
— Created 2015-08-21 by Shane Stanley — Modified 2015-08-21 by Takaaki Naganoya — Modified 2015-08-22 by Shane Stanley — Modified 2015-08-23 by Takaaki Naganoya use AppleScript version "2.4" use scripting additions use framework "Foundation" set theString to "長野谷隆昌 (Takaaki Naganoya) maro@piyocast.com http://piyocast.com/as 2015年8月21日〜23日 080-1111-2222 東京都練馬区中村橋1-2-3 " set resList to (extractLinksFromNaturalText(theString)) –> {(NSURL) mailto:maro@piyocast.com, (NSURL) http://piyocast.com/as} set bList to {} repeat with i in resList set the end of bList to (i’s absoluteString()) as text end repeat bList –> {"mailto:maro@piyocast.com", "http://piyocast.com/as"} on extractLinksFromNaturalText(aString) set anNSString to current application’s NSString’s stringWithString:aString set {theDetector, theError} to current application’s NSDataDetector’s dataDetectorWithTypes:(current application’s NSTextCheckingTypeLink) |error|:(reference) set theMatches to theDetector’s matchesInString:anNSString options:0 range:{0, anNSString’s |length|()} set theResults to theMatches’s valueForKey:"URL" return theResults as list end extractLinksFromNaturalText |
与えられたテキストからメールアドレスを抽出する
AppleScript名:与えられたテキストからメールアドレスを抽出する |
— Created 2017-01-13 by Shane Stanley — Modified 2017-01-13 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set aText to " — Takaaki Naganoya Piyomaru Software http://piyocast.com/as maro@piyocast.com " set aRes to findEmailAddressesIn(aText) of me –> {"maro@piyocast.com"} on findEmailAddressesIn(someString) set theDD to current application’s NSDataDetector’s dataDetectorWithTypes:(current application’s NSTextCheckingTypeLink) |error|:(missing value) set theURLs to theDD’s matchesInString:someString options:0 range:{location:0, |length|:length of someString} set thePredicate to current application’s NSPredicate’s predicateWithFormat:"self.URL.scheme == ’mailto’" set theURLs to theURLs’s filteredArrayUsingPredicate:thePredicate set theURLs to theURLs’s valueForKeyPath:"URL.resourceSpecifier" set theURLs to (current application’s NSSet’s setWithArray:theURLs)’s allObjects() return theURLs as list end findEmailAddressesIn |
ASOCで文字列を%エンコーディング文字列に変換する
AppleScript名:ASOCで文字列を%エンコーディング文字列に変換する |
use AppleScript version "2.4" — Yosemite (10.10) or later use framework "Foundation" use scripting additions set aString to current application’s NSString’s stringWithString:"The time has come for all good “men”" set aString to (aString’s stringByAddingPercentEncodingWithAllowedCharacters:(current application’s NSCharacterSet’s URLQueryAllowedCharacterSet())) as text –> "The%20time%20has%20come%20for%20all%20good%20%E2%80%9Cmen%E2%80%9D" |
実体参照している文字列をデコードする(ASOC)
AppleScript名:実体参照している文字列をデコードする(ASOC) |
— Created 2017-01-18 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" –http://piyocast.com/as/archives/4568 set aStr to "龍馬伝" set aRes to decodeCharacterReference(aStr) of me –> "龍馬伝" set aStr to "\"第2エア\"" set aRes to decodeCharacterReference(aStr) of me –> "\"第2エア\"" on decodeCharacterReference(aStr) set anNSString to current application’s NSString’s stringWithString:aStr set theData to anNSString’s dataUsingEncoding:(current application’s NSUTF16StringEncoding) set styledString to current application’s NSAttributedString’s alloc()’s initWithHTML:theData documentAttributes:(missing value) set plainText to (styledString’s |string|()) as string return plainText end decodeCharacterReference |
指定文字コードでファイル書き出し(UTF-16LE)v2
AppleScript名:指定文字コードでファイル書き出し(UTF-16LE)v2 |
use AppleScript version "2.4" use scripting additions use framework "Foundation" set aStr to "高島屋" set aPath to choose file name set cStr to current application’s NSString’s stringWithString:aStr set thePath to POSIX path of aPath set aRes to cStr’s writeToFile:thePath atomically:false encoding:(current application’s NSUTF16LittleEndianStringEncoding) |error|:(missing value) |
指定文字コードでファイル書き出し(UTF-16BE)v2
AppleScript名:指定文字コードでファイル書き出し(UTF-16BE)v2 |
use AppleScript version "2.4" use scripting additions use framework "Foundation" set aStr to "高島屋" set aPath to choose file name set cStr to current application’s NSString’s stringWithString:aStr set thePath to POSIX path of aPath set aRes to cStr’s writeToFile:thePath atomically:false encoding:(current application’s NSUTF16BigEndianStringEncoding) |error|:(missing value) |
指定文字コードでファイル書き出し(SJIS)v2
AppleScript名:指定文字コードでファイル書き出し(SJIS)v2 |
use AppleScript version "2.4" use scripting additions use framework "Foundation" set aStr to "高島屋" set aPath to choose file name set cStr to current application’s NSString’s stringWithString:aStr set thePath to POSIX path of aPath set aRes to cStr’s writeToFile:thePath atomically:false encoding:(current application’s NSShiftJISStringEncoding) |error|:(missing value) |
指定文字コードでファイル書き出し(UTF-16)v2
AppleScript名:指定文字コードでファイル書き出し(UTF-16)v2 |
— Created 2014-11-11 by Takaaki Naganoya — 2014 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set aStr to "高島屋" set aPath to choose file name set cStr to current application’s NSString’s stringWithString:aStr set thePath to POSIX path of aPath set aRes to cStr’s writeToFile:thePath atomically:false encoding:(current application’s NSUnicodeStringEncoding) |error|:(missing value) |
指定文字コードでファイル書き出し(UTF-8)v2
AppleScriptの世界観では、文字コードというものは「存在しない」かのように位置付けています。2020年的な状況においてはUTF-8で事足りると言いたいところですが、(日本国内的には)100%そういうわけにも行きません。
AppleScriptのファイル書き出しコマンド(writeコマンド)では、UTF-8だけは文字コード指定のオプションが存在しています。ただし、ほぼ隠し仕様で生AppleEventコードを記述する必要があり、文字列で記述できる「予約語」が設けられているわけではありません。
予約語が存在していないといいつつ、やはり文字コードを指定できないのは不便だったこともあってか、予約語が存在していないものの、特定のファイル書き込み操作を行うと確実にその文字コードでファイル書き込みが行えるという「暗黙の文字コード指定」という手段が存在しています(macOS 10.8でここにバグを作られて大問題になりましたが、10.8.xで修正されました)。
しかし、macOS 10.10以降ではCocoaの機能を手軽に呼び出せるようになったために、状況が一変します。Cocoaの機能さえ呼び出せば、文字コード指定を行ってのファイルの書き出しも読み込みもお手の物。すでに、Cocoaの機能呼び出しのない世界は考えられないほど。なので、Cocoaの機能をAppleScriptネイティブの機能と位置づけ、Cocoaの機能を用いて文字コード指定を柔軟に行えるファイル入出力ルーチンが整備されています。
AppleScriptネイティブのwriteコマンドは、このCocoaの機能に影響を受けています。Cocoaの機能を利用する宣言文、
use framework "Foundation"
がある場合には、writeコマンドをそのまま記述できず(実行時にエラーになる)、tell current application〜end tellのブロック内に記述する必要が出てきます。そのため、掲載のファイル書き出しルーチンのプログラムリスト「UTF8でファイル書き込み」には、一見して無意味と思えるtell current application〜end tellが記述されています。ただ、対処方法がわかっているので使い回しを行うファイル書き出しルーチン側に本記述を書いておくことにしています。
正直なところ、文字コード指定をともなうファイル入出力については、Cocoaの機能を利用した方が手軽なので(AppleScriptのファイル追記モードは便利ですが)Cocoaの機能を使う方法だけ掲載しておけばよいと考えます。ごくまれに、古いバージョンのOS用にScriptを書かなくてはならないとか、Cocoa Scriptingを考慮していない偏屈なアプリケーションのScriptingで必要になることがある、程度のものでしょうか。
AppleScript名:指定文字コードでファイル書き出し(UTF-8)v2 |
use AppleScript version "2.4" use scripting additions use framework "Foundation" set aStr to "高島屋" set aPath to choose file name set cStr to current application’s NSString’s stringWithString:aStr set thePath to POSIX path of aPath set aRes to cStr’s writeToFile:thePath atomically:false encoding:(current application’s NSUTF8StringEncoding) |error|:(missing value) |
AppleScript名:UTF8でファイル書き込み |
set dFol to (path to desktop) as string set fullPath to dFol & "test_utf8.txt" set aStr to "髙島屋" set fRes to writeToFileAsUTF8(aStr, fullPath, false) of me on writeToFileAsUTF8(thisData, targetFile, appendF as boolean) tell current application try set the targetFile to the targetFile as text set the openTargetFile to open for access file targetFile with write permission if appendF is false then set eof of the openTargetFile to 0 write thisData to the openTargetFile as «class utf8» starting at eof close access the openTargetFile return true on error try close access file targetFile end try return false end try end tell end writeToFileAsUTF8 |
指定文字コードでファイル書き出し(EUC)
AppleScript名:指定文字コードでファイル書き出し(EUC) |
use AppleScript version "2.4" use scripting additions use framework "Foundation" set aStr to "高島屋" set aPath to choose file name set cStr to current application’s NSString’s stringWithString:aStr set thePath to POSIX path of aPath set aRes to cStr’s writeToFile:thePath atomically:false encoding:(current application’s NSJapaneseEUCStringEncoding) |error|:(missing value) |