AppleScript名:stringFrom_makingIt のサンプル v2 |
— Created 2015-01-04 by Takaaki Naganoya — 2015 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use bPlus : script "BridgePlus" –https://www.macosxautomation.com/applescript/apps/BridgePlus.html load framework set res0 to current application’s SMSForder’s |description|() as text –> "SMSForder” –Calculates the MD5 hash of a string set aStr to "0123456789" set a to bPlus’s MD5From:aStr –> "781e5e245d69b566979b86e28d23f2c7" –Converts straight quote marks into typographer’s quote marks set bStr to "’a’" set b to bPlus’s smartQuotedFrom:bStr –> "‘a’" –Converts typographer’s quote marks into straight quote marks set cStr to "‘a’" set c to bPlus’s unsmartQuotedFrom:cStr –> "’a’" –Encodes the five reserved XML characters only set d1Str to "&\"<>’" set d1 to bPlus’s encodedXMLFrom:d1Str –> "&"<>'" –Decode the five reserved XML characters only set d2Str to "&"<>'" set d2 to bPlus’s unencodedForXMLFrom:d2Str –> "&\"<>’" –Encodes characters outside ASCII 32-126 in hexadecimal form (&#xHHHH;) set eStr to "あいうえお" set e to bPlus’s encodedHexFrom:eStr –> "あいうえお" –Encodes characters outside ASCII 32-126 in decimal form (&#DD;), for use in HTML set fStr to "あいうえお" set f to bPlus’s encodedDecimalFrom:fStr –> "あいうえお" –Decodes characters that appear in decimal form (&#DD;) or hexidecimal form (&#xHHHH;), as used in XML and HTML set gStr to "あいうえお" set g to bPlus’s decodedDecimalFrom:gStr –> "あいうえお" –Deletes any paragraphs that are empty or contain only spaces and/or tabs set hStr to "a aaa a a " set h to bPlus’s emptyLineFreeFrom:hStr –> (* "a aaa a a a" *) –Converts runs of more than one space to a single space character, and trims spaces from the beginning and end of paragraphs. set hStr to " aaaaa bbbb ccccc " set h to bPlus’s cleanSpacedFrom:hStr –> "aaaaa bbbb ccccc" |
カテゴリー: Text
ICUTransformのサンプル v2
AppleScript名:ICUTransformのサンプル v2 |
–Sample Code — Created 2015-01-06 by Takaaki Naganoya use AppleScript version "2.4" use scripting additions use framework "Foundation" use bPlus : script "BridgePlus" –https://www.macosxautomation.com/applescript/apps/BridgePlus.html set theString to "ながのや, たかあき" –Hiragana set aRes to (bPlus’s transformedFrom:theString ICUTransform:"Hiragana-Latin" inverse:false) as text –> "naganoya, takaaki" set theString to "ながのや, たかあき" –Hiragana set aRes to (bPlus’s transformedFrom:theString ICUTransform:"Hiragana-Katakana" inverse:false) as text –> "ナガノヤ, タカアキ"–Katakana set theString to "Takaaki, Naganoya" set aRes to (bPlus’s transformedFrom:theString ICUTransform:"Latin-Hiragana" inverse:false) as text –> "たかあき、 ながのや"–Hiragana set theString to "Takaaki, Naganoya" set aRes to (bPlus’s transformedFrom:theString ICUTransform:"Latin-Katakana" inverse:false) as text –> "タカアキ、 ナガノヤ"–Katakana set theString to "Shane, Stanley" set aRes to (bPlus’s transformedFrom:theString ICUTransform:"Latin-Katakana" inverse:false) as text –> "シャネ、 スタンレイ"–Katakana…..this seems odd. "シェーン, スタンリー" will be a right spelling set theString to "Takaaki, Naganoya" set aRes to (bPlus’s transformedFrom:theString ICUTransform:"Halfwidth-Fullwidth" inverse:false) as text –> "Takaaki, Naganoya"–Double Width Alphabet set theString to "Naganoya, Takaaki" set aRes to (bPlus’s transformedFrom:theString ICUTransform:"Latin-Hangul" inverse:false) as text –> "나가노야, 타카아키"–Hangul |
文字列の長さを求める
AppleScript名:文字列の長さを求める |
— Created 2015-09-02 by Takaaki Naganoya — 2015 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set a to current application’s NSMutableString’s stringWithString:"あいうえお" set b to a’s |length|() –> 5 set c to "あいうえお" set d to length of c –> 5 |
considering numeric strings
AppleScript名:considering numeric strings |
set aVer to "10.10.1" set bVer to "10.9.5" –OS X 10.4で導入されたconsidering numeric strings considering numeric strings if aVer > bVer then display dialog "Yosemite is newer than Mavericks" else display dialog "Whoa!" end if end considering if aVer > bVer then display dialog "Yosemite is newer than Mavericks" else display dialog "Whoa!" end if |
NSStringによりバージョン文字列比較
AppleScript名:NSStringによりバージョン文字列比較 |
— Created 2015-07-27 by Takaaki Naganoya — 2015 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set aVer to "10.10.0" set bVer to "10.10" set aVerStr to current application’s NSString’s stringWithString:aVer set bVerStr to current application’s NSString’s stringWithString:bVer set aRes to aVerStr’s compare:bVer options:(current application’s NSNumericSearch) –> 1 –1:aVer > bVer, 0:aVer = bVer, -1:bVer > aVer |
バージョン番号文字列の正確な比較
AppleScript名:バージョン番号文字列の正確な比較 |
— Created 2015-07-27 by Takaaki Naganoya — 2015 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set a to "10.10" set b to "10.10.0" set aRes to compareVersionNumericStrings(a, b) of me –> "A=B" set a to "10.10" set b to "10.10.0.1" set aRes to compareVersionNumericStrings(a, b) of me –> "B>A" set a to "10.10.1" set b to "10.10.0.1" set aRes to compareVersionNumericStrings(a, b) of me –> "A>B" set a to "10.10.1" set b to "10.9.1.1" set aRes to compareVersionNumericStrings(a, b) of me –> "A>B" –バージョン番号文字列の正確な比較 on compareVersionNumericStrings(a, b) set aList to parseVersionNumber(a) of me –> {"10", "10"} set bList to parseVersionNumber(b) of me –> {"10", "10", "0"} set aLen to length of aList set bLen to length of bList if aLen = bLen then –何もしない set buryTimes to 0 else if aLen > bLen then set buryTimes to (aLen – bLen) set bList to addNullItems(bList, "0", buryTimes) of me else if bLen > aLen then set buryTimes to (bLen – aLen) set aList to addNullItems(aList, "0", buryTimes) of me end if set cLen to length of aList –aListもbListも同じ長さなのでこれでいい repeat with i from 1 to cLen set aItem to contents of item i of aList set bItem to contents of item i of bList considering numeric strings if aItem > bItem then return "A>B" else if aItem < bItem then return "B>A" end if end considering end repeat return "A=B" end compareVersionNumericStrings –バージョン番号文字列からメジャーバージョンを取り出し数値として返す on parseVersionNumber(a) set aStr to current application’s NSString’s stringWithString:a set aRes to (aStr’s componentsSeparatedByString:".") set bRes to aRes’s allObjects() return bRes as list end parseVersionNumber –指定リストの末尾に指定個数、埋め草を追加する on addNullItems(aList, anItem, aTimes) copy aList to bList repeat aTimes times set the end of bList to anItem end repeat return bList end addNullItems |
NFKC Casefoldの影響を除外した文字列比較
AppleScript名:NFKC Casefoldの影響を除外した文字列比較 |
use AppleScript version "2.4" use framework "Foundation" use scripting additions set a to "㍑" set b to "リットル" log (a = b) –AppleScriptネイティブの文字列比較だとこれらが混同されることに注意 –> true set c to compStrA_B_(a, b) –> false set a to "バビブベボ" set b to "ハヒフヘホ" log (a = b) –> false set c to compStrA_B_(a, b) –> false set a to "あいうえお" set b to "アイウエオ" log (a = b) –AppleScriptネイティブの文字列比較だとこれらが混同されることに注意 –> true set c to compStrA_B_(a, b) –> false –文字列比較をASOC(Cocoa)とAppleScriptで実施するテストルーチン on compStrA:a b:b set aStr to current application’s NSString’s stringWithString:a set bStr to current application’s NSString’s stringWithString:b return (aStr’s isEqualToString:bStr) as boolean end compStrA:b: |
Umlautを無視した文字列比較(NSDiacriticInsensitiveSearch)
AppleScript名:Umlautを無視した文字列比較(NSDiacriticInsensitiveSearch) |
— Created 2015-03-28 by Shane Stanley use AppleScript version "2.4" use scripting additions use framework "Foundation" set aRes to compareIgnoringDiacriticals("AbC", "ÂbC") –> true set aRes to compareIgnoringDiacriticals("Abc", "Ábc") –> true set aRes to compareIgnoringDiacriticals("abc", "åbc") –> true –Strings Compare with ignoring Umlaut on compareIgnoringDiacriticals(aText as text, bText as text) set aStr to current application’s NSString’s stringWithString:aText return (aStr’s compare:bText options:((current application’s NSCaseInsensitiveSearch) + (current application’s NSDiacriticInsensitiveSearch as integer))) = current application’s NSOrderedSame end compareIgnoringDiacriticals |
テキストからリガチャーを削除する v2
AppleScript名:テキストからリガチャーを削除する v2 |
— Created 2017-01-17 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set inputStringWithLigatures to "ꜲꜳÆæꜴꜵꜶꜷꜸꜹꜺꜻꜼꜽffffifflfiflŒœꝎstꜨꜩᵫꝠꝡ" & "LJLjNJNjnjDZDzdzIJij" set aRes to removeLigaturesFromString(inputStringWithLigatures) of me –> "AAaaAEaeAOaoAUauAVavAVavAYayffffifflfiflOEoeOOstTZtzueVYvyLJLjNJNjnjDZDzdzIJij" on removeLigaturesFromString(inputStringWithLigatures) set theString to current application’s NSString’s stringWithString:inputStringWithLigatures # Convert what may be done applying transform "ÆæffffifflfiflŒœᵫ" & "LJLjNJNjnjDZDzdzIJij" set inputStringWithLigatures to (theString’s stringByApplyingTransform:"Latin-ASCII" |reverse|:false) as text # Treat the remaining ligatures set searchStrings to {"Ꜳ", "ꜳ", "Ꜵ", "ꜵ", "Ꜷ", "ꜷ", "Ꜹ", "ꜹ", "Ꜻ", "ꜻ", "Ꜽ", "ꜽ", "Ꝏ", "Ꜩ", "ꜩ", "Ꝡ", "ꝡ"} — if you find others, add them here set replaceStrings to {"AA", "aa", "AO", "ao", "AU", "au", "AV", "av", "AV", "av", "AY", "ay", "OO", "TZ", "tz", "VY", "vy"} — if you find others, add them here set saveTID to AppleScript’s text item delimiters considering case set i to 0 repeat with lig in searchStrings set i to i + 1 set AppleScript’s text item delimiters to {lig} set inputStringWithLigatures to text items of inputStringWithLigatures set AppleScript’s text item delimiters to {item i of replaceStrings} set inputStringWithLigatures to inputStringWithLigatures as text end repeat end considering set AppleScript’s text item delimiters to saveTID return inputStringWithLigatures end removeLigaturesFromString |
指定キーワードの出現回数のカウント
AppleScript名:指定キーワードの出現回数のカウント |
set origText to "このテキストはいずれ何らかの指定キーワードが何回出現するかという見地から評価される運命にある。評価されるということは、何がしかの文法的評価体系によって分析されるということである(すんごい、いいかげんな日本語)。" set aKeyText to "何" set freqNum to retFrequency(origText, aKeyText) of me –指定文字列内の指定キーワードの出現回数を取得する on retFrequency(origText, aKeyText) set aRes to parseByDelim(origText, aKeyText) of me return ((count every item of aRes) – 1) end retFrequency on parseByDelim(aData, aDelim) set curDelim to AppleScript’s text item delimiters set AppleScript’s text item delimiters to aDelim set dList to text items of aData set AppleScript’s text item delimiters to curDelim return dList end parseByDelim |
ASOCで文字を逆順に v2(CJK文字などマルチバイト対応)
AppleScript名:ASOCで文字を逆順に v2(CJK文字などマルチバイト対応) |
— Created 2015-09-01 by Takaaki Naganoya — Modified 2015-09-01 by Shane Stanley –Consider CJK Characters & Emoji –http://stackoverflow.com/questions/6720191/reverse-nsstring-text use AppleScript version "2.4" use scripting additions use framework "Foundation" set aUUID to (current application’s NSUUID’s UUID()’s UUIDString()) as text –> "46EF17B7-CB3E-4DD9-BA8A-013D3B30A80A" set aUUID to "😀😐" & aUUID & "😀😐" –> "😀😐46EF17B7-CB3E-4DD9-BA8A-013D3B30A80A😀😐" set revUUID to reversedStr(aUUID) as text –> "😐😀A08A03B3D310-A8AB-9DD4-E3BC-7B71FE64😐😀" on reversedStr(paramStr as text) set aStr to current application’s NSString’s stringWithString:paramStr set strLength to aStr’s |length|() set revStr to current application’s NSMutableString’s stringWithCapacity:strLength set charIndex to strLength – 1 repeat while charIndex > -1 set subStrRange to aStr’s rangeOfComposedCharacterSequenceAtIndex:charIndex revStr’s appendString:(aStr’s substringWithRange:subStrRange) set charIndex to (location of subStrRange) – 1 end repeat return revStr end reversedStr |
バージョン番号文字列からメジャーバージョンを取り出し数値として返す v4
AppleScript名:バージョン番号文字列からメジャーバージョンを取り出し数値として返す v4 |
use AppleScript version "2.5" use scripting additions use framework "Foundation" set a to "10.0.1" set b to retMajorVersionNumber(a) of me –> 10 set a to "9.10" set b to retMajorVersionNumber(a) of me –> 9 –バージョン番号文字列からメジャーバージョンを取り出し数値として返す on retMajorVersionNumber(a) set aStr to current application’s NSString’s stringWithString:a –> "10.0.1" (NSString) set aRes to (aStr’s componentsSeparatedByString:".") –> {"10","0","1"} (NSArray) set bRes to aRes’s firstObject() –> "10" (NSString) set cRes to bRes’s integerValue() –> 10 return cRes as integer end retMajorVersionNumber |
2つのPDFのテキストの指定ページの差分をVimdiffで表示する v2
2つのPDFの指定ページのテキスト内容をvimdiffで差分表示するAppleScriptです。
Terminal上でvimdiffによる差分比較を表示します。Mac AppStoreに出したアプリ(Double PDF)の部品として使ったらリジェクトされました。Terminal.appを使うものはリジェクトなんだそうで。
半日ぐらいですぐに別のルーチンに差し替えたので本Scriptはあっという間に闇から闇へと葬られました。
FileMergeがAppleScript用語辞書を持っていて単独配布されていたらいろいろ問題は解決される気がします。
AppleScript名:2つのPDFのテキストの指定ページの差分をVimdiffで表示する v2 |
— Created 2017-06-24 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "Quartz" set aPageNum to 103 –diffを表示する対象ページ set aPath to POSIX path of (choose file of type {"com.adobe.pdf"}) set a1Name to makeTmpFileStrPath(aPath) of me set aStr to retBodyStringFromPdf(aPath, aPageNum) of me set aStr1 to cleanUpText(aStr as string, string id 13, string id 10) of me –改行コードをCRからLFに置換 aStr1’s writeToFile:a1Name atomically:true encoding:(current application’s NSUTF8StringEncoding) |error|:(missing value) set bPath to POSIX path of (choose file of type {"com.adobe.pdf"}) set b1Name to makeTmpFileStrPath(bPath) of me set bStr to retBodyStringFromPdf(bPath, aPageNum) of me set bStr1 to cleanUpText(bStr as string, string id 13, string id 10) of me –改行コードをCRからLFに置換 bStr1’s writeToFile:b1Name atomically:true encoding:(current application’s NSUTF8StringEncoding) |error|:(missing value) set sText to "vimdiff " & quoted form of (a1Name as string) & " " & quoted form of (b1Name as string) doComInTerminalWindow(sText) of me on makeTmpFileStrPath(aPath) set aTmpPath to current application’s NSString’s stringWithString:(POSIX path of (path to temporary items)) set aUUID to current application’s NSUUID’s UUID()’s UUIDString() set aName to aUUID’s stringByAppendingPathExtension:"txt" set a1FullPath to (aTmpPath’s stringByAppendingString:aName) return a1FullPath end makeTmpFileStrPath on retBodyStringFromPdf(thePath as string, targPageNum as integer) set anNSURL to (current application’s |NSURL|’s fileURLWithPath:thePath) set theDoc to current application’s PDFDocument’s alloc()’s initWithURL:anNSURL set theCount to theDoc’s pageCount() as integer if targPageNum > theCount then return "" set aPage to (theDoc’s pageAtIndex:(targPageNum – 1)) set tmpStr to (aPage’s |string|()) return tmpStr end retBodyStringFromPdf on doComInTerminalWindow(aCMD) using terms from application "Terminal" tell application id "com.apple.Terminal" activate set wCount to count (every window whose visible is true) if wCount = 0 then –ウィンドウが1枚も表示されていない場合 do script "pwd" activate set size of front window to {1280, 700} do script aCMD in front window else –すでにウィンドウが表示されている場合 do script "pwd" in front window activate set size of front window to {1280, 700} do script aCMD in front window end if end tell end using terms from end doComInTerminalWindow on cleanUpText(someText, targStr, repStr) set theString to current application’s NSString’s stringWithString:someText set targString to current application’s NSString’s stringWithString:targStr set repString to current application’s NSString’s stringWithString:repStr set theString to theString’s stringByReplacingOccurrencesOfString:targString withString:repString options:(current application’s NSRegularExpressionSearch) range:{location:0, |length|:length of someText} return theString end cleanUpText |
PDFから本文テキストを抽出して配列にストアして文字列検索 v2
指定のPDFの本文テキストから、同義語をリストで与えて文字列検索を行い、出現ページのページ数を返すAppleScriptです。
PDFからの索引作成を行うために作成したものです。最初に対象PDFから本文テキストを(ページごとに)抽出してテキスト検索キャッシュを作成。
まずはこのテキスト検索キャッシュへの検索を行ったのち、ヒットしなかったらPDFに対して文字列検索を行います。
筆者の実行環境(MacBook Pro Retina 2012)で483ページある「AppleScript最新リファレンス」に対して本Scriptを実行して4.66 secぐらいです。
テキスト検索キャッシュの効果を発揮するためには、索引作成の同義語リストをまとめて与えて処理するのがベストでしょう。
AppleScript名:PDFから本文テキストを抽出して配列にストアして文字列検索 v2 |
— Created 2017-06-18 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "Quartz" use bPlus : script "BridgePlus" –検索対象の語群 set sList to {"Piyomaru Software", "ぴよまるソフトウェア"} –considering case set thePath to POSIX path of (choose file of type {"com.adobe.pdf"}) set aRes to findWordListInPDFContents(thePath, sList) of me –> {1, 3, 4, 71, 72, 75, 95, 96, 97, 98, 420, 429, 479, 483} —PDF本文テキスト中から、語群で出現ページをリストで取得(索引作成用) on findWordListInPDFContents(thePOSIXPath as string, sList as list) script spdPDF property textCache : missing value property aList : {} end script –PDFのテキスト内容をあらかじめページごとに読み取って、検索用のテキストキャッシュを作成 set anNSURL to (current application’s |NSURL|’s fileURLWithPath:thePOSIXPath) set theDoc to current application’s PDFDocument’s alloc()’s initWithURL:anNSURL set theCount to theDoc’s pageCount() as integer set (textCache of spdPDF) to current application’s NSMutableArray’s new() repeat with i from 0 to (theCount – 1) set aPage to (theDoc’s pageAtIndex:i) set tmpStr to (aPage’s |string|()) ((textCache of spdPDF)’s addObject:{pageIndex:i + 1, pageString:tmpStr}) end repeat –主にテキストキャッシュを対象にキーワード検索 repeat with s in sList –❶部分一致で抽出 set bRes to ((my filterRecListByLabel1((textCache of spdPDF), "pageString contains ’" & s & "’"))’s pageIndex) as list –❷、❶のページ単位のテキスト検索で見つからなかった場合(ページ間でまたがっている場合など) if bRes = {} then set bRes to {} set theSels to (theDoc’s findString:s withOptions:0) repeat with aSel in theSels set thePage to (aSel’s pages()’s objectAtIndex:0)’s label() set curPage to (thePage as integer) if curPage is not in bRes then set the end of bRes to curPage end if end repeat end if set the end of (aList of spdPDF) to bRes end repeat –2D list to 1D list conversion (Flatten) load framework set bList to (current application’s SMSForder’s arrayByFlattening:(aList of spdPDF)) as list –Uniquefy set cList to uniquifyList(bList) of me –Sort 1D List set anArray to current application’s NSArray’s arrayWithArray:cList set sortRes1 to (anArray’s sortedArrayUsingSelector:"compare:") as list of string or string –as anything set (textCache of spdPDF) to "" –Purge set (aList of spdPDF) to {} –Purge return sortRes1 end findWordListInPDFContents –リストに入れたレコードを、指定の属性ラベルの値で抽出 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 return filteredArray end filterRecListByLabel1 on uniquifyList(aList as list) set aArray to current application’s NSArray’s arrayWithArray:aList set bArray to aArray’s valueForKeyPath:"@distinctUnionOfObjects.self" return bArray as list end uniquifyList |
GHKitのじっけん
AppleScript名:GHKitのじっけん |
— Created 2016-04-12 by Takaaki Naganoya — 2016 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "GHKit" –https://github.com/gabriel/GHKit –AppleScriptObjC uses "_" as special character (equivalent to ":" in method names). So, I changed them in whole project. – Original Method Name: gh_parseISO8601: – Converted Method Name: GHparseISO8601: set aStr to current application’s NSString’s stringWithString:"Sun, 06 Nov 1994 08:49:37 +0000" set aDate to (current application’s NSDate’s GHparseRFC822:aStr) as date –> date "1994年11月6日日曜日 17:49:37" set bStr to current application’s NSString’s stringWithString:"1997-07-16T19:20:30.045Z" set bDate to (current application’s NSDate’s GHparseISO8601:bStr) as date –> date "1997年7月17日木曜日 4:20:30" set cDateStr to bDate’s GHformatHTTP() –> (NSString) "Wed, 16 Jul 1997 19:20:30 GMT" set dDate to current application’s NSDate’s GHparseTimeSinceEpoch:(1.23456789E+9) –> (NSDate) 2009-02-13 23:31:30 +0000 set eDate to current application’s NSDate’s |date|() eDate’s GHisToday() as boolean –> true —–GHyesterday() cause error.. set fDate to eDate’s GHaddDays:-1 fDate’s GHwasYesterday() as boolean –> true set ffRes to ((fDate’s GHtimeAgo:false)’s |description|()) as string –> "1 day" set anArray to current application’s NSArray’s arrayWithArray:{1, 1, 3} set cArray to anArray’s GHuniq() as list –> {1, 3} set aDic to current application’s NSDictionary’s dictionaryWithDictionary:{key1:2, key2:3.1, key3:true} set aJSONstr to (aDic’s GHtoJSON:(current application’s NSJSONWritingPrettyPrinted) |error|:(missing value)) as string (* –> (NSString) "{\n "key1" : 2,\n "key3" : true,\n "key2" : 3.1\n}" *) ( current application’s NSString’s GHisBlank:" ") as boolean–> true ( current application’s NSString’s GHisBlank:(missing value)) as boolean–> true set aStr to current application’s NSString’s stringWithString:" some text " set a1Str to (aStr’s GHstrip()) as string –> "some text" set a2Str to (aStr’s GHpresent()) as string –> " some text " set a3Str to aStr’s GHreverse() –> " txet emos " set a4Str to aStr’s GHcount:"e" –> 2 |
Absolute Timeを取得
AppleScript名:Absolute Timeを取得 |
— Created 2016-07-12 by Takaaki Naganoya — 2016 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" –タイムスタンプ取得(Jan 1 2001 00:00:00 GMTからの相対秒、Absolute Timeで取得) set aTime to current application’s NSString’s stringWithFormat_("%@", current application’s CFAbsoluteTimeGetCurrent()) as string –> "490022703.57607" |
自然言語テキストから日付を抽出
NSDataDetectorを用いて、自然言語テキスト(ここでは日本語のテキスト)から日付の情報を抽出するAppleScriptです。
AppleScript名:自然言語テキストから日付を抽出 |
— Created 2015-10-08 by Takaaki Naganoya — 2015 Piyomaru Software use AppleScript version "2.5" use framework "Foundation" use scripting additions set theDate to my getDatesIn:"本テキストには次の火曜日という日付情報を含んでいる。" log theDate –> date "2015年10月13日火曜日 12:00:00" set theDate to my getDatesIn:"本テキストには今度の土曜日という日付情報を含んでいる。" log theDate –> date "2015年10月10日土曜日 12:00:00" set theDate to my getDatesIn:"昨日うな重を食べた。" log theDate –> date "2015年10月7日水曜日 12:00:00" –set theDate to my getDatesIn:"一昨日何を食べたか覚えていない。" –> error number -2700 No date found –set theDate to my getDatesIn:"The day after tommorow." –set theDate to my getDatesIn:"相対日付の認識能力は低い。明後日はいつだ?" –> error number -2700 No date found –set theDate to my getDatesIn:"本テキストには元旦という日付情報を含んでいる。" –This means 1/1 in next year –> error number -2700 No date found on getDatesIn:aString set anNSString to current application’s NSString’s stringWithString:aString set theDetector to current application’s NSDataDetector’s dataDetectorWithTypes:(current application’s NSTextCheckingTypeDate) |error|:(missing value) set theMatch to theDetector’s firstMatchInString:anNSString options:0 range:{0, anNSString’s |length|()} if theMatch = missing value then error "No date found with String:" & aString set theDate to theMatch’s |date|() return theDate as date end getDatesIn: |
GMTとの時差を求める
AppleScript名:GMTとの時差を求める |
set tDIff to (time to GMT) / 3600 –> 9.0 |
UTCTime StringとNSDateの相互変換
AppleScript名:UTCTime StringとNSDateの相互変換 |
— Created 2015-02-24 by Shane Stanley — Changed 2015-02-25 By Takaaki Naganoya use AppleScript version "2.4" use scripting additions use framework "Foundation" set aStr to retUTCTimeString() –> "2018-02-13T12:40:01.936" set aNSDate to retNSDateFromUTCString(aStr) as date –> date "2018年2月13日火曜日 21:39:43" –Current Date -> UTCTime String on retUTCTimeString() –There is need to get Current Calendar in my Time Zone set aCalendar to current application’s NSCalendar’s currentCalendar() set aTimeZone to (aCalendar’s timeZone) set tDiff to (aTimeZone’s secondsFromGMT()) set theNSDateFormatter to current application’s NSDateFormatter’s alloc()’s init() theNSDateFormatter’s setDateFormat:"yyyy-MM-dd’T’HH:mm:ss.SSS" theNSDateFormatter’s setTimeZone:(current application’s NSTimeZone’s timeZoneForSecondsFromGMT:tDiff) return (theNSDateFormatter’s stringFromDate:(current application’s NSDate’s |date|())) as text end retUTCTimeString –UTCTime String -> NSDate on retNSDateFromUTCString(aText) set aStr to current application’s NSString’s stringWithString:aText set theNSDateFormatter to current application’s NSDateFormatter’s alloc()’s init() theNSDateFormatter’s setDateFormat:"yyyy-MM-dd’T’HH:mm:ss.SSS" theNSDateFormatter’s setTimeZone:(current application’s NSTimeZone’s timeZoneForSecondsFromGMT:0) return theNSDateFormatter’s dateFromString:aStr end retNSDateFromUTCString |
ISO8601フォーマット日付のテキストをdateに変換
AppleScript名:ISO8601フォーマット日付のテキストをdateに変換 |
— Created 2015-08-28 20:19:04 +0900 by Takaaki Naganoya — 2015 Piyomaru Software — http://www.tondering.dk/claus/cal/iso8601.php use AppleScript version "2.4" use scripting additions use framework "Foundation" use BridgePlus : script "BridgePlus" –https://www.macosxautomation.com/applescript/apps/BridgePlus.html set aStr to "2010-12-01T21:35:43+09:00" BridgePlus’s datesFromStrings:{aStr} inFormat:"yyyy-MM-dd’T’HH:mm:ssZ" –> {date "2010年12月1日水曜日 21:35:43"} set aStr to "2010-12-01 21:35:43" BridgePlus’s datesFromStrings:{aStr} inFormat:"yyyy-MM-dd HH:mm:ss" –> {date "2010年12月1日水曜日 21:35:43"} |