ゼロサプレスの処理を変更した
桁数を文字列として文字数を数える事で取得し
処理する桁数での制限を無くした
(*
add_line_no.scpt
20160303 初回作成
20160305 ゼロサプレス処理変更
テキストデータに行番号を付加してその後にタブを挿入し
タブ区切りテキスト形式に変換した出力を
テキストエディタに表示します。
JeditXでテキストに行番号を発生させる
http://mottainaidtp.seesaa.net/article/434495744.html
の
アップルスクリプト版
*)
----設定項目
(*
1ならゼロサプレス有り
ソレ以外ならゼロサプレス無し
*)
propertypreZeroSup : 1
------------------------------------ ダブルクリックの始まり
onrun
------------プロンプトの文言改行が使えます\nを入れます
settheWithPromptto"テキストデーターにラインNO(行番号+タブ)を挿入します"
------------ファイル選択ダイアログのデフォルトのディレクトリ
tellapplication"Finder"
---set theDefaultLocation to (path to desktop from user domain) as alias
settheDefaultLocationto (containerof (path tome)) asalias
endtell
------------Uniform Type Identifier指定
settheFileTypeListto"public.plain-text,com.apple.traditional-mac-plain-text" astext
------------↑のファイルタイプをリスト形式に整形する
setAppleScript's text item delimitersto {","}
settheFileTypeListtoeverytext itemoftheFileTypeList
------------ダイアログを出して選択されたファイルは「open」に渡す
open (choose filedefault locationtheDefaultLocation¬
with prompttheWithPrompt¬
of typetheFileTypeList¬
invisiblestrue¬
withoutmultiple selections allowedandshowing package contents)
endrun
onopenDropObj
---ファイルのエイリアスを取得
settheFileAliastoDropObjasalias
------選択したファイルのUNIXパスを取得
settheUnixPasstoPOSIX pathoftheFileAliasastext
-----ファイルを読み込む
settheDatato (do shell script"cat '" & theUnixPass& "'") as«class utf8»
---リスト用の区切り文字
setAppleScript's text item delimitersto {"\r"}
---読み込んだデータをリスト形式で格納
setretListValto (everytext itemoftheData) aslist
---行毎データ初期化
settheDataListLineto"" astext
---アウトプットデータ初期化
settheOutPutto"" astext
-----読み込んだテキストの行数を数える
setnumListLinetocountofretListVal
-----行数カウンタ初期化
setnumLineto 1 asnumber
----繰り返しの始まり
repeatnumListLinetimes
---1つ読み込む
settheDataListLineto (itemnumLineofretListVal) astext
---ゼロサプレスの有無
ifpreZeroSupis 1 then
----ゼロサプレスあり
settheZeroSupLineNotodoZeroSuppress(numLine, numListLine)
else
----ゼロサプレス無し
settheZeroSupLineNotonumLineastext
endif
---出力テキスト整形
settheOutPuttotheOutPut& theZeroSupLineNo& "\t" & theDataListLine& "\r"
---カウントアップ
setnumLineto (numLine + 1) asnumber
---リピートのおわり
endrepeat
---結果をテキストエディタで開く
tellapplication"TextEdit" tolaunch
tellapplication"TextEdit"
makenewdocumentwith properties {text:theOutPut}
endtell
tellapplication"TextEdit" toactivate
endopen
----ゼロサプレスのサブルーチン
#nに処理する値
#totalnoに最大数
#ゼロサプレス処理をします
todoZeroSuppress(n, totalno)
---戻り値の初期化
settheSupZeroto"" astext
---最大数の桁数を数える
settheCntTotalto (countof (totalnoastext)) asnumber
---処理する値の桁数を数える
settheCntNto (countof (nastext)) asnumber
---差分を求める
setnumZeoCntto ((theCntTotal) - (theCntN)) asnumber
---桁数の差だけ処理する
repeatnumZeoCnttimes
---戻り値にゼロを追加
settheSupZeroto (theSupZero& "0") astext
endrepeat
---戻り値を返す
return (theSupZero& n) astext
enddoZeroSuppress