miでブログの下書きをしているが、いろいろ不便なのでアップルスクリプトで補完する。やはり定型作業はスクリプトに限るな。
その1:TABを に変換。
プログラム・コードでインデントにTAB文字を使用するが、ブログ(ブラウザ?)では無効になってしまう。そのため選択文字列のTAB文字をスペース( )4文字に変換するスクリプトを作成。
property tab : ASCII character 9
on run
tell application "mi"
set theSelectText to content of selection object 1 of front document
set theRepStr to replaceText(theSelectText, tab, " ") of me
set content of selection object 1 of document 1 to (theRepStr as string)
end tell
end run
(*文字列の置換*)
on replaceText(theText, serchStr, replaceStr)
set tmp to AppleScript's text item delimiters
set AppleScript's text item delimiters to serchStr
set theList to every text item of theText
set AppleScript's text item delimiters to replaceStr
set theText to theList as string
set AppleScript's text item delimiters to tmp
return theText
end replaceText
その2:CSV形式をHTMLのTableに変換
選択文字列のタブ区切りCSV形式をHTMLのTableタグに変換するアップルスクリプト。こんな感じで変換される。(ヘッダは手で書き換えているが)
最初の1行目は、無条件にヘッダ行として扱うので注意。
ジャンル毎の平均マイレート順位
| 順位 | ジャンル名 | 曲数 | 再生回数 | 平均マイレート |
|---|
| 1 | Jazz | 1 | 24 | 100 |
| 1 | Serenade | 1 | 26 | 100 |
| 1 | Soundtrack | 2 | 43 | 100 |
| 4 | Electronica/Dance | 2 | 44 | 90 |
| 5 | Folk | 5 | 127 | 88 |
| 6 | Rock | 15 | 350 | 85 |
MacOSX 10.4.7、mi 2.1.6で動作確認。例によって、文字列の置換はAppleScript PARKよりパクったものを使用。
property maccr : ASCII character 13
(* miでの選択文字列(タブ区切りのCSV形式)をHtmlのTableに変換するスクリプト *)
on run
tell application "mi"
set theSelectText to content of selection object 1 of front document
set theRepStr to "<TABLE BORDER>" & maccr & "<CAPTION></CAPTION>" & maccr
-- テーブルヘッダ追加
set theParag to paragraph 1 of theSelectText
set theRepStr to theRepStr & "<TR><TH>" & replaceText(theParag, tab, "</TH><TH>") of me & "</TH></TR>"
-- テーブルデータ追加
repeat with theParag in items 2 thru end of (paragraphs of theSelectText)
if theParag as string is not "" then
set theRepStr to theRepStr & "<TR><TD>" & replaceText(theParag, tab, "</TD><TD>") of me & "</TD></TR>" & maccr
end if
end repeat
set theRepStr to theRepStr & "</TABLE>" & maccr
--書き出し
set content of selection object 1 of document 1 to (theRepStr as string)
end tell
end run
(*文字列の置換*)
on replaceText(theText, serchStr, replaceStr)
set tmp to AppleScript's text item delimiters
set AppleScript's text item delimiters to serchStr
set theList to every text item of theText
set AppleScript's text item delimiters to replaceStr
set theText to theList as string
set AppleScript's text item delimiters to tmp
return theText
end replaceText
最近のコメント