Menu

Skip to content
AppleScriptの穴
  • Home
  • Products
  • Books
  • Docs
  • Events
  • Forum
  • About This Blog
  • License
  • 仕事依頼

AppleScriptの穴

Useful & Practical AppleScript archive. Click '★Click Here to Open This Script' Link to download each AppleScript

カテゴリー: list

RTFを読み込んで書式をDictionary化 v3.0

Posted on 2月 11, 2018 by Takaaki Naganoya

指定のRTF書類の内容を読み取って、書式情報をNSDictionaryに変換するAppleScriptです。

  stringVal:文字列データ
  colorStr:RGBの色データを文字列化したもの
  colorVal:RGBの色データの数値list
  colorDomainName:おおまかな色名称
  fontName:フォント名
  fontSize:フォントサイズ

書式情報で抽出しやすくしてあります。複数要素(例:フォント名+色)を一緒にまとめて格納しておくことで、検索パフォーマンスを稼げるはずです。

AppleScript名:RTFを読み込んで書式をDictionary化 v3.0
— Created 2017-12-10 by Takaaki Naganoya
— Modified 2017-12-11 by Shane Stanley
— Modified 2017-12-11 by Nigel Garvey
— Modified 2017-12-12 by Takaaki Naganoya
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"

property NSData : a reference to current application’s NSData
property NSString : a reference to current application’s NSString
property NSAttributedString : a reference to current application’s NSAttributedString

set fRes to choose file of type {"public.rtf"}

set aFilePath to NSString’s stringWithString:(POSIX path of fRes)
set aData to NSData’s dataWithContentsOfFile:aFilePath options:0 |error|:(missing value)
set theStyledText to NSAttributedString’s alloc()’s initWithData:aData options:(missing value) documentAttributes:(missing value) |error|:(missing value)

set attrRes to getAttributeRunsFromAttrString(theStyledText) of me
(*
–>  {​​​​​{​​​​​​​stringVal:"■■■■■■■", ​​​​​​​colorStr:"0 0 0", ​​​​​​​colorVal:{​​​​​​​​​0, ​​​​​​​​​0, ​​​​​​​​​0​​​​​​​}, ​​​​​​​colorDomainName:"black", ​​​​​​​fontName:"ヒラギノ角ゴ ProN W6", ​​​​​​​fontSize:12.0​​​​​}, ​​​​​{​​​​​​​stringVal:"Xxxxxxx", ​​​​​​​colorStr:"217 11 0", ​​​​​​​colorVal:{​​​​​​​​​217, ​​​​​​​​​11, ​​​​​​​​​0​​​​​​​}, ​​​​​​​colorDomainName:"red", ​​​​​​​fontName:"ヒラギノ角ゴ ProN W6", ​​​​​​​fontSize:12.0​​​​​}, …
*)

on getAttributeRunsFromAttrString(theStyledText)
  script aSpd
    property styleList : {}
  end script
  
  
set (styleList of aSpd) to {} —for output
  
  
set thePureString to theStyledText’s |string|() –pure string from theStyledText
  
  
set theLength to theStyledText’s |length|()
  
set startIndex to 0
  
  
repeat until (startIndex = theLength)
    set {theAtts, theRange} to theStyledText’s attributesAtIndex:startIndex longestEffectiveRange:(reference) inRange:{startIndex, theLength – startIndex}
    
    
–String  
    
set aText to (thePureString’s substringWithRange:theRange) as string
    
    
–Color
    
set aColor to (theAtts’s valueForKeyPath:"NSColor")
    
if aColor is not equal to missing value then
      set aSpace to aColor’s colorSpace()
      
      
set aRed to (aColor’s redComponent()) * 255
      
set aGreen to (aColor’s greenComponent()) * 255
      
set aBlue to (aColor’s blueComponent()) * 255
      
      
set colList to {aRed as integer, aGreen as integer, aBlue as integer} –for comparison
      
set colStrForFind to (aRed as integer as string) & " " & (aGreen as integer as string) & " " & (aBlue as integer as string) –for filtering
    else
      set colList to {0, 0, 0}
      
set colStrForFind to "0 0 0"
    end if
    
    
–Color domain name
    
set cdnStr to retColorDomainNameFromNSColor(aColor) of me
    
    
–Font
    
set aFont to (theAtts’s valueForKeyPath:"NSFont")
    
if aFont is not equal to missing value then
      set aDFontName to aFont’s displayName()
      
set aDFontSize to aFont’s pointSize()
    end if
    
    
set the end of (styleList of aSpd) to {stringVal:aText, colorStr:colStrForFind, colorVal:colList, colorDomainName:cdnStr, fontName:aDFontName as string, fontSize:aDFontSize}
    
set startIndex to current application’s NSMaxRange(theRange)
    
  end repeat
  
  
return (styleList of aSpd)
  
end getAttributeRunsFromAttrString

on retColorDomainNameFromNSColor(aCol)
  set hueVal to aCol’s hueComponent()
  
set satVal to aCol’s saturationComponent()
  
set brightVal to aCol’s brightnessComponent()
  
  
if satVal ≤ 0.01 then set satVal to 0.0
  
  
set colName to ""
  
  
if satVal = 0.0 then
    if brightVal ≤ 0.2 then
      set colName to "black"
    else if (brightVal > 0.95) then
      set colName to "white"
    else
      set colName to "gray"
    end if
  else
    if hueVal ≤ (15.0 / 360) or hueVal ≥ (330 / 360) then
      set colName to "red"
    else if hueVal ≤ (45.0 / 360) then
      set colName to "orange"
    else if hueVal < (70.0 / 360) then
      set colName to "yellow"
    else if hueVal < (150.0 / 360) then
      set colName to "green"
    else if hueVal < (190.0 / 360) then
      set colName to "cyan"
    else if (hueVal < 250.0 / 360.0) then
      set colName to "blue"
    else if (hueVal < 290.0 / 360.0) then
      set colName to "purple"
    else
      set colName to "magenta"
    end if
  end if
  
  
return colName
end retColorDomainNameFromNSColor

★Click Here to Open This Script 

Posted in Color list Record RTF | Tagged 10.11savvy 10.12savvy 10.13savvy | 1 Comment

リストから選択してアイテム番号を返す

Posted on 2月 8, 2018 by Takaaki Naganoya

AppleScriptの配列変数であるリスト型の変数(ただし、1次元)から選択し、選択した要素のアイテム番号(1はじまり)を数値で返すAppleScriptです。

AppleScriptに標準で用意されている「choose from list」コマンドは、選択した項目の文字列を返してきますが、その仕様だと困るケースがけっこうあります。

選択された要素の内容(文字列)ではなく、何項目(インデックス、アイテム番号)が選択されたかという情報を返してきてほしいところです。

そこで、ないなら作ってしまえばよいわけで、そういう仕様のルーチンを自分で作っておいたわけです。

AppleScript名:リストから選択してアイテム番号を返す
set aList to {"red", "blue", "green", "white"}
set aMes to "項目を選択してください"
set aRes to retItemFromListByItemNo(aList, aMes) of me
–> 3 (選択したアイテムの番号(1はじまり)が返る

–リストから選択してアイテム番号を返す
on retItemFromListByItemNo(aList, aMes)
  set aRes to choose from list aList with prompt aMes
  
if aRes = false then return 0
  
  
set aRes to contents of item 1 of aRes
  
set hitNum to 1
  
repeat with i in aList
    set j to contents of i
    
if j is equal to aRes then
      exit repeat
    end if
    
set hitNum to hitNum + 1
  end repeat
  
return hitNum
end retItemFromListByItemNo

★Click Here to Open This Script 

Posted in dialog list | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

都道府県リストから隣接都道府県を含む該当のコードを抽出する

Posted on 2月 8, 2018 by Takaaki Naganoya

膨大な位置情報の距離を計算するような場合に、対象の都道府県と隣接する都道府県に限定して距離計算を行うことで計算量を削減するためのAppleScriptです。

ある1点の位置情報に対して大量の地点との距離計算を行う処理は割とよくあります。自宅からの最寄駅の計算や、日本全国に点在している施設の最寄駅の計算などなどです。

たいていは、総当たりですべての計算対象との距離計算を行い、近い順でソートすることになります。自分の場合では日本国内の700か所ぐらいの(「戦場の絆」が設置されている)ゲームセンターの位置と日本全国の鉄道の駅(8,000か所程度)を比較して最寄駅を計算しました。

700 x 8,000 = 5,600,000回の距離計算を行うわけで、あまりに時間がかかるのでめまいがします(90分ぐらいかかりました)。1か所あたりの距離計算の所用時間は0.086秒程度なのでいちがいに遅いとはいえません(CocoaのCoreLocationの機能を使わないで、地球を球としてみなして近似的な距離計算を行えば速くなることでしょう)。

ここで、脳みそが頭に入っている人間であれば考えるわけです。もっと速く計算できる方法があるんじゃないか? と。

だいたい、北海道にある地点の最寄駅を求めるのに九州の最果てにある駅との距離計算を行う必要はありません。8,000箇所のすべての駅との距離計算を行う中には、膨大な「無意味な計算」が含まれています。この、「無意味な計算」を除外してあげれば、計算量が半分で済む場合には計算時間は半分になります。

そうやって考えていくと、都道府県レベルで隣接している地点については計算を行う必要がありますが、隣接していない都道府県にある駅との計算は行う意味がないことに気づきます。

そして実際に本テーブルを作成して計算に導入したところ、当初の90分が10分に短縮されました。さらに、データの持ち方を変えて最終的には5分ぐらいまで短縮できました。

「都道府県レベルで計算対象の除外ができて、高速処理を実現できたんだから、市区町村レベルの隣接情報を作って計算すれば、さらに高速化できるのでは?」

と考える人がいるかもしれません。ただ、その処理を実際に頭の中でシミュレーションしてみると、市区町村レベルでは「駅」が存在していないものもあったりで、日本国内に3,0000程度あるとみられるそのレベルの地方公共団体の隣接データの作成は大変です。たしかに高速化できるかもしれませんが、その成果は微々たるものになることでしょう(東京、名古屋、大阪などの大都市にかぎっては効果があるかも? その他の地域ではさほど効果はありません)。

対象は日本国内の都道府県。海外の住所情報でも同様に都道府県にコードを振って隣接情報をデータ化すれば同様の処理は可能です。

# BridgePlus Script Libraryを併用しなくてもすむ v2を追加掲載しておきました

AppleScript名:都道府県リストから隣接都道府県を含む該当のコードを抽出する
— Created 2015-12-06 by Takaaki Naganoya
— 2015 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use BridgePlus : script "BridgePlus" –https://www.macosxautomation.com/applescript/apps/BridgePlus.html

property prefList : {{prefCode:1, prefName:"北海道", neighbors:{}}, {prefCode:2, prefName:"青森県", neighbors:{3, 5}}, {prefCode:3, prefName:"岩手県", neighbors:{2, 4, 5}}, {prefCode:4, prefName:"宮城県", neighbors:{3, 5, 6, 7}}, {prefCode:5, prefName:"秋田県", neighbors:{2, 3, 4, 6}}, {prefCode:6, prefName:"山形県", neighbors:{3, 4, 5, 7, 15}}, {prefCode:7, prefName:"福島県", neighbors:{4, 6, 8, 9, 10, 15}}, {prefCode:8, prefName:"茨城県", neighbors:{7, 9, 10, 11, 12}}, {prefCode:9, prefName:"栃木県", neighbors:{7, 8, 10, 11, 12}}, {prefCode:10, prefName:"群馬県", neighbors:{7, 9, 11, 15, 20}}, {prefCode:11, prefName:"埼玉県", neighbors:{8, 9, 10, 12, 13, 19, 20}}, {prefCode:12, prefName:"千葉県", neighbors:{8, 11, 13}}, {prefCode:13, prefName:"東京都", neighbors:{11, 12, 19, 14}}, {prefCode:14, prefName:"神奈川県", neighbors:{13, 19, 22}}, {prefCode:15, prefName:"新潟県", neighbors:{6, 7, 10, 16, 20}}, {prefCode:16, prefName:"富山県", neighbors:{15, 17, 20, 21}}, {prefCode:17, prefName:"石川県", neighbors:{16, 18, 21}}, {prefCode:18, prefName:"福井県", neighbors:{17, 21, 25, 26}}, {prefCode:19, prefName:"山梨県", neighbors:{11, 13, 14, 20, 22}}, {prefCode:20, prefName:"長野県", neighbors:{10, 11, 15, 16, 19, 21, 22, 23}}, {prefCode:21, prefName:"岐阜県", neighbors:{16, 17, 18, 20, 23, 24, 25}}, {prefCode:22, prefName:"静岡県", neighbors:{14, 19, 20, 23}}, {prefCode:23, prefName:"愛知県", neighbors:{20, 21, 22, 24}}, {prefCode:24, prefName:"三重県", neighbors:{21, 23, 25, 26, 29}}, {prefCode:25, prefName:"滋賀県", neighbors:{18, 21, 24, 26}}, {prefCode:26, prefName:"京都府", neighbors:{18, 24, 25, 27, 28, 29}}, {prefCode:27, prefName:"大阪府", neighbors:{26, 29, 28, 30}}, {prefCode:28, prefName:"兵庫県", neighbors:{26, 27, 31, 33}}, {prefCode:29, prefName:"奈良県", neighbors:{24, 25, 26, 27, 30}}, {prefCode:30, prefName:"和歌山県", neighbors:{24, 27, 29}}, {prefCode:31, prefName:"鳥取県", neighbors:{28, 33, 32, 34}}, {prefCode:32, prefName:"島根県", neighbors:{31, 34, 35}}, {prefCode:33, prefName:"岡山県", neighbors:{28, 31, 34}}, {prefCode:34, prefName:"広島県", neighbors:{33, 31, 32, 35}}, {prefCode:35, prefName:"山口県", neighbors:{32, 34}}, {prefCode:36, prefName:"徳島県", neighbors:{37, 39}}, {prefCode:37, prefName:"香川県", neighbors:{36, 38, 39}}, {prefCode:38, prefName:"愛媛県", neighbors:{37, 39}}, {prefCode:39, prefName:"高知県", neighbors:{36, 37, 38}}, {prefCode:40, prefName:"福岡県", neighbors:{44, 43, 41}}, {prefCode:41, prefName:"佐賀県", neighbors:{40, 42}}, {prefCode:42, prefName:"長崎県", neighbors:{41}}, {prefCode:43, prefName:"熊本県", neighbors:{40, 42, 44, 45, 46}}, {prefCode:44, prefName:"大分県", neighbors:{40, 43, 45}}, {prefCode:45, prefName:"宮崎県", neighbors:{43, 44, 46}}, {prefCode:46, prefName:"鹿児島県", neighbors:{43, 45}}, {prefCode:47, prefName:"沖縄県", neighbors:{}}}

set aPref to 13 –Tokyo
set aRes to my filterRecListByLabel2(prefList, "prefCode == [c]%@", {aPref})
set targList to neighbors of aRes & aPref
–>  {​​​​​11, ​​​​​12, ​​​​​19, ​​​​​14, ​​​​​13​​​}–Saitama, Chiba, yamanashi, Kanagawa, Tokyo

–リストに入れたレコードを、指定の属性ラベルの値で抽出(predicateとパラメータを分離)し、1つのアイテムだけを返す
on filterRecListByLabel2(aRecList as list, aPredicate as string, aParam)
  set aArray to current application’s NSArray’s arrayWithArray:aRecList
  
set aPredicate to current application’s NSPredicate’s predicateWithFormat:aPredicate argumentArray:aParam
  
set filteredArray to aArray’s filteredArrayUsingPredicate:aPredicate
  
set bList to ASify from filteredArray as list
  
set cList to first item of bList
  
return cList
end filterRecListByLabel2

★Click Here to Open This Script 

AppleScript名:都道府県リストから隣接都道府県を含む該当のコードを抽出する v2.scpt
— Created 2015-12-06 by Takaaki Naganoya
— 2015 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
—BridgePlusを使わなくていいように書き換え

property prefList : {{prefCode:1, prefName:"北海道", neighbors:{}}, {prefCode:2, prefName:"青森県", neighbors:{3, 5}}, {prefCode:3, prefName:"岩手県", neighbors:{2, 4, 5}}, {prefCode:4, prefName:"宮城県", neighbors:{3, 5, 6, 7}}, {prefCode:5, prefName:"秋田県", neighbors:{2, 3, 4, 6}}, {prefCode:6, prefName:"山形県", neighbors:{3, 4, 5, 7, 15}}, {prefCode:7, prefName:"福島県", neighbors:{4, 6, 8, 9, 10, 15}}, {prefCode:8, prefName:"茨城県", neighbors:{7, 9, 10, 11, 12}}, {prefCode:9, prefName:"栃木県", neighbors:{7, 8, 10, 11, 12}}, {prefCode:10, prefName:"群馬県", neighbors:{7, 9, 11, 15, 20}}, {prefCode:11, prefName:"埼玉県", neighbors:{8, 9, 10, 12, 13, 19, 20}}, {prefCode:12, prefName:"千葉県", neighbors:{8, 11, 13}}, {prefCode:13, prefName:"東京都", neighbors:{11, 12, 19, 14}}, {prefCode:14, prefName:"神奈川県", neighbors:{13, 19, 22}}, {prefCode:15, prefName:"新潟県", neighbors:{6, 7, 10, 16, 20}}, {prefCode:16, prefName:"富山県", neighbors:{15, 17, 20, 21}}, {prefCode:17, prefName:"石川県", neighbors:{16, 18, 21}}, {prefCode:18, prefName:"福井県", neighbors:{17, 21, 25, 26}}, {prefCode:19, prefName:"山梨県", neighbors:{11, 13, 14, 20, 22}}, {prefCode:20, prefName:"長野県", neighbors:{10, 11, 15, 16, 19, 21, 22, 23}}, {prefCode:21, prefName:"岐阜県", neighbors:{16, 17, 18, 20, 23, 24, 25}}, {prefCode:22, prefName:"静岡県", neighbors:{14, 19, 20, 23}}, {prefCode:23, prefName:"愛知県", neighbors:{20, 21, 22, 24}}, {prefCode:24, prefName:"三重県", neighbors:{21, 23, 25, 26, 29}}, {prefCode:25, prefName:"滋賀県", neighbors:{18, 21, 24, 26}}, {prefCode:26, prefName:"京都府", neighbors:{18, 24, 25, 27, 28, 29}}, {prefCode:27, prefName:"大阪府", neighbors:{26, 29, 28, 30}}, {prefCode:28, prefName:"兵庫県", neighbors:{26, 27, 31, 33}}, {prefCode:29, prefName:"奈良県", neighbors:{24, 25, 26, 27, 30}}, {prefCode:30, prefName:"和歌山県", neighbors:{24, 27, 29}}, {prefCode:31, prefName:"鳥取県", neighbors:{28, 33, 32, 34}}, {prefCode:32, prefName:"島根県", neighbors:{31, 34, 35}}, {prefCode:33, prefName:"岡山県", neighbors:{28, 31, 34}}, {prefCode:34, prefName:"広島県", neighbors:{33, 31, 32, 35}}, {prefCode:35, prefName:"山口県", neighbors:{32, 34}}, {prefCode:36, prefName:"徳島県", neighbors:{37, 39}}, {prefCode:37, prefName:"香川県", neighbors:{36, 38, 39}}, {prefCode:38, prefName:"愛媛県", neighbors:{37, 39}}, {prefCode:39, prefName:"高知県", neighbors:{36, 37, 38}}, {prefCode:40, prefName:"福岡県", neighbors:{44, 43, 41}}, {prefCode:41, prefName:"佐賀県", neighbors:{40, 42}}, {prefCode:42, prefName:"長崎県", neighbors:{41}}, {prefCode:43, prefName:"熊本県", neighbors:{40, 42, 44, 45, 46}}, {prefCode:44, prefName:"大分県", neighbors:{40, 43, 45}}, {prefCode:45, prefName:"宮崎県", neighbors:{43, 44, 46}}, {prefCode:46, prefName:"鹿児島県", neighbors:{43, 45}}, {prefCode:47, prefName:"沖縄県", neighbors:{}}}

set aPref to 13 –Tokyo
set aRes to my filterRecListByLabel2(prefList, "prefCode == [c]%@", {aPref})
set targList to neighbors of aRes & aPref
–>  {​​​​​11, ​​​​​12, ​​​​​19, ​​​​​14, ​​​​​13​​​}–Saitama, Chiba, yamanashi, Kanagawa, Tokyo

–リストに入れたレコードを、指定の属性ラベルの値で抽出(predicateとパラメータを分離)し、1つのアイテムだけを返す
on filterRecListByLabel2(aRecList as list, aPredicate as string, aParam)
  set aArray to current application’s NSArray’s arrayWithArray:aRecList
  
set aPredicate to current application’s NSPredicate’s predicateWithFormat:aPredicate argumentArray:aParam
  
set filteredArray to aArray’s filteredArrayUsingPredicate:aPredicate
  
set bList to filteredArray as list
  
set cList to first item of bList
  
return cList
end filterRecListByLabel2

★Click Here to Open This Script 

Posted in geolocation list | Tagged 10.11savvy 10.12savvy 10.13savvy | 1 Comment

2D ListをCSVに v3(サニタイズ処理つき)

Posted on 2月 7, 2018 by Takaaki Naganoya
AppleScript名:2D ListをCSVに v3(サニタイズ処理つき)
— Created 2015-10-01 by Takaaki Naganoya
— 2015 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set aNewFile to choose file name

set dataList to {{"0010", "ひよこタオルギフト", "200", "手に取ったとき、\"使うとき\"、ちょっと楽しくてかわいいひよこのタオル。", "●サイズ/H200㎜×W200㎜●素材/ひよこ羽毛100%●重量/170g●内容/5枚入り"}, {"0020", "ひよこホイッスル", "250", "今までにないデザインの、ひよこ型のホイッスル。ぴよ〜音を音階で吹き分けます。", "●サイズ/H60㎜×W40㎜×D10㎜●素材/プラスチック
●色/ひよこ色●重量/10g●付属品/首かけロープ付き
●型番/PIYO1"
}}

saveAsCSV(dataList, aNewFile) of me

–2D List to CSV file
on saveAsCSV(aList, aPath)
  –set crlfChar to (ASCII character 13) & (ASCII character 10)
  
set crlfChar to (string id 13) & (string id 10)
  
set LF to (string id 10)
  
set wholeText to ""
  
  
repeat with i in aList
    set newLine to {}
    
    
–Sanitize (Double Quote)
    
repeat with ii in i
      set jj to ii as text
      
set kk to repChar(jj, string id 34, (string id 34) & (string id 34)) of me –Escape Double Quote
      
set the end of newLine to kk
    end repeat
    
    
–Change Delimiter
    
set aLineText to ""
    
set curDelim to AppleScript’s text item delimiters
    
set AppleScript’s text item delimiters to "\",\""
    
set aLineList to newLine as text
    
set AppleScript’s text item delimiters to curDelim
    
    
set aLineText to repChar(aLineList, return, "") of me –delete return
    
set aLineText to repChar(aLineText, LF, "") of me –delete lf
    
    
set wholeText to wholeText & "\"" & aLineText & "\"" & crlfChar –line terminator: CR+LF
  end repeat
  
  
if (aPath as string) does not end with ".csv" then
    set bPath to aPath & ".csv" as Unicode text
  else
    set bPath to aPath as Unicode text
  end if
  
  
write_to_file(wholeText, bPath, false) of me
  
end saveAsCSV

on write_to_file(this_data, target_file, append_data)
  tell current application
    try
      set the target_file to the target_file as text
      
set the open_target_file to open for access file target_file with write permission
      
if append_data is false then set eof of the open_target_file to 0
      
write this_data to the open_target_file starting at eof
      
close access the open_target_file
      
return true
    on error error_message
      try
        close access file target_file
      end try
      
return error_message
    end try
  end tell
end write_to_file

on repChar(origText as text, targChar as text, repChar as text)
  set curDelim to AppleScript’s text item delimiters
  
set AppleScript’s text item delimiters to targChar
  
set tmpList to text items of origText
  
set AppleScript’s text item delimiters to repChar
  
set retText to tmpList as string
  
set AppleScript’s text item delimiters to curDelim
  
return retText
end repChar

★Click Here to Open This Script 

Posted in file list Text | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

CSVのParse 5(ASOC)

Posted on 2月 7, 2018 by Takaaki Naganoya
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 

Posted in file list Text | Tagged 10.11savvy 10.12savvy 10.13savvy | 1 Comment

CSVのParse 4a(ASOC)

Posted on 2月 7, 2018 by Takaaki Naganoya
AppleScript名:CSVのParse 4a(ASOC)
— Created 2015-03-11 by Shane Stanley
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 someString to read (choose file of type {"public.comma-separated-values-text"})

set theData to current application’s SMSForder’s arrayFromCSV:someString commaIs:","
set theData to (current application’s SMSForder’s subarraysIn:theData paddedWith:"" |error|:(missing value)) as list
–>  {​​​​​{​​​​​​​"cust1", ​​​​​​​"prod,1", ​​​​​​​"season 1", ​​​​​​​""​​​​​}, ​​​​​{​​​​​​​"cust1", ​​​​​​​"prod1", ​​​​​​​"season 2", ​​​​​​​""​​​​​}, ​​​​​{​​​​​​​"cust2", ​​​​​​​"prod1", ​​​​​​​"event1", ​​​​​​​"season 1"​​​​​}, ​​​​​{​​​​​​​"cust2", ​​​​​​​"prod2", ​​​​​​​"event1", ​​​​​​​"season 2"​​​​​}, ​​​​​{​​​​​​​"cust2", ​​​​​​​"prod3", ​​​​​​​"event1", ​​​​​​​"season 1"​​​​​}​​​}

★Click Here to Open This Script 

Posted in file list Text | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

ASOCでNSPredicateによる正規表現を併用した抽出

Posted on 2月 7, 2018 by Takaaki Naganoya
AppleScript名:ASOCでNSPredicateによる正規表現を併用した抽出
— Created 2015-09-28 by Takaaki Naganoya
— 2015 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use BridgePlus : script "BridgePlus" —https://www.macosxautomation.com/applescript/apps/BridgePlus.html

set sampleList to {{textData:"Piyomaru", uID:1}, {textData:"Xx Piyomaru x", uID:2}, {textData:"xxxxx 11111111 98 x xxxxxxxx.", uID:3}, {textData:"98x Xxxxxx (xx xxxxxxxxxx)", uID:4}, {textData:"<< 98158113 >>", uID:5}, {textData:"#98158084 Xxxxx Xxxxx xxxx", uID:6}, {textData:"#98158084 Xxxxx Xxxxx xxxx", uID:7}, {textData:"Office # 98158107", uID:8}, {textData:"ID#98158087", uID:9}, {textData:"98158089", uID:10}, {textData:"00158098", uID:11}}

–全文一致で抽出
set aRes to my filterRecListByLabel1(sampleList, "textData == ’Piyomaru’")
–>  {​​​​​{​​​​​​​textData:"Piyomaru", ​​​​​​​uID:1​​​​​}​​​}

–部分一致で抽出
set bRes to my filterRecListByLabel1(sampleList, "textData contains ’Piyomaru’")
–>  {​​​​​{​​​​​​​textData:"Piyomaru", ​​​​​​​uID:1​​​​​}, ​​​​​{​​​​​​​textData:"Xx Piyomaru x", ​​​​​​​uID:2​​​​​}​​​}

–正規表現で抽出(8桁の数字)
set cRes to my filterRecListByLabel1(sampleList, "textData MATCHES ’\\\\d{8}’")
–>  {​​​​​{​​​​​​​textData:"98158089", ​​​​​​​uID:10​​​​​}, ​​​​​{​​​​​​​textData:"00158089", ​​​​​​​uID:11​​​​​}​​​}

set dRes to my filterRecListByLabel1(sampleList, "textData MATCHES ’98\\\\d{6}’")
–>  {​​​​​{​​​​​​​textData:"98158089", ​​​​​​​uID:10​​​​​}​​​}

set eRes to my filterRecListByLabel1(sampleList, "textData LIKE ’*98??????*’")
–>  {​​​​​{​​​​​​​textData:"xxxxx 11111111 98 x xxxxxxxx.", ​​​​​​​uID:3​​​​​}, ​​​​​{​​​​​​​textData:"98x Xxxxxx (xx xxxxxxxxxx)", ​​​​​​​uID:4​​​​​}, ​​​​​{​​​​​​​textData:"<< 98158113 >>", ​​​​​​​uID:5​​​​​}, ​​​​​{​​​​​​​textData:"#98158084 Xxxxx Xxxxx xxxx", ​​​​​​​uID:6​​​​​}, ​​​​​{​​​​​​​textData:"#98158084 Xxxxx Xxxxx xxxx", ​​​​​​​uID:7​​​​​}, ​​​​​{​​​​​​​textData:"Office # 98158107", ​​​​​​​uID:8​​​​​}, ​​​​​{​​​​​​​textData:"ID#98158087", ​​​​​​​uID:9​​​​​}, ​​​​​{​​​​​​​textData:"98158089", ​​​​​​​uID:10​​​​​}​​​}

set fRes to my filterRecListByLabel1(sampleList, "textData LIKE ’*\"98\"[0-9][0-9][0-9][0-9][0-9][0-9]*’") –Oops!!
–>  {}

set gRes to my filterRecListByLabel1(sampleList, "textData LIKE ’*[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]*’") –Oops!!
–>  {}

set hRes to my filterRecListByLabel1(sampleList, "textData MATCHES ’.*[98]\\\\d{6}.*’") –OK!!

–>  {​​​​​{​​​​​​​textData:"<< 98158113 >>", ​​​​​​​uID:5​​​​​}, ​​​​​{​​​​​​​textData:"#98158084 Xxxxx Xxxxx xxxx", ​​​​​​​uID:6​​​​​}, ​​​​​{​​​​​​​textData:"#98158084 Xxxxx Xxxxx xxxx", ​​​​​​​uID:7​​​​​}, ​​​​​{​​​​​​​textData:"Office # 98158107", ​​​​​​​uID:8​​​​​}, ​​​​​{​​​​​​​textData:"ID#98158087", ​​​​​​​uID:9​​​​​}, ​​​​​{​​​​​​​textData:"98158089", ​​​​​​​uID:10​​​​​}​​​}

–リストに入れたレコードを、指定の属性ラベルの値で抽出
on filterRecListByLabel1(aRecList as list, aPredicate as string)
  set aArray to current application’s NSArray’s arrayWithArray:aRecList
  
set aPredicate to current application’s NSPredicate’s predicateWithFormat:aPredicate
  
set filteredArray to aArray’s filteredArrayUsingPredicate:aPredicate
  
set bList to ASify from filteredArray as list
  
return bList
end filterRecListByLabel1

★Click Here to Open This Script 

Posted in list Record | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

リストに入れたレコードを、指定の属性ラベルの値で抽出

Posted on 2月 7, 2018 by Takaaki Naganoya
AppleScript名:リストに入れたレコードを、指定の属性ラベルの値で抽出
— Created 2017-03-16 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set aList to {{aLabel:"1", bLabel:"1"}, {aLabel:"2", bLabel:"2"}, {aLabel:"3", bLabel:"3"}, {aLabel:"4", bLabel:"4"}}

set anArray to current application’s NSMutableArray’s arrayWithArray:aList
set aRes to my filterRecListByLabel1(anArray, "aLabel == ’1’")
–>  {​​​​​{​​​​​​​aLabel:"1", ​​​​​​​bLabel:"1"​​​​​}​​​}

–リストに入れたレコードを、指定の属性ラベルの値で抽出
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 as list
end filterRecListByLabel1

★Click Here to Open This Script 

Posted in list Record | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

ASOCでレコードのリストから抽出

Posted on 2月 7, 2018 by Takaaki Naganoya
AppleScript名:ASOCでレコードのリストから抽出
— Created 2017-08-05 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set dList to {{deviceName:"Piyomaru AirPods", deviceAddress:"7c-04-d0-8b-bc-fb"}, {deviceName:"Takaaki Naganoya のマウス", deviceAddress:"ac-bc-32-dd-99-3e"}, {deviceName:"Takaaki Naganoya のキーボード #1", deviceAddress:"04-69-f8-be-2a-c7"}}

set dRes to filterRecListByLabel(dList, "deviceName contains ’AirPods’") of me
if dRes = {} then return false –Case: No match
set dAddr to dRes’s first item’s deviceAddress
return dAddr

–リストに入れたレコードを、指定の属性ラベルの値で抽出
on filterRecListByLabel(aRecList as list, aPredicate as string)
  –ListからNSArrayへの型変換
  
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
  
  
–NSArrayからListに型変換して返す
  
set bList to filteredArray as list
  
return bList
end filterRecListByLabel

★Click Here to Open This Script 

Posted in list Record | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

ASOCでDict読み込みして、指定のMSの搭乗回数を取得する v2

Posted on 2月 7, 2018 by Takaaki Naganoya
AppleScript名:ASOCでDict読み込みして、指定のMSの搭乗回数を取得する v2
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set aName to "efsf.plist"
set aFolName to "戦場の絆"
set aRec to retDictFromPlist(aFolName, aName) of me
set msL to msList of aRec

set eList to filterRecListByLabel(msL, "msName CONTAINS ’近 ザクII(F2) 獲得済’") of me
set aTimes to sortieTimes of first item of eList

on retDictFromPlist(aFolName, aPlistName)
  
  
set myAppSupDir to ((path to application support from user domain) as string) & aFolName & ":"
  
tell application "System Events" –Finderでなくこちらを使ってみた
    tell folder myAppSupDir
      set aExit to exists of file aPlistName
    end tell
  end tell
  
  
if aExit = false then
    return {}
  else
    set aPath to (POSIX path of myAppSupDir) & aPlistName
    
set thePath to current application’s NSString’s stringWithString:aPath
    
set thePath to thePath’s stringByExpandingTildeInPath()
    
set theDict to current application’s NSDictionary’s dictionaryWithContentsOfFile:thePath
    
return theDict as record
  end if
  
end retDictFromPlist

–リストに入れたレコードを、指定の属性ラベルの値で抽出
on filterRecListByLabel(aRecList as list, aPredicate as string)
  –ListからNSArrayへの型変換
  
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
  
  
–NSArrayからListに型変換して返す
  
set bList to filteredArray as list
  
return bList
end filterRecListByLabel

★Click Here to Open This Script 

Posted in file list Record | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

ASOCでDict書き込み_3(Bridge Plus)

Posted on 2月 7, 2018 by Takaaki Naganoya
AppleScript名:ASOCでDict書き込み_3(Bridge Plus)
use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
use script "BridgePlus" — https://www.macosxautomation.com/applescript/apps/BridgePlus.html

load framework — BridgePlus command to load

set a1List to {"msName", "sortieTimes"}
set b1List to {{"近 装甲強化型ジム 獲得済 COST: 200", 66}, {"遠 ジム・キャノン 獲得済 COST: 160", 43}, {"近 ザクII(F2) 獲得済 COST: 160", 42}, {"近 ジム・コマンド 獲得済 COST: 200", 32}, {"近 ジム(WD隊) 獲得済 COST: 160", 28}, {"近 陸戦型ガンダム 獲得済 COST: 220", 24}, {"近 ジム改 獲得済 COST: 240", 22}, {"遠 ガンタンク 獲得済 COST: 200", 22}, {"格 ジム(指揮官機) 獲得済 COST: 160", 20}, {"近 ジム 獲得済 COST: 120", 19}, {"遠 量産型ガンタンク 獲得済 COST: 160", 14}, {"格 陸戦型ジム 獲得済 COST: 120", 12}, {"格 ガンダム 獲得済 COST: 280", 11}, {"近 ジム・トレーナー 獲得済 COST: 120", 9}, {"射 ジム・スナイパーII(WD隊) 獲得済 COST: 220", 9}, {"射 陸戦型ガンダム(ジム頭) 獲得済 COST: 200", 7}, {"格 ガンダムEz8 獲得済 COST: 240", 6}, {"近 ジム・寒冷地仕様 獲得済 COST: 200", 6}, {"狙 ジム・スナイパーカスタム 獲得済 COST: 200", 6}, {"格 ジム・ストライカー 獲得済 COST: 180", 4}, {"格 ガンキャノン重装型 獲得済 COST: 160", 3}, {"近 アクア・ジム 獲得済 COST: 160", 2}, {"射 ガンキャノン 獲得済 COST: 200", 2}, {"近 ジム・コマンドライトアーマー 獲得済 COST: 160", 1}, {"格 ボールK型 獲得済 COST: 120", 0}, {"格 B.D.2号機 獲得済 COST: 260", 0}, {"格 プロトタイプガンダム 獲得済 COST: 280", 0}, {"近 パワード・ジム 獲得済 COST: 240", 0}, {"射 デザート・ジム 獲得済 COST: 160", 0}, {"遠 量産型ガンキャノン 獲得済 COST: 200", 0}}

— BridgePlus uses SMSForder instead of SMSFord in ASOBjCExtras, but method is the same
set aArray to current application’s SMSForder’s subarraysIn:b1List asDictionariesUsingLabels:a1List |error|:(missing value)

set cRec to {msList:aArray, sortieDate:date string of (current date)}

set aName to "efsf.plist"
saveRecordToFolAsPlist(cRec, "戦場の絆", aName) of me

on saveRecordToFolAsPlist(theRecord, folName, aName)
  
  
set myAppSupDir to POSIX path of (path to application support from user domain)
  
set folderURL to (current application’s class "NSURL"’s fileURLWithPath:myAppSupDir)’s URLByAppendingPathComponent:folName
  
  
–do shell script(mkdir -p)のかわりに、指定ディレクトリまで作成
  
current application’s NSFileManager’s defaultManager()’s createDirectoryAtURL:folderURL withIntermediateDirectories:true attributes:(missing value) |error|:(missing value)
  
  
set theDict to current application’s NSDictionary’s dictionaryWithDictionary:theRecord
  
set aRes to theDict’s writeToURL:(folderURL’s URLByAppendingPathComponent:aName) atomically:true
  
  
return aRes as boolean
  
end saveRecordToFolAsPlist

★Click Here to Open This Script 

Posted in file list Record | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

ASOCでレコードのリストをユニーク化

Posted on 2月 7, 2018 by Takaaki Naganoya
AppleScript名:ASOCでレコードのリストをユニーク化
use AppleScript version "2.4"
use framework "Foundation"
use scripting additions

set msRecList to {{msName:"格 陸戦型ジム 獲得済 COST: 120", sortieTimes:12}, {msName:"狙 ジム・スナイパーカスタム 獲得済 COST: 200", sortieTimes:6}, {msName:"狙 ジム・スナイパーカスタム 獲得済 COST: 200", sortieTimes:6}}

set newMsList to uniquefyList(msRecList)
–>  {{sortieTimes:6, msName:"狙 ジム・スナイパーカスタム 獲得済 COST: 200"}, {sortieTimes:12, msName:"格 陸戦型ジム 獲得済 COST: 120"}}

–レコードのリストをユニーク化
on uniquefyList(aList)
  set msArray to current application’s NSArray’s arrayWithArray:aList
  
set aRes to current application’s NSSet’s setWithArray:(msArray’s allObjects())
  
set bRes to aRes’s allObjects()
  
set cRes to bRes as list
  
return cRes
end uniquefyList

★Click Here to Open This Script 

Posted in list Record | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

なんでもデータを文字列化 v2

Posted on 2月 7, 2018 by Takaaki Naganoya

数値でもリストでもレコードでもなんでも文字列化するAppleScriptです。

大規模なシステムを作っているときに、ログに詳細なデータを書き込む必要があって、その入り組んだrecordから各種要素を取り出すのが嫌だったので、「そのまま文字列化してしまえ」と考えて作成したものです。

エラートラップを仕掛けてわざとエラーを発生させ、エラーメッセージに表記されているデータを加工して出力するというトリッキーかつやけっぱちな処理内容ですが、たしかに動作し、日本語以外の言語環境でもきちんと動くためひそかに利用されている処理です。

ただし、本ルーチンではアプリケーションのオブジェクトを含むデータの文字列化が行えないので、のちになって作成したOSAScriptControllerで実際にAppleScriptを実行して結果をすべて文字列で取得するようなタイプのものに入れ替えています。

AppleScript名:なんでもデータを文字列化 v2
set a to {{aName:"PiyoPiyo", anAge:10}, {aName:"Piyoko", anAge:9}} –record in list
–set a to {aName:"PiyoPiyo", anAge:10}–record
–set a to {{1, 2, 3}, {4, 5, 6}}–list
–set a to 1.0 as real–real
–set a to 1 as integer–integer
–set a to "1.0" as string–string
–set a to true–boolean
–set a to front window of application "Finder"–アプリケーションのオブジェクトはエラーになるよ!
–set a to missing value

set aRes to convToStr(a) of somethingToStrKit
–> "{{aName:\"PiyoPiyo\", anAge:10}, {aName:\"Piyoko\", anAge:9}}"

–リストでもレコードでもなんでも文字列化して返すキット
script somethingToStrKit
  
  
on convToStr(aRec)
    
    
set aClass to (class of aRec) as string
    
    
if (aClass = "integer") or (aClass = "number") or (aClass = "real") or (aClass = "string") or (aClass = "text") or (aClass = "Unicode text") or (aClass = "boolean") then
      set aRes to aRec as string
    else if aClass is "list" then
      set aRes to listToString(aRec)
    else if aClass is "record" then
      set aRes to recToString(aRec)
    else
      try
        set aRes to aRec as string
      on error
        –アプリケーションのオブジェクトとかはエラーで返す
        
return false
      end try
    end if
    
    
return aRes
    
  end convToStr
  
  
  
–レコードをStringに変換
  
  
–エラートラップを使って、わざとエラーを発生させ、エラーメッセージからレコードをstringに変換する
  
on recToString(aRec)
    
    
–レコードを無理矢理stringにcastして、エラーメッセージを取得する
    
try
      set a to aRec as string –ここでエラー発生
    on error aMes
      set a to aMes
    end try
    
    
–エラーメッセージ文字列から、元のレコードの情報を組み立てる
    
set b to trimStrFromTo(a, "{", "}")
    
set b to "{" & b & "}"
    
    
return b
    
  end recToString
  
  
  
on trimStrFromTo(aStr, fromStr, toStr)
    –fromStrは前から探す
    
if fromStr is not equal to "" then
      set sPos to (offset of fromStr in aStr) + 1
    else
      set sPos to 1
    end if
    
    
–toStrは後ろから探す
    
if toStr is not equal to "" then
      set b to (reverse of characters of aStr) as string
      
set ePos to (offset of toStr in b)
      
set ePos to ((length of aStr) – ePos)
    else
      set ePos to length of aStr
    end if
    
set aRes to text sPos thru ePos of aStr
    
    
return aRes
    
  end trimStrFromTo
  
  
  
–リストおよびリストに入ったレコードをStringに変換
  
  
on listToString(aList)
    set listText to {"{"}
    
set quotChar to ASCII character 34
    
set firstFlag to true
    
    
repeat with i in aList
      set j to contents of i
      
set aClass to (class of i) as string
      
if (aClass = "integer") or (aClass = "number") or (aClass = "real") then
        set the end of listText to (getFirst(firstFlag) of me & j as text)
        
set firstFlag to false
      else if (aClass = "string") or (aClass = "text") or (aClass = "Unicode text") then
        set the end of listText to ((getFirst(firstFlag) of me & quotChar & j as text) & quotChar)
        
set firstFlag to false
      else if aClass is "list" then
        set the end of listText to (getFirst(firstFlag) & listToString(j)) –ちょっと再帰処理
        
set firstFlag to false
      else if aClass is "record" then
        set the end of listText to (getFirst(firstFlag) & recToString(j))
        
set firstFlag to false
      end if
    end repeat
    
    
set the end of listText to "}"
    
set listText to listText as text
    
    
return listText
  end listToString
  
  
on getFirst(aFlag)
    if aFlag = true then return ""
    
if aFlag = false then return ", "
  end getFirst
  
end script

★Click Here to Open This Script 

Posted in list Record Text | Tagged 10.11savvy 10.12savvy 10.13savvy | 2 Comments

asoc_レコードのリストをソート

Posted on 2月 7, 2018 by Takaaki Naganoya
AppleScript名:asoc_レコードのリストをソート
— Created 2017-05-22 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set aList to {{theWord:"英国", retweetCount:0}, {theWord:"新潟記念", retweetCount:0}, {theWord:"一時的", retweetCount:0}, {theWord:"原宿", retweetCount:0}, {theWord:"経常収支", retweetCount:0}, {theWord:"板倉", retweetCount:0}, {theWord:"格安スマホ", retweetCount:0}, {theWord:"Wikileaks", retweetCount:0}, {theWord:"ジャニーズ事務所", retweetCount:0}, {theWord:"日産", retweetCount:0}, {theWord:"あずきバー", retweetCount:0}, {theWord:"海南省", retweetCount:0}, {theWord:"海外市場", retweetCount:0}, {theWord:"亀田劇場", retweetCount:0}, {theWord:"東芝", retweetCount:0}, {theWord:"南シナ海", retweetCount:0}, {theWord:"光害", retweetCount:0}, {theWord:"債務不履行", retweetCount:0}, {theWord:"BR", retweetCount:0}, {theWord:"大リーグ", retweetCount:0}, {theWord:"イスラム", retweetCount:0}, {theWord:"未来", retweetCount:0}, {theWord:"インプラント", retweetCount:0}, {theWord:"リーバイス", retweetCount:0}, {theWord:"スマホ", retweetCount:0}, {theWord:"ジル", retweetCount:0}, {theWord:"G7", retweetCount:0}, {theWord:"貿易統計", retweetCount:0}, {theWord:"宮崎県", retweetCount:0}, {theWord:"新基準", retweetCount:0}, {theWord:"福岡三越", retweetCount:0}, {theWord:"Croiseur Milan", retweetCount:0}, {theWord:"リスクオン", retweetCount:0}, {theWord:"異常接近", retweetCount:0}, {theWord:"キャッシング", retweetCount:0}, {theWord:"キレイだ", retweetCount:0}, {theWord:"HD", retweetCount:0}, {theWord:"高校野球", retweetCount:0}, {theWord:"田母神俊雄", retweetCount:0}, {theWord:"2月", retweetCount:0}, {theWord:"本田圭佑", retweetCount:0}, {theWord:"工藤静香", retweetCount:0}, {theWord:"大久保", retweetCount:0}, {theWord:"山東", retweetCount:0}, {theWord:"生中継", retweetCount:0}, {theWord:"有田哲平", retweetCount:0}, {theWord:"ジカ熱", retweetCount:0}, {theWord:"11月26日", retweetCount:0}, {theWord:"事実上", retweetCount:0}, {theWord:"英語表記", retweetCount:0}, {theWord:"文句なし", retweetCount:0}, {theWord:"糖質制限", retweetCount:0}, {theWord:"ベン", retweetCount:0}, {theWord:"監視社会", retweetCount:0}, {theWord:"三原じゅん子", retweetCount:0}, {theWord:"週刊ダイヤモンド", retweetCount:0}, {theWord:"スウェーデン", retweetCount:0}, {theWord:"赤ちゃんポスト", retweetCount:0}, {theWord:"20歳", retweetCount:0}, {theWord:"見聞録", retweetCount:0}, {theWord:"運動員", retweetCount:0}, {theWord:"liverty", retweetCount:0}, {theWord:"民進党", retweetCount:0}, {theWord:"ケー・エフ・シー", retweetCount:0}, {theWord:"ロシア当局", retweetCount:0}, {theWord:"拡張現実", retweetCount:0}, {theWord:"キスショット", retweetCount:0}, {theWord:"に学ぶ", retweetCount:0}, {theWord:"ASCII", retweetCount:0}, {theWord:"FOMC", retweetCount:0}, {theWord:"機関投資家", retweetCount:0}, {theWord:"労働法", retweetCount:0}, {theWord:"カンヌ映画祭", retweetCount:0}, {theWord:"賛否両論", retweetCount:0}, {theWord:"ユキ", retweetCount:0}, {theWord:"サウジ", retweetCount:0}, {theWord:"スカイツリー", retweetCount:0}, {theWord:"アリゲーターガー", retweetCount:0}, {theWord:"オフィスワーク", retweetCount:0}, {theWord:"アフガン", retweetCount:0}, {theWord:"北九州", retweetCount:0}, {theWord:"キリスト", retweetCount:0}, {theWord:"利上げ観測", retweetCount:0}, {theWord:"渋谷", retweetCount:0}, {theWord:"カナダ", retweetCount:0}, {theWord:"京大", retweetCount:0}, {theWord:"侵略者", retweetCount:0}, {theWord:"夏季五輪", retweetCount:0}, {theWord:"乾", retweetCount:0}, {theWord:"グラミー賞", retweetCount:3154}, {theWord:"トヨタショック", retweetCount:27716}, {theWord:"スペイン", retweetCount:57180}, {theWord:"ソ連崩壊", retweetCount:1028}, {theWord:"マイナス金利", retweetCount:11783}, {theWord:"Triumph", retweetCount:5955}, {theWord:"通信社", retweetCount:10379}, {theWord:"そのままで", retweetCount:8114}, {theWord:"高須", retweetCount:50330}, {theWord:"田口淳之介", retweetCount:30}, {theWord:"利下げ観測", retweetCount:0}, {theWord:"日本企業", retweetCount:109187}, {theWord:"日向夏", retweetCount:98639}, {theWord:"観光農園", retweetCount:2342}, {theWord:"ASEAN", retweetCount:267}, {theWord:"規制撤廃", retweetCount:2044}, {theWord:"国2", retweetCount:27669}, {theWord:"サラリーマン川柳", retweetCount:581}, {theWord:"株式相場", retweetCount:6}, {theWord:"ハラ", retweetCount:1010}, {theWord:"仙台", retweetCount:44049}, {theWord:"駐日大使", retweetCount:233}, {theWord:"有吉", retweetCount:267388}, {theWord:"英", retweetCount:22235}, {theWord:"一発逆転", retweetCount:2923}, {theWord:"北海道", retweetCount:8818}, {theWord:"3D", retweetCount:18976}, {theWord:"佐田建設", retweetCount:2901}, {theWord:"穏健派", retweetCount:4641}, {theWord:"聞く力", retweetCount:399}, {theWord:"火星探査機", retweetCount:1216}, {theWord:"ウォール街", retweetCount:1092}, {theWord:"Crowd Funding", retweetCount:6811}, {theWord:"赤羽", retweetCount:6874}, {theWord:"株高", retweetCount:28}, {theWord:"Pizza Hut", retweetCount:55727}, {theWord:"KAT-TUN", retweetCount:3330}, {theWord:"ドイツ", retweetCount:573124}, {theWord:"バルサ", retweetCount:4872}, {theWord:"SweetS", retweetCount:16952}, {theWord:"コミー", retweetCount:1305}, {theWord:"ご当地グルメ", retweetCount:1240}, {theWord:"アレグラ", retweetCount:0}, {theWord:"安倍首相", retweetCount:49530}, {theWord:"大腸がん", retweetCount:78}, {theWord:"業務提携", retweetCount:119}, {theWord:"米国", retweetCount:11739}, {theWord:"5日", retweetCount:18262}, {theWord:"総選挙", retweetCount:8269}, {theWord:"東武鉄道", retweetCount:963}, {theWord:"Samsung", retweetCount:20895}, {theWord:"RALPH LAUREN", retweetCount:7611}, {theWord:"ロ", retweetCount:4692}, {theWord:"ローソン", retweetCount:166787}, {theWord:"大嘘", retweetCount:8333}, {theWord:"森友", retweetCount:39699}, {theWord:"高島市", retweetCount:9868}, {theWord:"チワワ", retweetCount:9022}, {theWord:"栗原類", retweetCount:3020}, {theWord:"プラスワン", retweetCount:2299}, {theWord:"東大阪", retweetCount:5250}, {theWord:"政治学者", retweetCount:3365}, {theWord:"京", retweetCount:6701}, {theWord:"開催地", retweetCount:10018}, {theWord:"視聴率", retweetCount:20114}, {theWord:"大企業", retweetCount:11424}, {theWord:"近畿", retweetCount:4530}, {theWord:"石狩", retweetCount:145}, {theWord:"小田急", retweetCount:94364}, {theWord:"ベルーフ", retweetCount:12}, {theWord:"鉄道ジャーナル", retweetCount:970}, {theWord:"IT", retweetCount:437124}, {theWord:"公明党", retweetCount:2060}, {theWord:"表参道", retweetCount:18059}, {theWord:"経済成長", retweetCount:2257}, {theWord:"魚住りえ", retweetCount:25}, {theWord:"女子高生", retweetCount:512226}, {theWord:"野村", retweetCount:14080}, {theWord:"Google", retweetCount:1515}, {theWord:"NYダウ", retweetCount:253}, {theWord:"くりぃむ", retweetCount:5893}, {theWord:"博多", retweetCount:679890}, {theWord:"交通事故", retweetCount:3622}, {theWord:"就職四季報", retweetCount:0}, {theWord:"大統領制", retweetCount:3455}, {theWord:"トルコ", retweetCount:28673}, {theWord:"長野駅", retweetCount:11630}, {theWord:"繁華街", retweetCount:8179}, {theWord:"Deep Impact", retweetCount:10379}, {theWord:"ソニー", retweetCount:25410}, {theWord:"統一省", retweetCount:1250}, {theWord:"赤羽駅", retweetCount:9681}, {theWord:"U2", retweetCount:214}, {theWord:"日経ビジネス", retweetCount:4942}, {theWord:"日本人男性", retweetCount:6990}, {theWord:"キャサリン妃", retweetCount:1242}, {theWord:"重賞", retweetCount:670}, {theWord:"ニッポン", retweetCount:10690}, {theWord:"ブラジル", retweetCount:53316}, {theWord:"本部長", retweetCount:12333}, {theWord:"香川真司", retweetCount:28884}, {theWord:"金融市場", retweetCount:248}, {theWord:"万景峰号", retweetCount:2228}, {theWord:"W杯", retweetCount:9696}, {theWord:"北朝鮮", retweetCount:260562}, {theWord:"You Tube", retweetCount:70170}, {theWord:"橋本愛", retweetCount:4255}, {theWord:"日の丸半導体", retweetCount:464}, {theWord:"Enterprise", retweetCount:19289}, {theWord:"SNS", retweetCount:273982}, {theWord:"ヴェノム", retweetCount:18037}, {theWord:"インフレ率", retweetCount:5034}, {theWord:"アプリ", retweetCount:312863}, {theWord:"柴崎岳", retweetCount:1282}, {theWord:"独自制裁", retweetCount:1548}, {theWord:"弾道ミサイル", retweetCount:12150}, {theWord:"エジプト", retweetCount:55094}, {theWord:"日独", retweetCount:1528}, {theWord:"大分県警", retweetCount:1133}, {theWord:"ベルギー", retweetCount:29151}, {theWord:"労働組合", retweetCount:14314}, {theWord:"委員長", retweetCount:30619}, {theWord:"企業戦士", retweetCount:2749}, {theWord:"円高株安", retweetCount:18536}, {theWord:"下げ幅", retweetCount:56}, {theWord:"日本型", retweetCount:2164}, {theWord:"ペットショップ", retweetCount:7808}, {theWord:"イラン大統領", retweetCount:2817}, {theWord:"サマー2000シリーズ", retweetCount:0}, {theWord:"広尾", retweetCount:436}, {theWord:"本田翼", retweetCount:8313}, {theWord:"深読み", retweetCount:4269}, {theWord:"妖怪ウォッチ", retweetCount:5}, {theWord:"情報筋", retweetCount:7180}, {theWord:"液晶ディスプレイ", retweetCount:86}, {theWord:"ピッパ・ミドルトン", retweetCount:1255}, {theWord:"経済学", retweetCount:1208}, {theWord:"婚活", retweetCount:16789}, {theWord:"女子アナ", retweetCount:83}, {theWord:"SMAP", retweetCount:19538}, {theWord:"バフェット", retweetCount:466}, {theWord:"専門家", retweetCount:75824}, {theWord:"体験型", retweetCount:1906}, {theWord:"ラストチャンス", retweetCount:121140}, {theWord:"和歌山大", retweetCount:911}, {theWord:"通勤電車", retweetCount:3988}, {theWord:"ロス", retweetCount:1625}, {theWord:"駒の", retweetCount:515}, {theWord:"タカタ", retweetCount:36}, {theWord:"今季初", retweetCount:7164}, {theWord:"電子機器", retweetCount:585}, {theWord:"反政府デモ", retweetCount:762}, {theWord:"市場規模", retweetCount:288}, {theWord:"NY", retweetCount:377126}, {theWord:"鉄道事故", retweetCount:5084}, {theWord:"発達障害", retweetCount:46538}, {theWord:"コーセー", retweetCount:4444}, {theWord:"梅田", retweetCount:110569}, {theWord:"中国", retweetCount:69727}, {theWord:"埼玉", retweetCount:410646}, {theWord:"JR東海", retweetCount:227767}, {theWord:"雄二", retweetCount:2185}, {theWord:"不動産業", retweetCount:12112}, {theWord:"観測機", retweetCount:15345}, {theWord:"竹内結子", retweetCount:1727}, {theWord:"労基署", retweetCount:23756}, {theWord:"ホー", retweetCount:6316}, {theWord:"東洋経済オンライン", retweetCount:165}, {theWord:"EA", retweetCount:8341}, {theWord:"UZA", retweetCount:23153}, {theWord:"ロシア", retweetCount:729687}, {theWord:"環境相", retweetCount:9992}, {theWord:"過剰反応", retweetCount:381747}, {theWord:"イラン", retweetCount:9612}, {theWord:"フランス人", retweetCount:38565}, {theWord:"金融緩和", retweetCount:1261}, {theWord:"相鉄", retweetCount:1203}, {theWord:"ブレ", retweetCount:27000}, {theWord:"鑑定士", retweetCount:24350}, {theWord:"BOSS", retweetCount:273204}, {theWord:"IMF", retweetCount:0}, {theWord:"ポケ", retweetCount:0}, {theWord:"若い人", retweetCount:0}, {theWord:"準々決勝", retweetCount:0}, {theWord:"I-O DATA", retweetCount:0}, {theWord:"国際政治", retweetCount:0}, {theWord:"FRB", retweetCount:0}, {theWord:"NIKE", retweetCount:0}, {theWord:"警視庁", retweetCount:0}, {theWord:"ロイター", retweetCount:0}, {theWord:"FBI", retweetCount:0}, {theWord:"FBI", retweetCount:0}, {theWord:"エコカー", retweetCount:0}, {theWord:"働く男", retweetCount:0}, {theWord:"消費者", retweetCount:0}, {theWord:"旗艦店", retweetCount:0}, {theWord:"サービス業", retweetCount:0}, {theWord:"個人投資家", retweetCount:0}, {theWord:"エルドアン", retweetCount:0}, {theWord:"千葉テレビ", retweetCount:0}, {theWord:"首都圏", retweetCount:0}, {theWord:"蓮舫", retweetCount:0}, {theWord:"大幅続落", retweetCount:0}, {theWord:"字数制限", retweetCount:0}, {theWord:"国際テロ", retweetCount:0}, {theWord:"懐疑的", retweetCount:0}, {theWord:"日本", retweetCount:0}, {theWord:"環境省", retweetCount:0}, {theWord:"北極星", retweetCount:0}, {theWord:"カンボジア", retweetCount:0}, {theWord:"大統領選", retweetCount:0}, {theWord:"EU", retweetCount:0}, {theWord:"財務相", retweetCount:0}, {theWord:"武", retweetCount:0}, {theWord:"EV", retweetCount:0}, {theWord:"日銀", retweetCount:0}, {theWord:"DELI", retweetCount:0}, {theWord:"田母神", retweetCount:0}, {theWord:"スキー場", retweetCount:0}, {theWord:"スリーエフ", retweetCount:0}, {theWord:"創設者", retweetCount:0}, {theWord:"日本銀行", retweetCount:0}, {theWord:"政府広報", retweetCount:0}, {theWord:"米韓", retweetCount:0}, {theWord:"HONZ", retweetCount:0}, {theWord:"国交省", retweetCount:0}, {theWord:"青森山田", retweetCount:0}, {theWord:"下値余地", retweetCount:0}, {theWord:"学生野球", retweetCount:0}, {theWord:"都知事選", retweetCount:0}, {theWord:"受動喫煙", retweetCount:0}, {theWord:"体調不良", retweetCount:0}, {theWord:"バーガー", retweetCount:0}, {theWord:"2016年", retweetCount:0}, {theWord:"本田", retweetCount:0}, {theWord:"シュツットガルト", retweetCount:0}, {theWord:"日本郵政", retweetCount:0}, {theWord:"億万長者", retweetCount:0}, {theWord:"堀ちえみ", retweetCount:0}, {theWord:"近畿財務局", retweetCount:0}, {theWord:"日本株", retweetCount:0}, {theWord:"ラッカ", retweetCount:0}, {theWord:"中谷美紀", retweetCount:0}, {theWord:"ロシア経済", retweetCount:0}, {theWord:"決算説明会", retweetCount:0}, {theWord:"秋山進", retweetCount:0}, {theWord:"通勤時間", retweetCount:0}, {theWord:"三菱自", retweetCount:0}, {theWord:"マクロン", retweetCount:0}, {theWord:"バイロン・ネルソン", retweetCount:0}, {theWord:"浅香光代", retweetCount:0}, {theWord:"官房長官", retweetCount:0}, {theWord:"Halloween", retweetCount:0}, {theWord:"浅野", retweetCount:0}, {theWord:"東武", retweetCount:0}, {theWord:"ミサイル発射", retweetCount:0}, {theWord:"共同店舗", retweetCount:0}, {theWord:"西武", retweetCount:0}, {theWord:"大手私鉄", retweetCount:0}, {theWord:"食物繊維", retweetCount:0}, {theWord:"ガジェット", retweetCount:0}, {theWord:"民主党", retweetCount:0}, {theWord:"軍事衛星", retweetCount:0}, {theWord:"タカ派", retweetCount:0}, {theWord:"SIM", retweetCount:0}, {theWord:"原油価格", retweetCount:0}, {theWord:"電通", retweetCount:0}, {theWord:"早実", retweetCount:0}, {theWord:"株安", retweetCount:0}, {theWord:"共謀罪", retweetCount:0}, {theWord:"アンド", retweetCount:0}, {theWord:"芸能活動", retweetCount:0}, {theWord:"JR", retweetCount:0}, {theWord:"青山", retweetCount:0}, {theWord:"国連", retweetCount:0}, {theWord:"ウィリアム", retweetCount:0}, {theWord:"GDP", retweetCount:0}, {theWord:"南青山", retweetCount:0}, {theWord:"大阪", retweetCount:0}, {theWord:"モテる", retweetCount:0}, {theWord:"名城大", retweetCount:0}, {theWord:"新興国", retweetCount:0}, {theWord:"キキララ", retweetCount:0}, {theWord:"金", retweetCount:0}, {theWord:"旅行会社", retweetCount:0}, {theWord:"メタンハイドレート", retweetCount:0}, {theWord:"井手", retweetCount:0}, {theWord:"マクドナルド", retweetCount:0}, {theWord:"国立科学博物館", retweetCount:0}, {theWord:"バークシャー", retweetCount:0}, {theWord:"成長率", retweetCount:0}, {theWord:"松坂桃李", retweetCount:0}, {theWord:"高齢者", retweetCount:0}, {theWord:"週刊東洋経済", retweetCount:253003}, {theWord:"マイケル・コース", retweetCount:0}, {theWord:"菊地", retweetCount:0}, {theWord:"ドル円", retweetCount:0}, {theWord:"音鼓 -OTOKO-", retweetCount:0}, {theWord:"滋賀", retweetCount:0}, {theWord:"株式市場", retweetCount:0}, {theWord:"資生堂", retweetCount:0}, {theWord:"東野圭吾", retweetCount:0}, {theWord:"投資家", retweetCount:0}, {theWord:"FLYNN", retweetCount:0}, {theWord:"キーエンス", retweetCount:0}, {theWord:"小澤征爾", retweetCount:0}, {theWord:"ダルビッシュ", retweetCount:0}, {theWord:"G1", retweetCount:0}, {theWord:"自動車産業", retweetCount:0}, {theWord:"低インフレ", retweetCount:0}, {theWord:"6月", retweetCount:0}, {theWord:"G2", retweetCount:0}, {theWord:"ワケ", retweetCount:0}, {theWord:"キアヌ・リーヴス", retweetCount:0}, {theWord:"韓国", retweetCount:0}, {theWord:"KIN", retweetCount:0}, {theWord:"欧州", retweetCount:0}, {theWord:"取締役会", retweetCount:0}, {theWord:"国民投票", retweetCount:0}, {theWord:"カリフォルニア", retweetCount:0}, {theWord:"配偶者", retweetCount:0}, {theWord:"AI", retweetCount:0}, {theWord:"特別展", retweetCount:0}, {theWord:"国内政治", retweetCount:0}, {theWord:"傷物語", retweetCount:0}, {theWord:"表現の自由", retweetCount:0}, {theWord:"大西", retweetCount:0}, {theWord:"関東大会", retweetCount:0}, {theWord:"ヘッドマーク", retweetCount:0}, {theWord:"家宅捜索", retweetCount:0}, {theWord:"EU", retweetCount:0}, {theWord:"ウォーターゲート事件", retweetCount:0}, {theWord:"名古屋城", retweetCount:0}, {theWord:"金融政策", retweetCount:0}, {theWord:"ACミラン", retweetCount:0}, {theWord:"インドネシア", retweetCount:0}, {theWord:"イギリス", retweetCount:0}, {theWord:"湘南新宿ライン", retweetCount:0}, {theWord:"消費増税", retweetCount:0}, {theWord:"世論調査", retweetCount:0}, {theWord:"リアル", retweetCount:0}, {theWord:"唯夫", retweetCount:0}, {theWord:"豊", retweetCount:0}, {theWord:"可能性", retweetCount:0}, {theWord:"2015年", retweetCount:0}, {theWord:"欧州諸国", retweetCount:0}, {theWord:"AR", retweetCount:0}, {theWord:"リオ", retweetCount:0}, {theWord:"有罪判決", retweetCount:0}, {theWord:"不動産鑑定士", retweetCount:0}, {theWord:"対象外", retweetCount:0}, {theWord:"エル特急", retweetCount:0}, {theWord:"東京", retweetCount:0}, {theWord:"宅配ピザ", retweetCount:0}, {theWord:"従業員", retweetCount:0}, {theWord:"モスル", retweetCount:0}, {theWord:"札幌2歳S", retweetCount:0}, {theWord:"利用者", retweetCount:0}, {theWord:"関係悪化", retweetCount:0}, {theWord:"LinkedIn", retweetCount:0}, {theWord:"わにとかげぎす", retweetCount:0}, {theWord:"ノアの箱舟", retweetCount:0}, {theWord:"公取委", retweetCount:0}, {theWord:"イタリア", retweetCount:0}, {theWord:"ミャンマー", retweetCount:0}, {theWord:"Oracle", retweetCount:0}, {theWord:"フェンネル", retweetCount:0}, {theWord:"シリコンバレー", retweetCount:0}, {theWord:"保守穏健派", retweetCount:0}, {theWord:"ヨーロッパ", retweetCount:0}, {theWord:"リオ五輪", retweetCount:0}, {theWord:"性同一性障害", retweetCount:0}, {theWord:"国税庁", retweetCount:0}, {theWord:"自民党", retweetCount:0}, {theWord:"長澤まさみ", retweetCount:0}, {theWord:"フリーキック!", retweetCount:0}, {theWord:"江東", retweetCount:0}, {theWord:"証明書", retweetCount:0}, {theWord:"国際エネルギー機関", retweetCount:0}, {theWord:"独", retweetCount:0}, {theWord:"Web", retweetCount:0}, {theWord:"ロハニ", retweetCount:0}, {theWord:"経営者", retweetCount:0}, {theWord:"シャラポワ", retweetCount:0}, {theWord:"喫煙者", retweetCount:0}, {theWord:"自民", retweetCount:0}, {theWord:"清宮", retweetCount:0}, {theWord:"Fess", retweetCount:0}, {theWord:"再生可能エネルギー", retweetCount:0}, {theWord:"回復の兆し", retweetCount:0}, {theWord:"野田稔", retweetCount:0}, {theWord:"依存症", retweetCount:0}, {theWord:"バブル期", retweetCount:0}, {theWord:"評価額", retweetCount:0}, {theWord:"ロッキード", retweetCount:0}, {theWord:"強硬派", retweetCount:0}, {theWord:"ボランチ", retweetCount:0}, {theWord:"下院", retweetCount:0}, {theWord:"サウジアラビア", retweetCount:0}, {theWord:"武装勢力", retweetCount:0}, {theWord:"白岡", retweetCount:0}, {theWord:"May", retweetCount:0}, {theWord:"元夫", retweetCount:0}, {theWord:"ロシア正教会", retweetCount:0}, {theWord:"横浜", retweetCount:0}, {theWord:"ベネズエラ", retweetCount:0}, {theWord:"財務局", retweetCount:0}, {theWord:"ハーバード大", retweetCount:0}, {theWord:"AMO’S STYLE", retweetCount:0}, {theWord:"高城幸司", retweetCount:0}, {theWord:"突然死", retweetCount:0}, {theWord:"無効化", retweetCount:0}, {theWord:"7月", retweetCount:0}, {theWord:"大西英男", retweetCount:0}, {theWord:"大串", retweetCount:0}, {theWord:"東急プラザ", retweetCount:0}, {theWord:"スローフード", retweetCount:0}, {theWord:"眞子さま", retweetCount:0}, {theWord:"東証", retweetCount:0}, {theWord:"キモ", retweetCount:0}, {theWord:"過激派", retweetCount:0}, {theWord:"シンシナティ動物園", retweetCount:0}, {theWord:"フジテレビ", retweetCount:0}}

set bList to sortRecListByLabel(aList, "retweetCount", false) of me –昇順ソート

–リストに入れたレコードを、指定の属性ラベルの値でソート
on sortRecListByLabel(aRecList as list, aLabelStr as string, ascendF as boolean)
  –ListからNSArrayへの型変換
  
set aArray to current application’s NSArray’s arrayWithArray:aRecList
  
  
–ソート
  
set sortDesc to current application’s NSSortDescriptor’s alloc()’s initWithKey:aLabelStr ascending:ascendF
  
set sortDescArray to current application’s NSArray’s arrayWithObjects:sortDesc
  
set sortedArray to aArray’s sortedArrayUsingDescriptors:sortDescArray
  
  
–NSArrayからListに型変換して返す
  
set bList to (sortedArray) as list
  
return bList
end sortRecListByLabel

★Click Here to Open This Script 

Posted in list Record Sort | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

asoc_レコード内の計算(平均)v2

Posted on 2月 7, 2018 by Takaaki Naganoya
AppleScript名:asoc_レコード内の計算(平均)v2
— Created 2014-11-19 by Takaaki Naganoya
— 2014 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set aCompanyRec to {employees:{{age:20, nameStr:"A"}, {age:23, nameStr:"B"}, {age:19, nameStr:"C"}}}

set aDic to current application’s NSDictionary’s dictionaryWithDictionary:aCompanyRec
set aveAge to (aDic’s valueForKeyPath:"employees.@avg.age")
set aveAgeNum to aveAge as real
–> 20.666666666667

★Click Here to Open This Script 

Posted in list Record | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

asoc_レコード内の計算(カウント)v2

Posted on 2月 7, 2018 by Takaaki Naganoya
AppleScript名:asoc_レコード内の計算(カウント)v2
— Created 2014-11-19 by Takaaki Naganoya
— 2014 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set aCompanyRec to {employees:{{age:20, nameStr:"A"}, {age:23, nameStr:"B"}, {age:19, nameStr:"C"}}}

set aDic to current application’s NSDictionary’s dictionaryWithDictionary:aCompanyRec
set aveAge to (aDic’s valueForKeyPath:"employees.@count.age")
set aveAgeNum to aveAge as real
–> 3.0

★Click Here to Open This Script 

Posted in list Record | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

asoc_レコード内の計算(最大)v2

Posted on 2月 7, 2018 by Takaaki Naganoya
AppleScript名:asoc_レコード内の計算(最大)v2
— Created 2014-11-19 by Takaaki Naganoya
— 2014 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set aCompanyRec to {employees:{{age:20, nameStr:"A"}, {age:23, nameStr:"B"}, {age:19, nameStr:"C"}}}

set aDic to current application’s NSDictionary’s dictionaryWithDictionary:aCompanyRec
set aveAge to (aDic’s valueForKeyPath:"employees.@max.age")
set aveAgeNum to aveAge as real
–> 23.0

★Click Here to Open This Script 

Posted in list Record | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

asoc_レコード内の計算(最小)v2

Posted on 2月 7, 2018 by Takaaki Naganoya
AppleScript名:asoc_レコード内の計算(最小)v2
— Created 2014-11-19 by Takaaki Naganoya
— 2014 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set aCompanyRec to {employees:{{age:20, nameStr:"A"}, {age:23, nameStr:"B"}, {age:19, nameStr:"C"}}}

set aDic to current application’s NSDictionary’s dictionaryWithDictionary:aCompanyRec
set aveAge to (aDic’s valueForKeyPath:"employees.@min.age")
set aveAgeNum to aveAge as real
–> 19.0

★Click Here to Open This Script 

Posted in list Record | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

asoc_レコード内の計算(合計)v2

Posted on 2月 7, 2018 by Takaaki Naganoya
AppleScript名:asoc_レコード内の計算(合計)v2
— Created 2014-11-19 by Takaaki Naganoya
— 2014 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set aCompanyRec to {employees:{{age:20, nameStr:"A"}, {age:23, nameStr:"B"}, {age:19, nameStr:"C"}}}

set aDic to current application’s NSDictionary’s dictionaryWithDictionary:aCompanyRec
set aveAge to (aDic’s valueForKeyPath:"employees.@sum.age")
set aveAgeNum to aveAge as real
–> 62.0

★Click Here to Open This Script 

Posted in list Record | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

指定リストの次元を取得する v3

Posted on 2月 7, 2018 by Takaaki Naganoya

任意のリスト(配列)変数の次元を取得するAppleScriptです。

手軽に多次元配列を宣言してアクセスできるAppleScriptですが、作成した配列変数の次元を確認する方法は用意されていません。

そこで、指定リスト(配列)の次元数を取得するものを書いてみました。

日常的に2次元配列とか3次元配列は使いますが、4次元配列まではあまり使ったことがありません。実用上は3次元配列ぐらいでしょうか。

たまに2次元配列が使えない言語とか、実用上は2次元配列ぐらいまでしか直接宣言できない言語があって驚かされます。

AppleScript名:指定リストの次元を取得する v3
use AppleScript version "2.4"
use scripting additions

set aList to {{1, 2, 3}, {4, 5, 6}} –2D List
set aDim to getDimension given tArray:aList
–> 2

set bList to {{{1, 2}, {2, 3}, {3, 4}}, {{1, 2}, {2, 3}, {3, 4}}, {{1, 2}, {2, 3}, {3, 4}}} –3D List
set bDim to getDimension given tArray:bList
–> 3

set cList to {1, 2, 3, 4, 5, 6} –1D List
set cDim to getDimension given tArray:cList
–> 1

set dList to {{{{1, 2}, {2, 3}}, {{1, 2}, {2, 3}}, {{1, 2}, {2, 3}}}} –4D List
set dDim to getDimension given tArray:dList
–> 4

–指定Listの次元を再帰で取得する
on getDimension given tArray:aList as list : {}, curDim:aNum as integer : 1
  
  
set anItem to contents of first item of aList
  
set aClass to class of anItem
  
  
if aClass = list then
    set aNum to aNum + 1
    
set aRes to getDimension given tArray:anItem, curDim:aNum
  else
    return aNum
  end if
  
end getDimension

★Click Here to Open This Script 

Posted in list recursive call | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

Post navigation

  • Older posts
  • Newer posts

電子書籍(PDF)をオンラインストアで販売中!

Google Search

Popular posts

  • 開発機としてM2 Mac miniが来たのでガチレビュー
  • macOS 15, Sequoia
  • Pages本執筆中に、2つの書類モード切り替えに気がついた
  • Numbersで選択範囲のセルの前後の空白を削除
  • メキシカンハットの描画
  • Pixelmator Pro v3.6.4でAppleScriptからの操作時の挙動に違和感が
  • AppleScriptによる並列処理
  • Safariで「プロファイル」機能を使うとAppleScriptの処理に影響
  • macOS 15でも変化したText to Speech環境
  • AppleScript入門③AppleScriptを使った「自動化」とは?
  • デフォルトインストールされたフォント名を取得するAppleScript
  • macOS 15 リモートApple Eventsにバグ?
  • 【続報】macOS 15.5で特定ファイル名パターンのfileをaliasにcastすると100%クラッシュするバグ
  • AppleScript入門① AppleScriptってなんだろう?
  • Script Debuggerの開発と販売が2025年に終了
  • macOS 14で変更になったOSバージョン取得APIの返り値
  • NSObjectのクラス名を取得 v2.1
  • 有害ではなくなっていたSpaces
  • macOS 15:スクリプトエディタのAppleScript用語辞書を確認できない
  • Xcode上のAppleScriptObjCのプログラムから、Xcodeのログ欄へのメッセージ出力を実行

Tags

10.11savvy (1101) 10.12savvy (1242) 10.13savvy (1391) 10.14savvy (587) 10.15savvy (438) 11.0savvy (283) 12.0savvy (212) 13.0savvy (194) 14.0savvy (147) 15.0savvy (135) CotEditor (66) Finder (51) iTunes (19) Keynote (119) NSAlert (61) NSArray (51) NSBitmapImageRep (20) NSBundle (20) NSButton (34) NSColor (53) NSDictionary (28) NSFileManager (23) NSFont (21) NSImage (41) NSJSONSerialization (21) NSMutableArray (63) NSMutableDictionary (22) NSPredicate (36) NSRunningApplication (56) NSScreen (30) NSScrollView (22) NSString (119) NSURL (98) NSURLRequest (23) NSUTF8StringEncoding (30) NSView (33) NSWorkspace (20) Numbers (76) Pages (55) Safari (44) Script Editor (27) WKUserContentController (21) WKUserScript (20) WKWebView (23) WKWebViewConfiguration (22)

カテゴリー

  • 2D Bin Packing
  • 3D
  • AirDrop
  • AirPlay
  • Animation
  • AppleScript Application on Xcode
  • Beginner
  • Benchmark
  • beta
  • Bluetooth
  • Books
  • boolean
  • bounds
  • Bug
  • Calendar
  • call by reference
  • check sum
  • Clipboard
  • Cocoa-AppleScript Applet
  • Code Sign
  • Color
  • Custom Class
  • date
  • dialog
  • diff
  • drive
  • Droplet
  • exif
  • file
  • File path
  • filter
  • folder
  • Font
  • Font
  • GAME
  • geolocation
  • GUI
  • GUI Scripting
  • Hex
  • History
  • How To
  • iCloud
  • Icon
  • Image
  • Input Method
  • Internet
  • iOS App
  • JavaScript
  • JSON
  • JXA
  • Keychain
  • Keychain
  • Language
  • Library
  • list
  • Locale
  • Localize
  • Machine Learning
  • Map
  • Markdown
  • Menu
  • Metadata
  • MIDI
  • MIME
  • Natural Language Processing
  • Network
  • news
  • Noification
  • Notarization
  • Number
  • Object control
  • OCR
  • OSA
  • parallel processing
  • PDF
  • Peripheral
  • process
  • PRODUCTS
  • QR Code
  • Raw AppleEvent Code
  • Record
  • rectangle
  • recursive call
  • regexp
  • Release
  • Remote Control
  • Require Control-Command-R to run
  • REST API
  • Review
  • RTF
  • Sandbox
  • Screen Saver
  • Script Libraries
  • sdef
  • search
  • Security
  • selection
  • shell script
  • Shortcuts Workflow
  • Sort
  • Sound
  • Spellchecker
  • Spotlight
  • SVG
  • System
  • Tag
  • Telephony
  • Text
  • Text to Speech
  • timezone
  • Tools
  • Update
  • URL
  • UTI
  • Web Contents Control
  • WiFi
  • XML
  • XML-RPC
  • イベント(Event)
  • 未分類

アーカイブ

  • 2025年6月
  • 2025年5月
  • 2025年4月
  • 2025年3月
  • 2025年2月
  • 2025年1月
  • 2024年12月
  • 2024年11月
  • 2024年10月
  • 2024年9月
  • 2024年8月
  • 2024年7月
  • 2024年6月
  • 2024年5月
  • 2024年4月
  • 2024年3月
  • 2024年2月
  • 2024年1月
  • 2023年12月
  • 2023年11月
  • 2023年10月
  • 2023年9月
  • 2023年8月
  • 2023年7月
  • 2023年6月
  • 2023年5月
  • 2023年4月
  • 2023年3月
  • 2023年2月
  • 2023年1月
  • 2022年12月
  • 2022年11月
  • 2022年10月
  • 2022年9月
  • 2022年8月
  • 2022年7月
  • 2022年6月
  • 2022年5月
  • 2022年4月
  • 2022年3月
  • 2022年2月
  • 2022年1月
  • 2021年12月
  • 2021年11月
  • 2021年10月
  • 2021年9月
  • 2021年8月
  • 2021年7月
  • 2021年6月
  • 2021年5月
  • 2021年4月
  • 2021年3月
  • 2021年2月
  • 2021年1月
  • 2020年12月
  • 2020年11月
  • 2020年10月
  • 2020年9月
  • 2020年8月
  • 2020年7月
  • 2020年6月
  • 2020年5月
  • 2020年4月
  • 2020年3月
  • 2020年2月
  • 2020年1月
  • 2019年12月
  • 2019年11月
  • 2019年10月
  • 2019年9月
  • 2019年8月
  • 2019年7月
  • 2019年6月
  • 2019年5月
  • 2019年4月
  • 2019年3月
  • 2019年2月
  • 2019年1月
  • 2018年12月
  • 2018年11月
  • 2018年10月
  • 2018年9月
  • 2018年8月
  • 2018年7月
  • 2018年6月
  • 2018年5月
  • 2018年4月
  • 2018年3月
  • 2018年2月

https://piyomarusoft.booth.pm/items/301502

メタ情報

  • ログイン
  • 投稿フィード
  • コメントフィード
  • WordPress.org

Forum Posts

  • 人気のトピック
  • 返信がないトピック

メタ情報

  • ログイン
  • 投稿フィード
  • コメントフィード
  • WordPress.org
Proudly powered by WordPress
Theme: Flint by Star Verte LLC