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ハンドラをお借りました。ありがとうございます。
ApppleScriptを触る(1)
AppleScriptを初めて触って、幾つか使った基本型をメモする.
変数宣言
text(文字列)
set a to "Hello Word!" as text
- 変数aにHello Word!を代入できる
as test
は省略できる
number(数字)
set a to 1234 as number
- 変数aに数値1234を代入できる
as number
は省略できる
list(配列)
set a to {123,456,789} as list
- 配列変数aに数値1234,456,789を代入できる
as list
は省略できる- 文字列も
set a to {"Hello","World","!"}
とすることでできる
表示する
イベントログに表示する
set a to "Hello Word!" log a
- 結果
(*Hello Word!*)
set a to {123,456,789} as list log a
- 結果
(*123, 456, 789*)
- カンマ区切りで表示された
set a to {"Hello","World","!"} log a
- 結果
(*Hello, World, !*)
- 文字列もいける
結合
set a to {"Hello", "World", "!"} set b to {"こんにちは", "せかい", "!"} log a & b
- 結果
(*Hello, World, !, こんにちは, せかい, !*)
&
で変数、文字列を繋げることができる
ダイアログを表示する
set a to "Hello World !" set b to "こんにちは、せかい!" display dialog a & b
結果
表示できた
set a to {"Hello", "World", "!"} set b to {"こんにちは", "せかい", "!"} display dialog a & return & b
結果
文字列のリストを表示することはできなかった
display dialog
命令がlist型をstring型に変換できないために起こるらしい
set a to {"Hello", "World", "!"} set b to {"こんにちは", "せかい", "!"} display dialog a & return & b as text
結果
as text
をつけることで表示することができた
雑感
今回はAppleScriptの基本事項を試してみた
簡単にダイアログを出せて感動した
この簡単さはApple Scriptならではなのでは...
Apple Scriptは他のMacアプリケーションと連携できるらしいのでそれも試してみたい
参考にしたページ
- life log: AppleScript 最速基本文法マスター
- 基本事項がまとまっていてよかったです
- 鳶嶋工房 / AppleScript / Tips / 暗黙の型変換
- AppleScript特有の型変換について納得することができます
ありがとうございます
Vagrant導入メモ
準備
Vagrant と VirtualBox サイトからダウンロードしてインストール
VagantをインストールするとVagrantフォルダが出来るのでそこに今回のBox用のフォルダlocaldev
を作る。
vagrant boxをつくる
BoxファイルをVagrantbox.esから落としてくる。
今回はcentosでやったので
$ vagrant box add centos64box http://developer.nrel.gov/downloads/vagrant-boxes/CentOS-6.4-x86_64-v20130731.box
とした。
centos64boxがBoxの名前。
initする
$ vagrant init centos64box
Vagrantfileを編集
$ vi Vagrantfile
でconfig.vm.network :private_network, ip: “192.168.33.10”
をアンコメント
起動
$ vagrant up
で起動
sshログイン
$ vagrant ssh
お手本にしました。
ローカル開発環境の構築 (全13回) - プログラミングならドットインストール
ありがとうございます!