JeditXでテキストに行番号を発生させる
http://mottainaidtp.seesaa.net/article/434495744.html
に
反応
ただやってもナニなので
行番号にゼロサプレス処理を付加出来るようにした
(*
add_line_no.scpt
テキストデータに行番号を付加してその後にタブを挿入し
タブ区切りテキスト形式に変換した出力を
テキストエディタに表示します。
JeditXでテキストに行番号を発生させる
http://mottainaidtp.seesaa.net/article/434495744.html
の
アップルスクリプト版
*)
----設定項目
(*
1ならゼロサプレス有り
ソレ以外ならゼロサプレス無し
*)
propertypreZeroSup : 2
------------------------------------ ダブルクリックの始まり
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
----ゼロサプレスのサブルーチン
todoZeroSuppress(n, totalno)
iftotalno< 10 then
returnnastext
elseiftotalno< 100 then
ifn< 10 then
return"0" & n
else
returnnastext
endif
elseiftotalno< 1000 then
ifn< 10 then
return"00" & n
elseifn< 100 then
return"0" & n
else
returnnastext
endif
elseiftotalno< 10000 then
ifn< 10 then
return"000" & n
elseifn< 100 then
return"00" & n
elseifn< 1000 then
return"0" & n
else
returnnastext
endif
endif
enddoZeroSuppress
ソースはこちらに保存
https://github.com/force4u/add_line_no.scpt