xml構造をリアルタイムでチェックする
InDesignCS5からeventにafterSelectionChangedが追加された。
例えばテキストを触って選択状態が変更すると、eventを発行する。
CS2の頃からやってみたかったことがあった。
IDでxmlを扱うとき、オペレータが誤ってxml構造を壊してしまうことがある。
オペ中にリアルタイムで構造チェックをしたかった。
(積極的にxmlの組版をしてこなかった理由もこんなところにある)
このeventを使えば、構造をオペ中にチェックできるのではないか? と思った次第。
サンプルのxmlはこんな感じ。
dtdはこんな感じ。
タイトルは1つ無くてはいけない。ここを消すとアラートを出すようにしたい。
まず、イベントを発生させるスクリプトは、
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のスクリプトは、
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を削除するのは、
tell application "Adobe InDesign CS5"
repeat with myCounter from 1 to (count event listeners)
delete event listener 1
end repeat
end tell
辞書を引く.appを作ってみた
ルビについて調べていたら、ルビとは無関係だが、辞書ということで、次のようなページにいきあたった。
http://sakito.jp/mac/dictionary.html#url-scheme
http://macscripter.net/viewtopic.php?id=26661
「辞書.app」って使ったことがなかった。
コピーした文字列を辞書で調べるってアプリ、上の情報からできるなっと思って作ってみた。
調べたい言葉をコピーして、アプリを実行すると、ダイアログにこんな感じで表示します。
メインのapplesript部分は、
--* 自身のバンドル・パスを得る
set self to path to me
set scriptBundlePath to (path to resource "Scripts" in bundle self) as Unicode text
set iconPath to (path to resource "applet.icns" in bundle self) as Unicode text
--* スクリプトファイルのフルパスを作成する
set pyScriptFile to scriptBundlePath & "dict2.py"
--* Unix用のパスに変換する
set pyscriptpath to quoted form of (POSIX path of pyScriptFile)
--* paste値を変数にセット
set searchword to do shell script "pbpaste"
--* shell コマンド
set command to "/usr/bin/python2.5 " & pyscriptpath & " " & quoted form of searchword
--* コマンド実行
set theResult to do shell script command
--* 表示
display dialog theResult with title searchword buttons {"Close"} default button {"Close"} with icon file iconPath
Pythonスクリプトは、
#!/usr/bin/python2.5
import sys
from DictionaryServices import *
def main():
try:
searchword = sys.argv[1].decode('utf-8')
except IndexError:
errmsg = 'You did not enter any terms to look up in the Dictionary.'
print errmsg
sys.exit()
wordrange = (0, len(searchword))
dictresult = DCSCopyTextDefinition(None, searchword, wordrange)
if not dictresult:
errmsg = "'%s' not found in Dictionary." % (searchword)
print errmsg.encode('utf-8')
else:
print dictresult.encode('utf-8')
if __name__ == '__main__':
main()
弊社ダウンロードサイトから取得できます。
https://www.web-cte.co.jp/tools/