PageSpinnerのスクリプティング

1997.9.5


 PageSpinnerをより便利にするためのスクリプティングを解説します。

 まず、選択されたテキストの操作について。

 現在最前面にあるドキュメントのテキストを取り出すには、次のように記述します。

tell front document of application "PageSpinner"
      set theStr to selected text
end tell

 これでtheStrに現在選択されているテキストを格納することができます。またtheStrを最前面のドキュメントの選択部分に書き戻すときは、

tell front document of application "PageSpinner"
      set selected text to theStr
end tell

 とするだけです。基本的に他のスクリプト対応エディタと同じですね。

 スクリプトで文字列を選択部分に書き込んだ場合、Tagなどの書式が設定されません。このため、文字列を変更するような操作をしたときには「restyle」命令を行って下さい。

 以上のことを統合すると、次のようになります。

on run
      tell front document of application "PageSpinner"
            set theStr to selected text
            set theStr to main(theStr) of me
            set selected text to theStr
            restyle
      end tell
end run

on main(theStr) (theStrを自由に変更) return theStr end main

 私のPageSpinnerスクリプトの多くは、基本的にこの例を変化させただけのものです。

 例えば、拙作「TEXTをHTML形式に変換」のスクリプトは、次の通り。

on run
      tell front document of application "PageSpinner"
            set theStr to selected text
            set theStr to main(theStr) of me
            set selected text to theStr
            restyle
      end tell
end run

on main(theStr) set theStr to FindAndReplace("&", "&amp;", theStr) set theStr to FindAndReplace("<", "&lt;", theStr) set theStr to FindAndReplace(">", "&gt;", theStr) set theStr to FindAndReplace(return, "<BR>" & return, theStr) set theStr to FindAndReplace("<BR>" & return & "<BR>", return & "<P>", theStr) set theStr to FindAndReplace(tab, " ", theStr) return theStr end main

on FindAndReplace(pre, post, TargetStr) set CurDelim to AppleScript's text item delimiters set AppleScript's text item delimiters to pre set buf to text items of TargetStr set AppleScript's text item delimiters to post set buf to buf as string set AppleScript's text item delimiters to CurDelim return buf end FindAndReplace


 「FindAndReplace」は文字列の置換を行うためのサブルーチンです。ここでは、選択されている文字列theStrのなかの特殊文字などを、全て対応する文字列に変換しています。やっていること自体は簡単ですよね。

 尚、最前面のドキュメントに含まれる文字列全部を取り出したい場合は、先に「select all」で文章を全て選択しておくといいでしょう。

 ところで、PageSpinnerに添付されているサンプルスクリプトのいくつかは、PageSpinnerの機能を使おうとするあまり不自然なスクリプティングになっています。参考にする場合は注意して下さい。


 次に、ファイルパスを扱ったスクリプティングについて述べます。

 PageSpinnerはHTMLエディタですから、ハイパーリンクの記述には相対パスが必要になることもあります。PageSpinner本来の機能でも画像やHTML文書を簡単にリンクすることができますが、頻繁に使う画像を毎回ダイアログで選択するのは面倒ですねよ。こういう部分をスクリプティングしようとすると、最前面のドキュメントから見た既存のファイルの相対パスを割り出す必要が出てくるわけです。

 まず、最前面のドキュメントが存在するディレクトリは、次のようにして得ることができます。

tell front document of application "PageSpinner"
      set PageFolder to page folder
end tell

 このディレクトリと目的のファイルのフルパスさえ分れば、後は相対パスを計算するだけ…なのですが、これが案外面倒なので、ここではそのためのサブルーチン「ABStoRELpath2」の解説は行いません。

 次の例は、あらかじめpropertyで指定してある画像ファイルをインライン画像としてHTMLファイルに埋め込むスクリプトです。

property pNew : "Macintosh HD:HTML:ICONS:the Icon" --画像のパスを入力
property pImageHead : "<IMG SRC=\""
property pImageFoot : "\">" --イメージタグのオプションは自分で書き込む

on run tell front document of application "PageSpinner" activate set PageFolder to page folder tell me set IMGTag to pImageHead & ABStoRELpath2(PageFolder, pNew) & pImageFoot end tell set selected text to IMGTag restyle end tell end run

on ABStoRELpath2(BasePath, FilePath) set CurDelim to AppleScript's text item delimiters set AppleScript's text item delimiters to "" set BasePath to BasePath as string set FilePath to FilePath as string set AppleScript's text item delimiters to ":" set BasePath to text items of BasePath set FilePath to text items of FilePath set baseLength to length of BasePath set fileLength to length of FilePath if baseLength ウ fileLength then set RepNum to fileLength else set RepNum to baseLength end if repeat with i from 1 to RepNum if (item i of BasePath) is not (item i of FilePath) then set baseLimit to baseLength - i + 1 set Header to "" repeat with ii from 1 to baseLimit set Header to Header & "../" end repeat set RelPath to items i thru end of FilePath exit repeat end if if i is RepNum then set RelPath to items (RepNum + 1) thru end of FilePath set Header to "" end if end repeat set AppleScript's text item delimiters to "/" set RelPath to RelPath as string set RelPath to Header & RelPath set AppleScript's text item delimiters to CurDelim return RelPath end ABStoRELpath2


 パスの計算は非常に面倒ですが、それ以外は最初に紹介した基本部分そのままです。相対パスはさまざまな部分に使われますので、覚えておいて損はないと思いますよ。


 ということで、PageSpinnerをより便利にするためのスクリプティングテクニックでした。よく考えてみたらPageSpinnerそのものよりHTMLに関するスクリプティングになってしまいましたが、PageSpinnerを使う人は確実にHTMLを作る人でしょうから、まあいい事にしておいてください。

 尚、PageSpinnerのためのスクリプト集「Scripts for PageSpinner」も併せてご利用下さい。


アプリケーション別スクリプティングに戻る
AppleScriptのページに戻る
このホームページに関するお問い合わせは、karino@drycarbon.comまで。