Archive for the 'NSCharacterSet' Category

2016/11/30 Yahoo! 形態素解析APIで日本語テキストを解釈

Yahoo!の形態素解析APIで、日本語テキストを形態素解析するAppleScriptです。

Yahoo!に開発者登録(無料)して、アプリケーションIDを取得し、リスト中のretAccessKey()ハンドラにアプリケーションIDを記入すると実行可能です。

単語ごとに「品詞」「よみがな」などを取得できます。辞書が充実しているためか、自分の名前も正しく単語として認識されました。

形態素解析エンジンはローカルに置いて、辞書をカスタマイズするべきだと思っていますが、Yahoo!のAPI(が備えている辞書)だとそれなりに使える感じがします。

Yahoo!のテキスト解析系APIはひととおり試してみましたが、

 校正支援API:漢字の誤変換は指摘してくれるが、助詞の間違いなどは指摘してくれない
 キーフレーズ抽出API:使えるかどうか評価が難しい
 かな漢字変換API:呼んで使えるが、使い道が難しい

この形態素解析APIが一番実用度が高そうな感じがします。

AppleScript名: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/jlp/ma/v1/parse.html

–日本語形態素解析Web APIは、24時間以内で1つのアプリケーションIDにつき50000件のリクエストが上限となっています。また、1リクエストの最大サイズを100KBに制限 しています。

property dictStack : missing value – stack to hold array of dictionaries
property textInProgress : “” – string to collect text as it is found
property anError : missing value – if we get an error, store it here

set japaneseText to “私の名前は長野谷です。”

set reqURLStr to “http://jlp.yahooapis.jp/MAService/V1/parse”

set aKey to retAccessKey() of me

set aRec to {|key|:aKey, sentence:japaneseText, results:“ma”, page:“1″, output:“xml”, appid:aKey}
set aURL to retURLwithParams(reqURLStr, aRec) of me
set aRes to callRestGETAPIAndParseXMLResults(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 aXMLres to (xml of aRes)

set parsedList to (aXMLres’s valueForKeyPath:“ResultSet.ma_result.word_list.word.surface.contents”) as list
–>  {”私”, “の”, “名前”, “は”, “長野谷”, “です”, “。”}

set yomiganaList to (aXMLres’s valueForKeyPath:“ResultSet.ma_result.word_list.word.reading.contents”) as list
–>  {”わたし”, “の”, “なまえ”, “は”, “ながのや”, “です”, “。”}

set kindList to (aXMLres’s valueForKeyPath:“ResultSet.ma_result.word_list.word.pos.contents”) as list
–>  {”名詞”, “助詞”, “名詞”, “助詞”, “名詞”, “助動詞”, “特殊”}

–GET methodのREST APIを呼ぶ
on callRestGETAPIAndParseXMLResults(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”
  
  
set aRes to current application’s NSURLConnection’s sendSynchronousRequest:aRequest returningResponse:(reference) |error|:(missing value)
  
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 aXmlRec to my makeRecordWithXML:resStr
  
  
  
–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 {xml:aXmlRec, responseCode:resCode, responseHeader:resHeaders}
end callRestGETAPIAndParseXMLResults

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

——–XMLParse Lib

on makeRecordWithXML:xmlString
  set my dictStack to current application’s NSMutableArray’s array() – empty mutable array
  
set anEmpty to current application’s NSMutableDictionary’s |dictionary|()
  (
my dictStack)’s addObject:anEmpty – add empty mutable dictionary
  
set my textInProgress to current application’s NSMutableString’s |string|() – empty mutable string
  
  
set anNSString to current application’s NSString’s stringWithString:xmlString
  
set theData to anNSString’s dataUsingEncoding:(current application’s NSUTF8StringEncoding)
  
  
set theNSXMLParser to current application’s NSXMLParser’s alloc()’s initWithData:theData
  
  
theNSXMLParser’s setDelegate:me
  
  
set theResult to theNSXMLParser’s parse()
  
if theResult then – went OK, get first item on stack
    return ((my dictStack)’s firstObject()) –as record
  else
    error (my anError’s localizedDescription() as text)
  end if
end makeRecordWithXML:

– this is an XML parser delegate method. Called when new element found
on parser:anNSXMLParser didStartElement:elementName namespaceURI:aString qualifiedName:qName attributes:aRecord
  set parentDict to my dictStack’s lastObject()
  
set childDict to current application’s NSMutableDictionary’s |dictionary|()
  
if aRecord’s |count|() > 0 then
    childDict’s setValue:aRecord forKey:“attributes”
  end if
  
  
set existingValue to parentDict’s objectForKey:elementName
  
  
if existingValue is not missing value then
    if (existingValue’s isKindOfClass:(current application’s NSMutableArray)) as boolean then
      set theArray to existingValue
    else
      set theArray to current application’s NSMutableArray’s arrayWithObject:existingValue
      
parentDict’s setObject:theArray forKey:elementName
    end if
    
    
theArray’s addObject:childDict
  else
    parentDict’s setObject:childDict forKey:elementName
  end if
  
  (
my dictStack)’s addObject:childDict
end parser:didStartElement:namespaceURI:qualifiedName:attributes:

– this is an XML parser delegate method. Called at the end of an element
on parser:anNSXMLParser didEndElement:elementName namespaceURI:aString qualifiedName:qName
  if my textInProgress’s |length|() > 0 then
    set dictInProgress to my dictStack’s lastObject()
    
dictInProgress’s setObject:textInProgress forKey:“contents”
    
set my textInProgress to current application’s NSMutableString’s |string|()
  end if
  
  
my dictStack’s removeLastObject()
end parser:didEndElement:namespaceURI:qualifiedName:

– this is an XML parser delegate method. Called when string is found. May be called repeatedly
on parser:anNSXMLParser foundCharacters:aString
  if (aString’s stringByTrimmingCharactersInSet:(current application’s NSCharacterSet’s whitespaceAndNewlineCharacterSet()))’s |length|() > 0 then
    (my textInProgress)’s appendString:aString
  end if
end parser:foundCharacters:

– this is an XML parser delegate method. Called when there’s an error
on parser:anNSXMLParser parseErrorOccurred:anNSError
  set my anError to anNSError
end parser:parseErrorOccurred:

★Click Here to Open This Script 

2016/11/21 HTMLReaderでWeb上のHTMLを取得する

オープンソースのフレームワーク「HTMLReader」を用いて、指定のURLのHTMLをダウンロードして指定要素を取り出すAppleScriptです。

実行にあたっては、HTMLReaderをダウンロードしてビルドし、~/Library/Frameworksにインストールしておく必要があります。

Github上のページで紹介されているサンプルを翻訳したもので、そのうち「Load a web page.」をblocks構文を使わずに大幅に書き換えてみました。

AppleScript名:HTMLReaderのじっけん
– Created 2016-06-10 by Takaaki Naganoya
– 2016 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "HTMLReader" –https://github.com/nolanw/HTMLReader
–http://piyocast.com/as/archives/4324

–Parse a string and find an element
set aMarkUp to current application’s NSString’s stringWithString:"<p><b>ぴよぴよ!</b><b>ぴよー</b></p>"
set aDocument to current application’s HTMLDocument’s documentWithString:aMarkUp
set anArray to ((aDocument’s nodesMatchingSelector:"b")’s textContent) as list
–>  {"ぴよぴよ!", "ぴよー"}

–Wrap one element in another.
set b to aDocument’s firstNodeMatchingSelector:"b"
set childArray to b’s parentNode()’s mutableChildren()
set aWrapper to current application’s HTMLElement’s alloc()’s initWithTagName:"div" attributes:{|class|:"special"}
childArray’s insertObject:aWrapper atIndex:(childArray’s indexOfObject:b)
b’s setParentNode:aWrapper
set htmlRes to (aDocument’s rootElement’s serializedFragment()) as string
–>  "<html><head></head><body><p><div class=\"special\"><b>ぴよぴよ!</b></div><b>ぴよー</b></p></body></html>"

★Click Here to Open This Script 

AppleScript名:HTMLReaderでWeb上のHTMLを取得する
– Created 2016-11-21 by Takaaki Naganoya
– 2016 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "HTMLReader" –https://github.com/nolanw/HTMLReader
–http://piyocast.com/as/archives/4324

set aStr to "https://github.com/nolanw/HTMLReader"
set aURL to (current application’s |NSURL|’s URLWithString:aStr)
set {exRes, headerRes, aData} to checkURLResourceExistence(aURL, 3) of me
if exRes = false then return –エラー発生時に処理打ち切り

–>  (NSDictionary) {Content-Encoding:"gzip", X-Runtime:"0.074184", Set-Cookie:"_gh_sess=eyJz…..",…….. X-Frame-Options:"deny", Content-Type:"text/html; charset=utf-8", X-Content-Type-Options:"nosniff", X-UA-Compatible:"IE=Edge,chrome=1"}

set conType to headerRes’s valueForKeyPath:"Content-Type"
–>  (NSString) "text/html; charset=utf-8"

set aHome to current application’s HTMLDocument’s documentWithData:aData contentTypeHeader:conType
set htmlSource to aHome’s rootElement()’s serializedFragment() –HTMLソース文字列

–(処理対象部分のみ掲載)
–> <meta content="HTMLReader - A WHATWG-compliant HTML parser in Objective-C." name="twitter:description">

set aDiv to aHome’s firstNodeMatchingSelector:".repository-meta-content"
set aWhiteSpace to current application’s NSCharacterSet’s whitespaceAndNewlineCharacterSet()
set dRes to (aDiv’s textContent()’s stringByTrimmingCharactersInSet:aWhiteSpace) as string
–>  "A WHATWG-compliant HTML parser in Objective-C."

– 指定URLにファイル(画像など)が存在するかチェック
–> {存在確認結果(boolean), レスポンスヘッダー(NSDictionary), データ(NSData)}
on checkURLResourceExistence(aURL, timeOutSec as real)
  set aRequest to (current application’s NSURLRequest’s requestWithURL:aURL cachePolicy:(current application’s NSURLRequestUseProtocolCachePolicy) timeoutInterval:timeOutSec)
  
set aRes to (current application’s NSURLConnection’s sendSynchronousRequest:aRequest returningResponse:(reference) |error|:(missing value))
  
set dRes to (first item of (aRes as list))
  
set bRes to (second item of (aRes as list))
  
if bRes is not equal to missing value then
    set hRes to (bRes’s allHeaderFields())
    
set aResCode to (bRes’s statusCode()) as integer
  else
    set hRes to {}
    
set aResCode to -1 –error
  end if
  
return {(aResCode = 200), hRes, dRes}
end checkURLResourceExistence

★Click Here to Open This Script 

2016/11/17 ぐるなびの「レストラン検索API」でレストランを現在位置から3km以内でキーワード検索する

ぐるなびの「レストラン検索API」で、レストランを現在位置(CoreLocation経由で取得)から3km以内でキーワードを指定して検索するAppleScriptです。

実行のためには、ぐるなびAPIの「新規アカウント発行」より申請を行い、アクセスキーを取得してください(申請無料)。取得したアクセスキーをプログラム中のretAccessKey()ハンドラ中に記述しておいてください。また、現在位置取得のためにWiFiをオンにして実行する必要があります。初回実行時になかなか位置取得機能がイネーブルにならずに焦りました。

ぐるなびでは、「レストラン検索API」「多言語版レストラン検索API」「応援口コミAPI」「エリアマスタ取得API」「都道府県マスタAPI」「エリアLマスタAPI」「エリアMマスタAPI」「エリアSマスタAPI」「大業態マスタ取得API」「小業態マスタ取得API」などのAPIを公開しているため、さまざまなアプリケーション上のデータと組み合わせて有効に利用できると思います。

下世話なところでは、Webサイトから検索すると無駄なデータが多い割に一度に検索できるデータ件数が少ないので、(Webブラウザではなく)スクリプトエディタ上でしぼりこみ検索を行っていたりします。

プログラムの難易度は高くないのですが、ふだんライブラリ化している部品を掲載のために1リスト中に展開しています。内容が素朴な割にプログラムが長くなってしまっていますが、AppleScriptObjCの宿命といったところでしょうか。

AppleScript名:ぐるなびの「レストラン検索API」でレストランを現在位置から3km以内でキーワード検索する
– Created 2016-10-29 by Takaaki Naganoya
– 2016 Piyomaru Software
use AppleScript version “2.5″
use scripting additions
use framework “Foundation”
use framework “CoreLocation”
–use curLocLib : script “curLocationLib”
–http://api.gnavi.co.jp/api/manual/restsearch/

–http://piyocast.com/as/archives/4320

–from curLocationLib
property locationManager : missing value
property curLatitude : 0
property curLongitude : 0
–from curLocationLib

set reqURLStr to “http://api.gnavi.co.jp/RestSearchAPI/20150630/”

–現在地の緯度、経度情報を取得する
set {aLat, aLong} to getCurrentLocation() of me
set aKey to retAccessKey() of me

–指定位置から3km以内の、キーワード”とんかつ”でヒットするレストランをピックアップ
set aRec to {keyid:aKey, |format|:“json”, latitude:aLat as string, longitude:aLong as string, range:“5″, freeword:“とんかつ”}
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 aRESTres to (json of aRes)
–>  {@attributes:{api_version:”20150630″}, total_hit_count:”24″, page_offset:”1″, rest:{{name_kana:”アサノ”, address:”〒179-0074 東京都練馬区春日町6-12-5 “, party:{}, credit_card:{}, url:”http://r.gnavi.co.jp/nhefra550000/?ak=Q%2BN8L78dCT2RgZUYqtECBTRS3%2BEtZ0qY0cmsoQMxWqU%3D”, code:{category_name_l:{”和食”, {@attributes:{order:”1″}}}, areaname:”関東”, areacode:”AREA110″, prefname:”東京都”, prefcode:”PREF13″, areacode_s:”AREAS2225″, areaname_s:”光が丘”, category_code_s:{”RSFST01005″, {@attributes:{order:”1″}}}, category_code_l:{”RSFST01000″, {@attributes:{order:”1″}}}, category_name_s:{”とんかつ(トンカツ)”, {@attributes:{order:”1″}}}}, tel:”03-3999-4099″, parking_lots:{}, lunch:{}, fax:{}, @attributes:{order:”0″}, latitude:”35.747856″, category:”とんかつ”, url_mobile:”http://mobile.gnavi.co.jp/shop/5421186/?ak=Q%2BN8L78dCT2RgZUYqtECBTRS3%2BEtZ0qY0cmsoQMxWqU%3D”, name:”あさの “, holiday:{}, flags:{mobile_site:”1″, mobile_coupon:”0″, pc_coupon:”0″}, id:”5421186″, coupon_url:{mobile:{}, pc:{}}, access:{walk:”徒歩7″, line:”都営大江戸線(放射部)”, station_exit:”A3口”, note:{}, station:”練馬春日町駅”}, tel_sub:{}, longitude:”139.637081″, pr:{pr_long:{}, pr_short:{}}, budget:{}, e_money:{}, opentime:{}, update_date:”2015-10-14 14:14:02″, image_url:{shop_image1:{}, shop_image2:{}, qrcode:”http://r.gnst.jp/tool/qr/?id=5421186&q=6″}}, ….

set aNameRes to (aRESTres’s valueForKeyPath:“rest.name”) as list
–>  {”あさの “, “CoCo壱番屋 西武中村橋駅前通店”, “とんかつ かつ富士 “, “華屋与兵衛 豊島園店”, “華屋与兵衛 春日町店”, “とんかつ和幸 西武練馬駅店 “, “とんかつ和幸 IMA光が丘店 “, “松乃家 練馬店”, “とん陣 “, “よしだ “}

set aAddressRes to (aRESTres’s valueForKeyPath:“rest.address”) as list
–>  {”〒179-0074 東京都練馬区春日町6-12-5 “, “〒176-0023 東京都練馬区中村北3-22-10 友伸ビル1F”, “〒176-0021 東京都練馬区貫井3-14-5 “, “〒179-0085 東京都練馬区早宮4-39″, “〒179-0074 東京都練馬区春日町2-7-18″, “〒176-0001 東京都練馬区練馬1-3-5 西武池袋線練馬駅下 2F”, “〒179-0072 東京都練馬区光が丘5-1-1 光が丘IMA内 3F”, “〒176-0012 東京都練馬区豊玉北5-19-12″, “〒176-0012 東京都練馬区豊玉北5-18-7 “, “〒179-0073 東京都練馬区田柄1-4-24 “}

–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”
  
  
set aRes to current application’s NSURLConnection’s sendSynchronousRequest:aRequest returningResponse:(reference) |error|:(missing value)
  
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 “xxXxXXxxXxxxXxxXXxXXxXXxXxXXxxXX” –ぐるなびAPI アカウントのアクセスキー
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

——————————————————–
— curLocationLib

on getCurrentLocation()
  
  
set locationManager to current application’s CLLocationManager’s alloc()’s init()
  
  
set locE to locationManager’s locationServicesEnabled()
  
if (locE as boolean) = true then
    locationManager’s setDelegate:me
    
locationManager’s setDesiredAccuracy:(current application’s kCLLocationAccuracyNearestTenMeters)
    
locationManager’s setDistanceFilter:500
    
locationManager’s startUpdatingLocation()
  else
    return false –error in init
  end if
  
  
set hitF to false
  
repeat 3000 times
    if {curLatitude, curLongitude} is not equal to {0, 0} then
      set hitF to true
      
exit repeat
    end if
    
delay 0.01
  end repeat
  
  
if hitF = false then return false
  
return {curLatitude, curLongitude}
  
end getCurrentLocation

on locationManager:manager didUpdateLocations:locations
  –Listing 1-3 Processing an incoming location event
  
set location to (locations’s lastObject())
  
set eventDate to (location’s timestamp())
  
set howRecent to (eventDate’s timeIntervalSinceNow())
  
  
–Calc ABS
  
set howRecent to howRecent as real
  
set howRecent to absNum(howRecent)
  
  
if howRecent < 15.0 then
    set alt to location’s altitude
    
set aSpeed to location’s speed
    
set aCourse to location’s course –North:0, East:90, South:180, West:270
    
set theDescription to location’s |description|()
    
set anNSScanner to current application’s NSScanner’s scannerWithString:theDescription
    
anNSScanner’s setCharactersToBeSkipped:(current application’s NSCharacterSet’s characterSetWithCharactersInString:“< ,")
    
set {theResult, aLat} to anNSScanner’s scanDouble:(reference)
    
set {theResult, aLng} to anNSScanner’s scanDouble:(reference)
    
    
copy {aLat, aLng} to {my curLatitude, my curLongitude}
  else
    locationManager’s stopUpdatingLocation()
  end if
end locationManager:didUpdateLocations:

on locationManager:anCLLocationManager didFailWithError:anNSError
  display dialog (anNSError’s localizedDescription() as text)
end locationManager:didFailWithError:

on absNum(aNum)
  if aNum > 0 then
    return aNum
  else
    return (aNum * -1)
  end if
end absNum

★Click Here to Open This Script 

2016/11/06 XMLをrecordにv2

XMLをrecordに変換するAppleScriptです。

以前にAppleScript-Users ML上で流れていたXML→record変換のAppleScriptでしたが、動作確認を行ってもうまく動かず、そのまま放置状態になっていました。

見直してみたところ、「NSMutableDictionary’s dictionary()」というカラのmutable dictionaryを作成する部分が、うまくAppleScriptの処理系に認識されていなかったようでした。少し書き直してみました。

AppleScript名:XMLをrecordにv2
–2015 Shane Stanley & Alex Zavatone
– Modified 2016-11-06 by Takaaki Naganoya
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”
–http://piyocast.com/as/archives/4306

property dictStack : missing value – stack to hold array of dictionaries
property textInProgress : “” – string to collect text as it is found
property anError : missing value – if we get an error, store it here

set xmlString to “< ?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>

Saga
Nor
én
Malm
ö
Martin
Rohde
K
øbenhavn

set xmlRes to my makeRecordWithXML:xmlString
–> {|character|:{firstName:{|contents|:”Saga”}, lastName:{|contents|:”Norén”}, city:{|contents|:”Malmö“}, partner:{firstName:{|contents|:”Martin”}, lastName:{|contents|:”Rohde”}, city:{|contents|:”København”}, attributes:{approach:”dogged”}}}}

on makeRecordWithXML:xmlString
  – set up properties
  
set my dictStack to current application’s NSMutableArray’s array() – empty mutable array
  
set anEmpty to current application’s NSMutableDictionary’s |dictionary|()
  (
my dictStack)’s addObject:anEmpty – add empty mutable dictionary
  
set my textInProgress to current application’s NSMutableString’s |string|() – empty mutable string
  
  
– convert XML from string to data
  
set anNSString to current application’s NSString’s stringWithString:xmlString
  
set theData to anNSString’s dataUsingEncoding:(current application’s NSUTF8StringEncoding)
  
  
– initialize an XML parser with the data
  
set theNSXMLParser to current application’s NSXMLParser’s alloc()’s initWithData:theData
  
  
– set this script to be the parser’s delegate
  
theNSXMLParser’s setDelegate:me
  
  
– tell it to parse the XML
  
set theResult to theNSXMLParser’s parse()
  
if theResult then – went OK, get first item on stack
    return ((my dictStack)’s firstObject()) as record
  else – error, so return error
    error (my anError’s localizedDescription() as text)
  end if
end makeRecordWithXML:

– this is an XML parser delegate method. Called when new element found
on parser:anNSXMLParser didStartElement:elementName namespaceURI:aString qualifiedName:qName attributes:aRecord
  – store reference to last item on the stack
  
set parentDict to my dictStack’s lastObject()
  
  
– make new child
  
set childDict to current application’s NSMutableDictionary’s |dictionary|()
  
  
– if there are attributes, add them as a record with key “attributes”
  
if aRecord’s |count|() > 0 then
    childDict’s setValue:aRecord forKey:“attributes”
  end if
  
  
– see if there’s already an item for this key
  
set existingValue to parentDict’s objectForKey:elementName
  
  
if existingValue is not missing value then
    – there is, so if it’s an array, store it…
    
if (existingValue’s isKindOfClass:(current application’s NSMutableArray)) as boolean then
      set theArray to existingValue
    else
      – otherwise create an array and add it
      
set theArray to current application’s NSMutableArray’s arrayWithObject:existingValue
      
parentDict’s setObject:theArray forKey:elementName
    end if
    
    
– then add the new dictionary to the array
    
theArray’s addObject:childDict
  else
    – add new dictionary directly to the parent
    
parentDict’s setObject:childDict forKey:elementName
  end if
  
  
– also add the new dictionary to the end of the stack
  (
my dictStack)’s addObject:childDict
end parser:didStartElement:namespaceURI:qualifiedName:attributes:

– this is an XML parser delegate method. Called at the end of an element
on parser:anNSXMLParser didEndElement:elementName namespaceURI:aString qualifiedName:qName
  – if any text has been stored, add it as a record with key “contents”
  
if my textInProgress’s |length|() > 0 then
    set dictInProgress to my dictStack’s lastObject()
    
dictInProgress’s setObject:textInProgress forKey:“contents”
    
    
– reset textInProgress property for next element
    
set my textInProgress to current application’s NSMutableString’s |string|()
  end if
  
  
– remove last item from the stack
  
my dictStack’s removeLastObject()
end parser:didEndElement:namespaceURI:qualifiedName:

– this is an XML parser delegate method. Called when string is found. May be called repeatedly
on parser:anNSXMLParser foundCharacters:aString
  – only append string if it’s not solely made of space characters (which should be, but aren’t, caught by another delegate method)
  
if (aString’s stringByTrimmingCharactersInSet:(current application’s NSCharacterSet’s whitespaceAndNewlineCharacterSet()))’s |length|() > 0 then
    (my textInProgress)’s appendString:aString
  end if
end parser:foundCharacters:

– this is an XML parser delegate method. Called when there’s an error
on parser:anNSXMLParser parseErrorOccurred:anNSError
  set my anError to anNSError
end parser:parseErrorOccurred:

★Click Here to Open This Script 

2016/10/10 数値に3桁セパレータを付加、外して数値に戻す

数値に3桁セパレーター(カンマ)を付加した文字列にするAppleScriptと、そのセパレーター入り文字列を数字に戻すAppleScriptです。

AppleScript名:数値に3桁セパレータを付加、外して数値に戻す
– Created 2016-10-09 by Takaaki Naganoya
– 2016 Piyomaru Software
use AppleScript version "2.5"
use scripting additions
use framework "Foundation"
–http://piyocast.com/as/archives/4253

set aNum to 100000
set aStr to formatNum(aNum) of me
–> "100,000"

set bNum to deFromatNumStr(aStr) of me
–>  100000

on formatNum(theNumber as number)
  set theResult to current application’s NSNumberFormatter’s localizedStringFromNumber:theNumber numberStyle:(current application’s NSNumberFormatterDecimalStyle)
  
return theResult as text
end formatNum

on deFromatNumStr(theNumericString as string)
  set notWantChars to current application’s NSCharacterSet’s characterSetWithCharactersInString:","
  
set targStr to current application’s NSString’s stringWithString:theNumericString
  
set newStr to (targStr’s componentsSeparatedByCharactersInSet:notWantChars)’s componentsJoinedByString:""
  
return ((newStr as string) as number) –Danger in OS X 10.10 (floating point casting bug)
end deFromatNumStr

★Click Here to Open This Script 

2016/02/01 指定の名称のフォントの見本を表示

指定のPostScript名称のフォントの見本をウィンドウ表示するAppleScriptです。

Script Editor上ではControl+Command+Rで実行する必要があります。

NSFont経由で指定フォントが含む文字セットを取得し、どの文字が含まれているのかをチェックしたい、という目的のために作成したものです。ウィンドウを作成して文字表示しているのは、あくまで「おまけ」です。

font1.png

NSFontにcoveredCharacterSet()で指定フォントが持っている文字セットを取得。この文字セットの中に含まれている文字を、1〜65535の数値で(文字を作ってチェックしたらダメで、数値)存在チェックしています。ここだけ、ループでいちいち調べているので時間がかかります(MacBook Pro Retina 2012で4秒ぐらい)。ここは、もうちょっとなんとかならないかと思っています。

また、OS標準のFont Book.appで調べたグリフ数とNSFont→NSCharacterSet経由で存在確認して得られるCharacter数は異なっているため、自分としてはちょっと肩透かしをくらいました。Font Book.appだと各フォントが含んでいる文字といった細かい情報が取得できないため、NSFont経由で調査してみたのですが・・・フォント内の各文字(コード)が実際にグリフを持っているかどうかは、文字セット中の文字を実際にレンダリングしてみないとダメそうな気配がしています(文字コードだけ宣言しておいて何も割り当てグリフがないというケースとか、同じ文字コードに複数のグリフが割り当てられているケースなど(「高」と「癲廖覆呂靴瓦世)など)、深入りするとトンでもないものがありそう)。

AppleScript中で指定している「TheLittleBirdFONT」は、手書き風日本語文字フォントの傑作「ことり文字ふぉんと」(自分が使っているのは大昔に無償配布されていたバージョン)です。OS標準でインストールされているわけではないため、動作チェックには他のものを指定したほうがよいでしょう。

font2.png

ことり文字ふぉんとの独特の特殊記号がどこに入っているかを忘れてしまったため、とりあえず文字セット中の文字をすべてことり文字ふぉんとで表示して「簡易フォント見本帖」のように使ってみたりもしました。

AppleScript名:ASOCでテキストビュー+ボタンを作成(フォント指定)
– Created 2016-02-01 by Takaaki Naganoya
– 2016 Piyomaru Software
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”
use framework “AppKit”
use framework “Carbon” – AEInteractWithUser() is in Carbon

property windisp : false

set aFontName to “TheLittleBirdFONT”
set aWidth to 450
set aHeight to 600

if current application’s AEInteractWithUser(-1, missing value, missing value) is not equal to 0 then return

set aTitle to “テキストビューのじっけん/TextView Test” –Window Title
set aButtonMSG to “OK” –Button Title

–表示用テキストの作成
set aRes to checkExistenceOfFont(aFontName) of me
if aRes = false then
  display dialog “There is no < " & aFontName & “> font. Designate another one.” –No font
  
return
end if
set bRes to retDefinedCharactersInFont(aFontName) of me
set dispStr to listToStringUsingTextItemDelimiter(bRes, “, “) of me

dispTextView(aWidth, aHeight, aTitle, dispStr, aButtonMSG, 180, aFontName, 36) of me

on dispTextView(aWidth as integer, aHeight as integer, aTitle as text, dispStr, aButtonMSG as text, timeOutSecs as number, fontID, fontSize)
  
  
set aColor to current application’s NSColor’s colorWithDeviceRed:0.0 green:0.0 blue:0.0 alpha:1.0
  
set (my windisp) to true
  
  
–Text View+Scroll Viewをつくる
  
set aScroll to current application’s NSScrollView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, aWidth, aHeight))
  
set aView to current application’s NSTextView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, aWidth, aHeight))
  
aView’s setRichText:true
  
aView’s useAllLigatures:true
  
aView’s setTextColor:(current application’s NSColor’s yellowColor()) –cyanColor
  
aView’s setFont:(current application’s NSFont’s fontWithName:fontID |size|:fontSize) –ヒラギノ明朝Pro W3
  
aView’s setBackgroundColor:aColor
  
aScroll’s setDocumentView:aView
  
aView’s enclosingScrollView()’s setHasVerticalScroller:true
  
  
–Buttonをつくる
  
set bButton to (current application’s NSButton’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, aWidth, 40)))
  
bButton’s setTitle:aButtonMSG
  
bButton’s setTarget:me
  
bButton’s setAction:(“clicked:”)
  
  
–SplitViewをつくる
  
set aSplitV to current application’s NSSplitView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, aHeight, aWidth))
  
aSplitV’s setVertical:false
  
  
aSplitV’s addSubview:aScroll
  
aSplitV’s addSubview:bButton
  
aSplitV’s setNeedsDisplay:true
  
  
–WindowとWindow Controllerをつくる
  
set aWin to makeWinWithView(aSplitV, aWidth, aHeight, aTitle, 0.9)
  
aWin’s makeKeyAndOrderFront:(missing value)
  
set wController to current application’s NSWindowController’s alloc()
  
wController’s initWithWindow:aWin
  
aWin’s makeFirstResponder:aView
  
aView’s setString:dispStr
  
wController’s showWindow:me
  
  
set aCount to timeOutSecs * 10 –timeout seconds * 10
  
repeat aCount times
    if (my windisp) = false then
      exit repeat
    end if
    
delay 0.1
    
set aCount to aCount - 1
  end repeat
  
  
my closeWin:aWin
  
end dispTextView

–Button Clicked Event Handler
on clicked:aSender
  set (my windisp) to false
end clicked:

–make Window for Input
on makeWinWithView(aView, aWinWidth, aWinHeight, aTitle, alphaV)
  set aScreen to current application’s NSScreen’s mainScreen()
  
set aFrame to {{0, 0}, {aWinWidth, aWinHeight}}
  
set aBacking to current application’s NSTitledWindowMask –NSBorderlessWindowMask
  
set aDefer to current application’s NSBackingStoreBuffered
  
  
– Window
  
set aWin to current application’s NSWindow’s alloc()
  (
aWin’s initWithContentRect:aFrame styleMask:aBacking backing:aDefer defer:false screen:aScreen)
  
–aWin’s setBackgroundColor:(current application’s NSColor’s whiteColor())
  
  
aWin’s setTitle:aTitle
  
aWin’s setDelegate:me
  
aWin’s setDisplaysWhenScreenProfileChanges:true
  
aWin’s setHasShadow:true
  
aWin’s setIgnoresMouseEvents:false
  
aWin’s setLevel:(current application’s NSNormalWindowLevel)
  
aWin’s setOpaque:false
  
aWin’s setAlphaValue:alphaV –append
  
aWin’s setReleasedWhenClosed:true
  
aWin’s |center|()
  
–aWin’s makeKeyAndOrderFront:(me)
  
  
– Set Custom View
  
aWin’s setContentView:aView
  
  
return aWin
  
end makeWinWithView

–close win
on closeWin:aWindow
  repeat with n from 10 to 1 by -1
    (aWindow’s setAlphaValue:n / 10)
    
delay 0.02
  end repeat
  
aWindow’s |close|()
end closeWin:

–指定PostScript名称のフォントがコンピューター上に存在するかどうかチェック
on checkExistenceOfFont(fontName as string)
  if fontName = “” then return false
  
set aFont to current application’s NSFont’s fontWithName:fontName |size|:9.0
  
if aFont = missing value then
    return false
  else
    return true
  end if
end checkExistenceOfFont

–指定Postscript名称のフォントに定義されている文字数を数えて返す
on countDefinedCharactersInFont(fontName as string)
  
  
script spdF
    property aList : {}
  end script
  
  
set aFont to current application’s NSFont’s fontWithName:fontName |size|:9.0
  
if aFont = missing value then return false
  
  
set aSet to aFont’s coveredCharacterSet()
  
  
set aList of spdF to {}
  
  
repeat with i from 1 to 65535
    set aRes to (aSet’s characterIsMember:i) as boolean
    
if aRes = true then
      set the end of aList of spdF to (string id i)
    end if
  end repeat
  
  
return length of (aList of spdF)
  
end countDefinedCharactersInFont

–指定Postscript名称のフォントに定義されている文字を返す
on retDefinedCharactersInFont(fontName as string)
  
  
script spdG
    property aList : {}
  end script
  
  
set aFont to current application’s NSFont’s fontWithName:fontName |size|:24.0
  
set aSet to aFont’s coveredCharacterSet()
  
  
set aList of spdG to {}
  
  
repeat with i from 1 to 65535
    set aRes to (aSet’s characterIsMember:i) as boolean
    
if aRes = true then
      set the end of aList of spdG to (string id i)
    end if
  end repeat
  
  
return (aList of spdG)
  
end retDefinedCharactersInFont

on listToStringUsingTextItemDelimiter(sourceList, textItemDelimiter)
  set the CocoaArray to current application’s NSArray’s arrayWithArray:sourceList
  
set the CocoaString to CocoaArray’s componentsJoinedByString:textItemDelimiter
  
return (CocoaString as string)
end listToStringUsingTextItemDelimiter

★Click Here to Open This Script 

2015/12/06 現在地点の緯度経度情報を取得する v2

Cocoaの機能を用いて、Macの現在位置の緯度経度情報を取得するAppleScriptです。実行時にWiFiがオンになっている必要があります。

だんだん、ASOCのScriptも書きこなれてきて、「この場合にはこう書く」的な方法論が蓄積され、便利に書けるようになってきました。処理系の方もCocoaを用いた記述に最適化されてきているのでしょうか(OS X 10.10ではForegroundで実行することを要求されましたが、OS X 10.11ではとくに文句も言われないので、、、)。

AppleScript名:GetCurrentLocation_ElCapitan v4
– Created 2015-03-04 by Takaaki Naganoya, Shane Stanley
– Modified 2015-12-06 by Takaaki Naganoya
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”
use framework “CoreLocation”

property locationManager : missing value
property curLatitude : 0
property curLongitude : 0

set {aLat, aLong} to getCurrentGeoLocation() of me

on getCurrentGeoLocation()
  set locationManager to current application’s CLLocationManager’s alloc()’s init()
  
  
set locE to locationManager’s locationServicesEnabled()
  
if (locE as boolean) = true then
    locationManager’s setDelegate:me
    
locationManager’s setDesiredAccuracy:(current application’s kCLLocationAccuracyNearestTenMeters)
    
locationManager’s setDistanceFilter:500
    
locationManager’s startUpdatingLocation()
  else
    return false –error in init
  end if
  
  
set hitF to false
  
repeat 3000 times
    if {curLatitude, curLongitude} is not equal to {0, 0} then
      set hitF to true
      
exit repeat
    end if
    
delay 0.01
  end repeat
  
  
if hitF = false then return false
  
return {curLatitude, curLongitude}
end getCurrentGeoLocation

on locationManager:manager didUpdateLocations:locations
  set location to (locations’s lastObject())
  
set eventDate to (location’s timestamp())
  
set howRecent to (eventDate’s timeIntervalSinceNow())
  
set howRecent to howRecent as real
  
set howRecent to absNum(howRecent)
  
  
if howRecent < 15.0 then
    set alt to location’s altitude –>(NSNumber) 46.356517791748
    
set aSpeed to location’s speed –>(NSNumber) -1.0
    
set aCourse to location’s course –North:0, East:90, South:180, West:270
    
–>  (NSNumber) -1.0
    
set theDescription to location’s |description|()
    
–> (NSString) “< +35.xxxxx,+139.xxxxxx> +/- 65.00m (speed -1.00 mps / course -1.00) @ 2015/03/04 8時56分41秒 日本標準時”
    
set anNSScanner to current application’s NSScanner’s scannerWithString:theDescription
    
anNSScanner’s setCharactersToBeSkipped:(current application’s NSCharacterSet’s characterSetWithCharactersInString:“< ,")
    
set {theResult, aLat} to anNSScanner’s scanDouble:(reference)
    
set {theResult, aLng} to anNSScanner’s scanDouble:(reference)
    
copy {aLat, aLng} to {my curLatitude, my curLongitude}
  else
    locationManager’s stopUpdatingLocation()
  end if
  
end locationManager:didUpdateLocations:

on locationManager:anCLLocationManager didFailWithError:anNSError
  display dialog (anNSError’s localizedDescription() as text)
end locationManager:didFailWithError:

on absNum(aNum)
  if aNum > 0 then
    return aNum
  else
    return (aNum * -1)
  end if
end absNum

★Click Here to Open This Script 

2015/12/04 AlphabetとNumericの混在かどうかを調べる

Cocoaの機能を用いて、アルファベットと数字の混在状態になっているかどうかを調べるAppleScriptです。

AppleScript名:AlphabetとNumericの混在かどうかを調べる
– Created 2015-12-04 by Takaaki Naganoya
– 2015 Piyomaru Software
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”

set aRes to chkMixtureOfNumericAndAlphabet(“ABC”) of me
–>  false

set aRes to chkMixtureOfNumericAndAlphabet(“123″) of me
–>  false

set aRes to chkMixtureOfNumericAndAlphabet(“4f73vg1v”) of me –Target
–>  true

set aRes to chkMixtureOfNumericAndAlphabet(“4f73vg1vあああ”) of me
–>  false

–数字とアルファベットの混在状態の時にtrueを返す
on chkMixtureOfNumericAndAlphabet(checkString)
  set a0Res to chkAlphabetAndNumeric(checkString) of me
  
set a1Res to chkNumeric(checkString) of me
  
set a2Res to chkAlphabet(checkString) of me
  
  
if {a0Res, a1Res, a2Res} = {true, false, false} then
    return true
  else
    return false
  end if
  
end chkMixtureOfNumericAndAlphabet

–数字のみかを調べて返す
on chkNumeric(checkString)
  set digitCharSet to current application’s NSCharacterSet’s characterSetWithCharactersInString:“0123456789″
  
set ret to my chkCompareString:checkString baseString:digitCharSet
  
return ret as boolean
end chkNumeric

– アルファベットのみか調べて返す
on chkAlphabet(checkString)
  set aStr to current application’s NSString’s stringWithString:checkString
  
set allCharSet to current application’s NSMutableCharacterSet’s alloc()’s init()
  
allCharSet’s addCharactersInRange:(current application’s NSMakeRange(ASCII number of “a”, 26))
  
allCharSet’s addCharactersInRange:(current application’s NSMakeRange(ASCII number of “A”, 26))
  
set aBool to my chkCompareString:aStr baseString:allCharSet
  
return aBool as boolean
end chkAlphabet

– アルファベットと数字のみか調べて返す
on chkAlphabetAndNumeric(checkString)
  set aStr to current application’s NSString’s stringWithString:checkString
  
set allCharSet to current application’s NSMutableCharacterSet’s alloc()’s init()
  
allCharSet’s addCharactersInRange:(current application’s NSMakeRange(ASCII number of “0″, 10))
  
allCharSet’s addCharactersInRange:(current application’s NSMakeRange(ASCII number of “a”, 26))
  
allCharSet’s addCharactersInRange:(current application’s NSMakeRange(ASCII number of “A”, 26))
  
set aBool to my chkCompareString:aStr baseString:allCharSet
  
return aBool as boolean
end chkAlphabetAndNumeric

on chkCompareString:checkString baseString:baseString
  set aScanner to current application’s NSScanner’s localizedScannerWithString:checkString
  
aScanner’s setCharactersToBeSkipped:(missing value)
  
aScanner’s scanCharactersFromSet:baseString intoString:(missing value)
  
return (aScanner’s isAtEnd()) as boolean
end chkCompareString:baseString:

★Click Here to Open This Script 

2015/09/14 ASOCで文字種別を判定する v2

Cocoaの機能を用いて、文字種別を判定するAppleScriptです。

記号(Symbol)のチェックと、製品コードなどのルールを供給するとそのとおりの文字の並びになっているかをチェックするルーチンを加えました。

これまでに作ってきた同種のルーチンは以下のようになります。

コードのチェック
簡易マクロ展開つきのコードチェック v1

わざわざ文字を分解するよりも、ルールから正規表現の文字列を組み立てて、そのとおりになっているかのチェックを行うほうがよいかも・・・とは考えたのですが、どの桁にエラーがあるかを確認するためにはこのような仕様のほうがよいだろうか、と思うものです。

AppleScript名:ASOCで文字種別を判定する v2
– Created 2015-09-14 by Takaaki Naganoya
– 2015 Piyomaru Software
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”

–http://core-tech.jp/gijutsublog/2014/06/23/10505

set aRes to my chkStrings:“0123-45-678A” asRule:“9999-99-999X”
–>  {​​​​​true, ​​​​​true, ​​​​​true, ​​​​​true, ​​​​​true, ​​​​​true, ​​​​​true, ​​​​​true, ​​​​​true, ​​​​​true, ​​​​​true, ​​​​​true​​​}
set bRes to my chkStrings:“0123-45-d2aA” asRule:“9999-99-999X”
–>  {​​​​​true, ​​​​​true, ​​​​​true, ​​​​​true, ​​​​​true, ​​​​​true, ​​​​​true, ​​​​​true, ​​​​​false, ​​​​​true, ​​​​​false, ​​​​​true​​​}

–指定したルールのとおりの文字種の並びになっているか?
on chkStrings:aStr asRule:aRuleStr
  set aList to characters of aStr
  
set rList to characters of aRuleStr
  
if (length of aList is not equal to length of rList) then return false
  
  
set chkList to {}
  
repeat with i from 1 to (length of aList)
    set j1 to contents of item i of aList
    
set j2 to contents of item i of rList
    
    
if j2 = “9″ then
      set j3 to (my chkNumeric:j1)
    else if j2 = “X” then
      set j3 to (my chkAlphabet:j1)
    else if j2 is in {“$”, “\”", “!”, “~”, “&”, “=”, “#”, “[”, “]”, “.”, “_”, “-”, “+”, “`”, “|”, “{”, “}”, “?”, “%”, “^”, “*”, “/”, “’”, “@”, “-”, “/”, “:”, “;”, “(”, “)”, “,”} then
      set j3 to (my chkSymbol:j1)
    end if
    
set the end of chkList to j3
  end repeat
  
return chkList
end chkStrings:asRule:

– アルファベットのみか
on chkAlphabet:checkString
  set aStr to current application’s NSString’s stringWithString:checkString
  
set allCharSet to current application’s NSMutableCharacterSet’s alloc()’s init()
  
allCharSet’s addCharactersInRange:(current application’s NSMakeRange(ASCII number of “a”, 26))
  
allCharSet’s addCharactersInRange:(current application’s NSMakeRange(ASCII number of “A”, 26))
  
set aBool to my chkCompareString:aStr baseString:allCharSet
  
return aBool as boolean
end chkAlphabet:

–数字のみか
on chkNumeric:checkString
  set digitCharSet to current application’s NSCharacterSet’s characterSetWithCharactersInString:“0123456789″
  
set ret to my chkCompareString:checkString baseString:digitCharSet
  
return ret as boolean
end chkNumeric:

–アルファベットと数字のみか
on chkAlphaNumeric:checkString
  set alnumCharSet to current application’s NSCharacterSet’s alphanumericCharacterSet()
  
set ret to my chkCompareString:checkString baseString:alnumCharSet
  
return ret as boolean
end chkAlphaNumeric:

–アルファベットと数字と記号のみか
on chkAlphaNumericSymbol:checkString
  set muCharSet to current application’s NSCharacterSet’s alphanumericCharacterSet()’s mutableCopy()
  
muCharSet’s addCharactersInString:“$\”!~&=#[]._-+`|{}?%^*/’@-/:;(),”
  
set ret to my chkCompareString:checkString baseString:muCharSet
  
return ret as boolean
end chkAlphaNumericSymbol:

–記号のみか
on chkSymbol:checkString
  set muCharSet to current application’s NSCharacterSet’s alloc()’s init()
  
muCharSet’s addCharactersInString:“$\”!~&=#[]._-+`|{}?%^*/’@-/:;(),”
  
set ret to my chkCompareString:checkString baseString:muCharSet
  
return ret as boolean
end chkSymbol:

–全角文字が存在するか
on chkMultiByteChar:checkString
  set aStr to current application’s NSString’s stringWithString:checkString
  
set aRes to aStr’s canBeConvertedToEncoding:(current application’s NSASCIIStringEncoding)
  
return (aRes as boolean)
end chkMultiByteChar:

on chkCompareString:checkString baseString:baseString
  set aScanner to current application’s NSScanner’s localizedScannerWithString:checkString
  
aScanner’s setCharactersToBeSkipped:(missing value)
  
aScanner’s scanCharactersFromSet:baseString intoString:(missing value)
  
return (aScanner’s isAtEnd()) as boolean
end chkCompareString:baseString:

★Click Here to Open This Script 

2015/09/13 文字種別を判定する

Cocoaの機能を用いて文字種別の判定を行うAppleScriptです。

Objective-Cのものをそのまま書き換えたレベルですが、Pure AppleScriptでやるやり方(そもそも仕組みがないから、自分で文字コードを調べてひたすらループして判定)とはずいぶん違うことがわかります。

AppleScript名:ASOCで文字種別を判定する
– Created 2015-09-12 by Takaaki Naganoya
– 2015 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

–http://core-tech.jp/gijutsublog/2014/06/23/10505

set aRes to my chkAlphabet:"p"
–>  true
set bRes to my chkAlphabet:"あ"
–>  false
set cRes to my chkMultiByteChar:"a"
–>  true
set dRes to my chkMultiByteChar:"ぴ"
–>  false
set eRes to my chkAlphaNumeric:"01A"
–>  true
set fRes to my chkAlphaNumeric:"%"
–>  false
set gRes to my chkAlphaNumericSymbol:"AAA`*+}{"
–>  true
set hRes to my chkNumeric:"0123"
–> true
set hRes to my chkNumeric:"0123A"
–> false

– アルファベットのみか
on chkAlphabet:checkString
  set aStr to current application’s NSString’s stringWithString:checkString
  
set allCharSet to current application’s NSMutableCharacterSet’s alloc()’s init()
  
allCharSet’s addCharactersInRange:(current application’s NSMakeRange(ASCII number of "a", 26))
  
allCharSet’s addCharactersInRange:(current application’s NSMakeRange(ASCII number of "A", 26))
  
set aBool to my chkCompareString:aStr baseString:allCharSet
  
return aBool as boolean
end chkAlphabet:

–数字のみか
on chkNumeric:checkString
  set digitCharSet to current application’s NSCharacterSet’s characterSetWithCharactersInString:"0123456789"
  
set ret to my chkCompareString:checkString baseString:digitCharSet
  
return ret as boolean
end chkNumeric:

–アルファベットと数字のみか
on chkAlphaNumeric:checkString
  set alnumCharSet to current application’s NSCharacterSet’s alphanumericCharacterSet()
  
set ret to my chkCompareString:checkString baseString:alnumCharSet
  
return ret as boolean
end chkAlphaNumeric:

–アルファベットと数字と記号のみか
on chkAlphaNumericSymbol:checkString
  set muCharSet to current application’s NSCharacterSet’s alphanumericCharacterSet()’s mutableCopy()
  
muCharSet’s addCharactersInString:"$\"!~&=#[]._-+`|{}?%^*/’@-/:;(),"
  
set ret to my chkCompareString:checkString baseString:muCharSet
  
return ret as boolean
end chkAlphaNumericSymbol:

–全角文字が存在するか
on chkMultiByteChar:checkString
  set aStr to current application’s NSString’s stringWithString:checkString
  
set aRes to aStr’s canBeConvertedToEncoding:(current application’s NSASCIIStringEncoding)
  
return (aRes as boolean)
end chkMultiByteChar:

on chkCompareString:checkString baseString:baseString
  set aScanner to current application’s NSScanner’s localizedScannerWithString:checkString
  
aScanner’s setCharactersToBeSkipped:(missing value)
  
aScanner’s scanCharactersFromSet:baseString intoString:(missing value)
  
return (aScanner’s isAtEnd()) as boolean
end chkCompareString:baseString:

★Click Here to Open This Script 

2015/03/12 CSVファイルをListにParseする(ASOC) 2

Shane Stanley rewrote me a true contents of his ASObjCExtras.framework’s “arrayFromCSV: commaIs:” method. Original version is written in Objective-C. He translated it into AppleScriptObjC to be understood for scripters.

There was no comment in his ASOC program and no blank line. So, I added comments and blanks to make it easy to read.

The script is very appropriate and tidy. To parse CSV file considering double-quote escape and irregular comma, there is need to use some flags to detect the state. The script have to scan CSV file by a character.

—-Japanese

ShaneがASObjCExtras.frameworkを用いてシンプルに書いたCSV parser scriptの「中身」(arrayFromCSV: commanIs:メソッド)をObjective-CからAppleScriptObjCに書き直して送ってくれました(理解しやすいように)。

ShaneのScriptにはコメントも空行も何もなかったので、解析ついでにいろいろコメントを足したり読みやすいように空行を追加しました。

Scriptの内容はきわめてまっとうで丁寧です。CSVをparseするには、ダブルクォート文字とかイレギュラーに行末に入るカンマがあったりとか、そうしたものを処理するためにいくつかのフラグを用いて根気よく処理する必要があります。ScriptはCSVを先頭から1文字づつ調べていく必要があります(もうちょっとは楽をしているかも)。

AppleScript名:CSVのParse 5(ASOC)
–Created By Shane Stanley 2015/03/12
–Commented & Arranged By Takaaki Naganoya 2015/03/12

use scripting additions
use framework “Foundation”

set theString to “cust1,\”prod,1\”,season 1,
cust1,prod1,season2,
cust2,prod1,event1,season1
cust2,prod3,event1,season 1″

its makeListsFromCSV:theString commaIs:“,”
–>  {​​​​​{​​​​​​​”cust1″, ​​​​​​​”prod,1″, ​​​​​​​”season 1″​​​​​}, ​​​​​{​​​​​​​”cust1″, ​​​​​​​”prod1″, ​​​​​​​”season2″​​​​​}, ​​​​​{​​​​​​​”cust2″, ​​​​​​​”prod1″, ​​​​​​​”event1″, ​​​​​​​”season1″​​​​​}, ​​​​​{​​​​​​​”cust2″, ​​​​​​​”prod3″, ​​​​​​​”event1″, ​​​​​​​”season 1″​​​​​}​​​}

–CSV Parser ASOC ver (Translated from “ASObjCExtras.framework” Objective-C version)
on makeListsFromCSV:theString commaIs:theComma
  
  
set theRows to {} –最終的に出力するデータ(2D Listになる)
  
  
set newLineCharSet to current application’s NSCharacterSet’s newlineCharacterSet() –改行キャラクタ
  
set importantCharSet to current application’s NSMutableCharacterSet’s characterSetWithCharactersInString:(“\”" & theComma) –カンマ
  
  
importantCharSet’s formUnionWithCharacterSet:newLineCharSet
  
  
set theNSScanner to current application’s NSScanner’s scannerWithString:theString
  
theNSScanner’s setCharactersToBeSkipped:(missing value)
  
  
  
–データ末尾を検出するまでループ
  
repeat while (theNSScanner’s isAtEnd() as integer = 0)
    
    
set insideQuotes to false
    
set finishedRow to false
    
set theColumns to {}
    
set currentColumn to “”
    
    
–すべての行を処理終了するまでループ(行内部の処理)
    
repeat while (not finishedRow)
      
      
set {theResult, tempString} to theNSScanner’s scanUpToCharactersFromSet:importantCharSet intoString:(reference)
      
–log {”theResult”, theResult, “tempString”, tempString}
      
      
if theResult as integer = 1 then set currentColumn to currentColumn & (tempString as text)
      
–log {”currentColumn”, currentColumn}
      
      
–データ末尾検出
      
if theNSScanner’s isAtEnd() as integer = 1 then
        if currentColumn is not “” then set end of theColumns to currentColumn
        
set finishedRow to true
        
      else
        –データ末尾ではない場合
        
set {theResult, tempString} to theNSScanner’s scanCharactersFromSet:newLineCharSet intoString:(reference)
        
        
if theResult as integer = 1 then
          
          
if insideQuotes then
            –ダブルクォート文字内の場合
            
set currentColumn to currentColumn & (tempString as text)
          else
            –ダブルクォート内ではない場合
            
if currentColumn is not “” then set end of theColumns to currentColumn
            
set finishedRow to true
          end if
          
        else
          –行末文字が見つからない場合
          
set theResult to theNSScanner’s scanString:“\”" intoString:(missing value)
          
          
if theResult as integer = 1 then
            –ダブルクォート文字が見つかった場合
            
if insideQuotes then
              –ダブルクォート文字内の場合
              
set theResult to theNSScanner’s scanString:“\”" intoString:(missing value)
              
              
if theResult as integer = 1 then
                set currentColumn to currentColumn & “\”"
              else
                set insideQuotes to (not insideQuotes)
              end if
            else
              –ダブルクォート文字内ではない場合
              
set insideQuotes to (not insideQuotes)
            end if
            
          else
            –ダブルクォート文字が見つからなかった場合
            
set theResult to theNSScanner’s scanString:theComma intoString:(missing value) –カンマの検索
            
            
if theResult as integer = 1 then
              if insideQuotes then
                set currentColumn to currentColumn & theComma
              else
                set end of theColumns to currentColumn
                
set currentColumn to “”
                
theNSScanner’s scanCharactersFromSet:(current application’s NSCharacterSet’s whitespaceCharacterSet()) intoString:(missing value)
              end if
            end if
          end if
        end if
      end if
      
    end repeat
    
    
if (count of theColumns) > 0 then set end of theRows to theColumns –行データ(1D List)をtheRowsに追加(2D List)
    
  end repeat
  
  
return theRows
  
end makeListsFromCSV:commaIs:

★Click Here to Open This Script 

2015/03/04 現在地点の緯度経度情報を取得する

This AppleScript get current location of your Mac by using CoreLocation. There is need to enable WiFi to get location using this AppleScript.

You can get altitude, timestamp, speed and course easily. But latitude and longitude were difficult to get.

I asked to Shane how to do this. His answer is “get description”.

You must execute this AppleScript in foreground. Original Script is made for a part of ASOC project on Xcode.

loc1.png
▲システム環境設定の「セキュリティとプライバシー」でScript EditorもしくはASObjC Explorer 4に位置情報の取得を許可しておく必要があります

loc2.png
▲許可しないとエラーが表示されます

本AppleScriptはお使いのMacの現在位置の緯度経度情報を取得するものです。CoreLocationを用いて、高度、タイムスタンプ、スピード、進行方向については容易に取得できました(位置情報を取得するためにWiFiをオンにしておく必要あり)。ただ、肝心の緯度経度情報だけはなかなか取得できず、Shaneに聞いてみたところ・・・「descriptionを取得しろ」とのこと(サンプルつきで)。

本AppleScriptの実行時には、foregroundで明示的に実行させる必要があります。オリジナルのScriptはASOC on Xcodeの一部として書いたもので、位置情報の取得/許可については本Scriptの方がうまくいっていない感じです(ASObjC Explorerがコケたりする)。

AppleScript名:GetCurrentLocation_Yosemite v3
– Created 2015-03-04 by Takaaki Naganoya, Shane Stanley
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”
use framework “ASObjCExtras”
use framework “CoreLocation”
use framework “AppKit”

property curLatitude : 0
property curLongitude : 0

on run
  startStandardUpdates()
end run

on startStandardUpdates()
  –Listing 1-1 Starting the standard location service
  
set locationManager to current application’s CLLocationManager’s alloc()’s init()
  
  
set locE to locationManager’s locationServicesEnabled()
  
if (locE as boolean) = true then
    locationManager’s setDelegate:me
    
locationManager’s setDesiredAccuracy:(current application’s kCLLocationAccuracyNearestTenMeters)
    
locationManager’s setDistanceFilter:500
    
locationManager’s startUpdatingLocation()
  end if
  
end startStandardUpdates

on locationManager:manager didUpdateLocations:locations
  
  
–Listing 1-3 Processing an incoming location event
  
set location to (locations’s lastObject())
  
set eventDate to (location’s timestamp())
  
set howRecent to (eventDate’s timeIntervalSinceNow())
  
  
–Calc ABS
  
set howRecent to howRecent as real
  
set howRecent to absNum(howRecent)
  
  
if howRecent < 15.0 then
    
    
set alt to location’s altitude
    
–>  (NSNumber) 46.356517791748
    
    
set aSpeed to location’s speed
    
–>  (NSNumber) -1.0
    
    
set aCourse to location’s course –North:0, East:90, South:180, West:270
    
–>  (NSNumber) -1.0
    
    
–By Shane Stanley
    
set theDescription to location’s |description|()
    
–> (NSString) “< +35.xxxxx,+139.xxxxxx> +/- 65.00m (speed -1.00 mps / course -1.00) @ 2015/03/04 8時56分41秒 日本標準時”
    
    
set anNSScanner to current application’s NSScanner’s scannerWithString:theDescription
    
anNSScanner’s setCharactersToBeSkipped:(current application’s NSCharacterSet’s characterSetWithCharactersInString:“< ,")
    
set {theResult, aLat} to anNSScanner’s scanDouble:(reference)
    
set {theResult, aLng} to anNSScanner’s scanDouble:(reference)
    
    
copy {aLat, aLng} to {my curLatitude, my curLongitude}
    
  else
    stopUpdatingLocation()
  end if
  
end locationManager:didUpdateLocations:

on locationManager:anCLLocationManager didFailWithError:anNSError
  display dialog (anNSError’s localizedDescription() as text)
end locationManager:didFailWithError:

on absNum(aNum)
  if aNum > 0 then
    return aNum
  else
    return (aNum * -1)
  end if
end absNum

★Click Here to Open This Script 

2014/11/22 Keynote 6.5で各スライドのタイトル、マスタースライド名を取得してデータ化

Keynote 6.5で作成中の書類(開発中のシステムの仕様書)から、各ページタイトルを取得。さらに各スライド(ページ)のマスタースライド名を取得しておいて、目次の文字の大きさに違いをつけようとして、データを作成するAppleScriptの試作品です。

実際に、一般的なアプリケーションであるKeynoteをコントロールしつつ、データ処理をASOCベースのサブルーチンで行わせてみました。

40ページ強あるKeynoteのデータの処理を1秒以下で行えます(MacBook Pro Retina mid 2012)。

また、各スライド(ページ)のマスタースライド名については、出現頻度をカウントし、出現頻度の少ないものが各章のトビラであると仮定して、各タイトルにレベル設定(トビラページは1、通常ページは2)を行っています。

実際に、OmniGraffleではこうした処理(各ページのタイトルを求めて、目次ページを動的に作成してすべての項目に実際のページへのリンクを設定)をすべてAppleScriptで行っていますが(PDFに書き出したときに便利)・・・OmniGraffleで行うよりも、(ユーザーの多い)Keynote上で行えたほうが便利です。

Keynote 6.5もけっこうAppleScript対応機能が向上してきてはいるのですが、たとえばテキストオブジェクトを生成しても・・・左寄せ/センタリング/右寄せを指定できないとか、フォントと文字サイズも設定できないとか(ここはまだ試行錯誤の余地あり)、他のスライド(ページ)へのリンク設定が行えないなど、実践的な処理を記述するためには「いまひとつ」な印象です。

とはいえ、Keynote 6.5のAppleScript用語辞書を見ていると、オブジェクトにScript Labelを(将来的に)設定できるように考えられているなど、楽しみです(グラフ方面は手付かずだったり)。

Keynoteには、早く完全体になってほしいところです(永遠に未完成ということはないと思いたいですが、なかなか進捗しないですね)。

ちなみに、Keynoteへのtellブロック内でcocoaの機能にアクセスするコードを直接書くと、実行時にScript Editorがクラッシュするなどなかなかたいへんでした。ASOCのコードは通常のアプリケーションへのアクセスと分けたほうが得策のようです。

AppleScript名:keynote_test
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”
use framework “ASObjCExtras”

property sPage : 3 –1ページ目が表紙、2ページ目が目次として3ページ目以降を処理対象とする

–最前面のドキュメントを取得する
tell application “Keynote”
  set aDocRef to front document
end tell

set tList to retKeynoteSlideTitleFrom(3, aDocRef) of me
–> {”システム概要”, “ハードウェア構成概要”, “ソフトウェア構成概要”,….}

–ドキュメント中の3ページ以降のslideの各マスタースライドの名称を取得して、各マスタースライドの出現頻度を取得して昇順ソート
set bList to retKeynoteSlideBaseSlideNameFrom(sPage, aDocRef) of me
–> {{”タイトル(中央)”, 8}, {”タイトル(上)”, 32}}–{マスタースライド名, 登場頻度カウント}

set {masterList, freqList} to conv2dListTo1dLists(bList) of me
–> {{”タイトル(中央)”, “タイトル(上)”}, {8, 32}}

set tOutList to {}

tell application “Keynote”
  tell aDocRef
    repeat with i from sPage to (count every slide)
      tell slide i
        –Title
        
set aIndex to (i - sPage + 1)
        
set aTitle to contents of item aIndex of tList
        
        
–Base Slide
        
set aBaseName to name of base slide
        
set aLevel to offseOfList(masterList, aBaseName) of me
        
        
set the end of tOutList to {aTitle, aLevel}
        
      end tell
    end repeat
  end tell
end tell

return tOutList
–> {{”システム概要”, 1}, {”ハードウェア構成概要”, 2}, {”ソフトウェア構成概要”, 2}, {”プログラム概要”, 2}, {”プログラム呼称一覧”, 2}, ….}
–1=マスタースライドが”タイトル(中央)”
–2=マスタースライドが “タイトル(上)”

–List中の指定項目の出現位置を返す(複数ヒットした場合には最初の項目番号)。1はじまりのAS仕様のインデックスを使用
on offseOfList(aList, anItem)
  set aResList to (current application’s SMSFord’s indexesOfItem:anItem inArray:(aList) inverting:false) as list
  
set aRes to (first item of aResList)
  
return (aRes + 1)
end offseOfList

–指定ページ(slide)から末尾までの各ページのTitleを取得して返す
on retKeynoteSlideTitleFrom(startPageNum as integer, aDocRef)
  
  
set tList to {}
  
  
try
    –全ページ(slide)のtitleを一括で取得するのが一番高速
    
tell application “Keynote”
      tell aDocRef
        set sCount to count every slide
        
        
if sCount < startPageNum then error
        
if startPageNum < 1 then return {}
        
        
set tList to object text of default title item of every slide
      end tell
    end tell
  on error
    return {}
  end try
  
  
–取得したタイトルから、前後の改行、前後の空白、および途中に入っている改行を削除する
  
set t2List to {}
  
repeat with i in tList
    set j to contents of i
    
set jj to cleanUpText(j) of me
    
set jj to cleanUpCRLF(jj) of me
    
set the end of t2List to jj
  end repeat
  
  
  
  
–一括取得した内容を適宜加工して返すのがベスト
  
set ttList to contents of items startPageNum thru -1 of t2List
  
  
return ttList
  
end retKeynoteSlideTitleFrom

–文字列の前後の改行と空白文字を除去
on cleanUpText(someText)
  set theString to current application’s NSString’s stringWithString:someText
  
set theString to theString’s stringByReplacingOccurrencesOfString:” +” withString:” “ options:(current application’s NSRegularExpressionSearch) range:{location:0, |length|:length of someText}
  
set theWhiteSet to current application’s NSCharacterSet’s whitespaceAndNewlineCharacterSet()
  
set theString to theString’s stringByTrimmingCharactersInSet:theWhiteSet
  
return theString as text
end cleanUpText

–文字置換
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’s ASify()) as string
  
return cString
end repChar

–指定文字列からCRLFを除去
on cleanUpCRLF(aStr)
  set aString to current application’s NSString’s stringWithString:aStr
  
set bString to aString’s stringByReplacingOccurrencesOfString:(string id 10) withString:“” –remove LF
  
set cString to bString’s stringByReplacingOccurrencesOfString:(string id 13) withString:“” –remove CR
  
set dString to (cString’s ASify()) as string
  
return dString
end cleanUpCRLF

–2D Listを1D Listに変換
on conv2dListTo1dLists(aList as list)
  
  
set newList to {}
  
set aLen to length of first item of aList
  
  
repeat aLen times
    set the end of newList to {}
  end repeat
  
  
  
repeat with i in aList
    repeat with ii from 1 to aLen
      set the end of item ii of newList to (item ii of i)
    end repeat
  end repeat
  
  
return newList
  
end conv2dListTo1dLists

–指定ページ(slide)から末尾までの各ページのbase slide名を取得してユニーク化して返す
on retKeynoteSlideBaseSlideNameFrom(startPageNum, aDocRef)
  
  
set aList to {}
  
  
tell application “Keynote”
    tell aDocRef
      set sCount to count every slide
      
if sCount < startPageNum then error
      
      
repeat with i from startPageNum to sCount
        tell slide i
          set aBase to name of base slide
          
set the end of aList to aBase
        end tell
      end repeat
      
    end tell
  end tell
  
  
  
–結果をユニーク化する
  
set aArray to current application’s NSArray’s arrayWithArray:aList
  
set bArray to aArray’s valueForKeyPath:“@distinctUnionOfObjects.self”
  
  
set bList to bArray’s ASify() as list
  
–> {1, 1.1, 2, 3, 4}
  
  
  
–Base Slide名の出現頻度を調べる
  
set cList to {}
  
  
repeat with i in bList
    set aName to contents of i
    
set aCount to countSpecifiedItem(aList, aName) of me
    
set the end of cList to {aName, aCount}
  end repeat
  
  
  
–出現頻度(aCount)をキーにして昇順ソート
  
set dList to sort2DList(cList, 2, {true}) of me
  
  
return dList
  
end retKeynoteSlideBaseSlideNameFrom

–リスト中の指定項目の出現回数を返す
on countSpecifiedItem(aList, countItem)
  set aRes to (current application’s SMSFord’s indexesOfItem:countItem inArray:aList inverting:false) as list
  
set aCount to length of aRes
  
return aCount
end countSpecifiedItem

–2D Listをソート
on sort2DList(aList as list, sortIndexes as list, sortOrders as list)
  
  
–index値をAS流(アイテムが1はじまり)からCocoa流(アイテムが0はじまり)に変換
  
set newIndex to {}
  
repeat with i in sortIndexes
    set j to contents of i
    
set j to j - 1
    
set the end of newIndex to j
  end repeat
  
  
–Sort TypeのListを作成(あえて外部から指定する内容でもない)
  
set sortTypes to {}
  
repeat (length of sortIndexes) times
    set the end of sortTypes to “compare:”
  end repeat
  
  
–Sort
  
set resList to (current application’s SMSFord’s subarraysIn:(aList) sortedByIndexes:newIndex ascending:sortOrders sortTypes:sortTypes |error|:(missing value)) as list
  
  
return resList
  
end sort2DList

★Click Here to Open This Script