ゆっくり技術ノート!

いつかきっとできるだろうよ

ApppleScriptを触る(2)

AppleScriptを使ってカンマで区切られているテキストをレコードに読み込むハンドラを書いてみた

与えるテキスト

,item-A,item-B,item-C,item-A,item-B,item-C,item-A,item-B,item-C,item-A,item-B,item-C,

こんな感じで3種類の文字列をカンマで区切ります これを適当な場所にtest.txtという名前で保存します

プログラム

-- ファイルの場所を指定
set dpath to "/Users/UserName/Documents/"

set theRecode to read_log(dpath)

-----------------------------------------------
on read_log(dpath)
    --logファイルのパスを作成
    set fileName to "test.txt"
    set readFile to dpath & fileName
    
    --ファイルを文字列として読み込む
    open for access readFile without write permission
    set myContents to read readFile as «class utf8»
    close access readFile
    get myContents
    
    --カンマ区切り文字列をリストに代入
    set myList to read_cvs(myContents)
    
    set myListNo to count myList
    
    set recordNo to (myListNo / 3)
    (*
   recodeNo回リピートする
   値の代入はmyListNoを使う
   1回リピートするごとにcounterを3プラスする
   
   *)
    
    set counter to 1
    
    set theRecode to {}
    
    repeat recordNo times
        
        set a to item (counter + 0) of myList --myListのi+0個目をaに代入
        set b to item (counter + 1) of myList --myListのi+1個目をbに代入
        set c to item (counter + 2) of myList --myListのi+2個目をcに代入
        
        copy {aName:a, bName:b, cName:c} to the end of theRecode
        
        set counter to counter + 3
    end repeat
    
    return theRecode
    
end read_log



--カンマ区切りテキストをリストで返す
-----------------------------------------------

on read_cvs(source_text)
    set comma_p to findText(source_text, ",") as list --カンマの場所を探索
    --カンマの個数を取得
    set commas_no to count comma_p
    --cvsコンテンツを格納するリストを作成
    set cvs to {}
    --カンマの個数回ループ
    repeat with i from 1 to (commas_no - 1)
        set s to (item i of comma_p) --i個目のcomma_p要素を代入
        set e to (item (i + 1) of comma_p) --i+1個目のcomma_p要素を代入
        
        set cvs_i to text s thru e of source_text --i個目のcvsコンテンツを切り出す
        set cvs_i to text 2 thru -2 of cvs_i --カンマを削除
        
        copy cvs_i to the end of cvs --cvsリストの末尾にcsv_iを追加
    end repeat
    
    return cvs
end read_cvs






--function by 鳶嶋工房

--文字列検索
on findText(theText, serchStr)
    set tmp to AppleScript's text item delimiters
    set AppleScript's text item delimiters to serchStr
    set theText to every text item of theText
    set theList to {}
    set x to 0
    set delNum to number of serchStr
    repeat with curItem in theText
        set x to x + (number of curItem)
        set end of theList to x + 1
        set x to x + delNum
    end repeat
    set AppleScript's text item delimiters to tmp
    if (number of theList) = 1 then return {}
    return items 1 thru -2 of theList
end findText

結果

{{aName:"item-A", bName:"item-B", cName:"item-C"}, {aName:"item-A", bName:"item-B", cName:"item-C"}, {aName:"item-A", bName:"item-B", cName:"item-C"}, {aName:"item-A", bName:"item-B", cName:"item-C"}}

ちなみにこれらを取り出す時は

log aName of item 4 of theRecode

とすれば4番目のaNameが取り出せる

雑感

  • だいぶAppleScriptになれてきた
  • 途中から変数の付け方かえたのでとても汚くなった
  • でも、レコードを使えばリストで無理矢理やりくりするより遥かに分かりやすいと思う

    参考にしたページ

  • 鳶嶋工房 / AppleScript / Tips / 文字列の検索・置換
    • findTextハンドラをお借りました。ありがとうございます。