xml構造をリアルタイムでチェックする
InDesignCS5からeventにafterSelectionChangedが追加された。
例えばテキストを触って選択状態が変更すると、eventを発行する。
CS2の頃からやってみたかったことがあった。
IDでxmlを扱うとき、オペレータが誤ってxml構造を壊してしまうことがある。
オペ中にリアルタイムで構造チェックをしたかった。
(積極的にxmlの組版をしてこなかった理由もこんなところにある)
このeventを使えば、構造をオペ中にチェックできるのではないか? と思った次第。
サンプルのxmlはこんな感じ。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE object SYSTEM "sample.dtd"> <object> <document> <title>ハリー・ポッターと賢者の石</title> <text>Harry Potter and the Philosopher's Stone .</text> <text>日本語版単行本 ISBN 4-915512-37-1(1999年12月1日発売)</text> <text>日本語版携帯版 ISBN 4-915512-49-5(2003年発売)</text> </document> <document> <title>ハリー・ポッターと秘密の部屋</title> <text>Harry Potter and the Chamber of Secrets</text> <text>日本語版単行本 ISBN 4-915512-39-8(2000年発売)</text> <text>日本語版携帯版 ISBN 4-915512-54-1(2004年発売)</text> </document> </object> |
dtdはこんな感じ。
1 2 3 4 5 | <!--sample.dtd--> <!ELEMENT object (document+)> <!ELEMENT document (title , text+)> <!ELEMENT title (#PCDATA)> <!ELEMENT text (#PCDATA)> |
タイトルは1つ無くてはいけない。ここを消すとアラートを出すようにしたい。
まず、イベントを発生させるスクリプトは、
1 2 3 | tell application "Adobe InDesign CS5" make event listener with properties {event type:"afterSelectionChanged",handler:"SnowLeopard:Users:yukio:Desktop:xmlValidationCheck.applescript"} end tell |
テストなのでhandlerの場所指定は、デスクトップ(SnowLeopard:Users:yukio:Desktop:)にxmlValidationCheck.applescriptという名称で置いています。
そのhandlerのスクリプトは、
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | tell application "Adobe InDesign CS5" tell document 1 set validErrors to validate XML element 1 if (length of validErrors) > 0 then set errMessage to "" set errCnt to 0 repeat with anvalidError in validErrors set errCnt to errCnt + 1 tell anvalidError tell element set errTag to name of markup tag select XML content end tell set errMessage to errMessage & (errCnt as text) & "<" & errTag & ">" & error message & return end tell end repeat display alert errMessage end if end tell end tell |
動作はこんな感じになる。クリックするとムービー始まります。

全てのeventを削除するのは、
1 2 3 4 5 | tell application "Adobe InDesign CS5" repeat with myCounter from 1 to (count event listeners) delete event listener 1 end repeat end tell |


