SSブログ

画像付きリッチテキストを、HTMLに変換してWebで表示できるようにする [AppleScript辞書はつくれるか?]

リッチテキスト(.rtf)や、画像付きリッチテキスト (.rtfd)を、HTML形式に変換します。

まぁ、Shellスクリプトの『textutil -convert html 』を使うんですけどね。

これを使うと一発で変換してくれるんだけど、少々変更が必要になってくる。

・画像付きだとその画像のアドレスが完全ではないので修正している

・バックグラウンドの色に対応できない。(もともとテキストエディターだと背景色って変更できたかな?)-未解決-

など

 

(* 入力ファイル(複数選択可) *)

set objList to choose file with multiple selections allowed

if objList = false then return "中止"

if (class of objList) ≠ list then set objList to objList as list

(* 出力フォルダ *)

set folderName to "RtfdToHTML"

set outputFolder to ((path to desktop folder) as text) & folderName

tell application "Finder"

try

make new folder at desktop with properties {name:folderName}

end try

try

make new folder at folder outputFolder with properties {name:"img"}

end try

try

get name of folder "img" of folder outputFolder

on error

log "make new folder Err. "

return false

end try

end tell

(* 変換 *)

repeat with aObj in objList

--ファイルを取得

tell application "Finder" to set name1 to name of aObj

set thePath to POSIX path of (aObj as text)

--名前からスペースなどを除去

set name2 to "_" & (text 1 thru ((offset in name1 of ".") - 1) of name1) & "_"

repeat 10 times

set c to offset in name2 of " "

if c = 0 then exit repeat

set name2 to (text 1 thru (c - 1) of name2) & "_" & (text (c + 1) thru -1 of name2)

end repeat

repeat 10 times

set c to offset in name2 of "'"

if c = 0 then exit repeat

set name2 to (text 1 thru (c - 1) of name2) & "_" & (text (c + 1) thru -1 of name2)

end repeat

set name2 to text 2 thru -2 of name2

--HTMLへ変換

do shell script ("textutil -convert html \"" & thePath & "\" -output $HOME/Desktop/" & folderName & "/" & name2 & ".html ;")

--画像を探し、あれば処理

try

set imageList to do shell script ("du -a $HOME/Desktop/" & folderName & "/ | cut -f2- | grep -e .png -e .jpg;")

on error

set imageList to ""

end try

if imageList ≠ "" then

tell application "Finder"

repeat with obj in (paragraphs of imageList)

set obj to (obj as POSIX file) as text

set name3 to name of file obj

move obj to folder (folderName & ":img:") of desktop

end repeat

end tell

end if

set aSource to do shell script ("cat \"$HOME/Desktop/" & folderName & "/" & name2 & ".html\" ;")

repeat 10000 times

set c to offset in aSource of "file:///"

if c = 0 then exit repeat

set aSource to (text 1 thru (c - 1) of aSource) & "./img/" & (text (c + 8) thru -1 of aSource)

end repeat

--最終HTMLファイルの保存

set locationURL to ((path to desktop folder) as text) & folderName & ":" & name2 & ".html"

try

set fn to open for access locationURL with write permission

set eof of fn to 0

write aSource to fn as «class utf8»

end try

try

close access fn

end try

end repeat

 

"End"

 

 

 

下記のファイルを変換するとして

ss1.jpg

内容はこんな感じで、画像付きのリッチテキストをテストで用意した。

ss2.jpg

スクリプトを実行して、ダイアログで先程のファイルを選ぶと、デスクトップに変換されたファイルが現れます。

ss3.jpg

 

 

Safariで開くとちゃんと表示されます。

ss4.jpg

 

とりあえず、必要になったので作ってみたというお話です。

 

 


nice!(0)  コメント(0) 
共通テーマ:パソコン・インターネット

nice! 0

コメント 0

コメントを書く

お名前:
URL:
コメント:
画像認証:
下の画像に表示されている文字を入力してください。

この広告は前回の更新から一定期間経過したブログに表示されています。更新すると自動で解除されます。