2月 18, 2012
kamata

xml構造をリアルタイムでチェックする

InDesignCS5からeventにafterSelectionChangedが追加された。
例えばテキストを触って選択状態が変更すると、eventを発行する。

CS2の頃からやってみたかったことがあった。
IDでxmlを扱うとき、オペレータが誤ってxml構造を壊してしまうことがある。
オペ中にリアルタイムで構造チェックをしたかった。
(積極的にxmlの組版をしてこなかった理由もこんなところにある)

このeventを使えば、構造をオペ中にチェックできるのではないか? と思った次第。

サンプルのxmlはこんな感じ。




	

		Harry Potter and the Philosopher's Stone .
		日本語版単行本 ISBN 4-915512-37-1(1999年12月1日発売)
		日本語版携帯版 ISBN 4-915512-49-5(2003年発売)
	
	

		Harry Potter and the Chamber of Secrets
		日本語版単行本 ISBN 4-915512-39-8(2000年発売)
		日本語版携帯版 ISBN 4-915512-54-1(2004年発売)
	

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
2月 18, 2012
kamata

辞書を引く.appを作ってみた

ルビについて調べていたら、ルビとは無関係だが、辞書ということで、次のようなページにいきあたった。
http://sakito.jp/mac/dictionary.html#url-scheme
http://macscripter.net/viewtopic.php?id=26661

「辞書.app」って使ったことがなかった。
コピーした文字列を辞書で調べるってアプリ、上の情報からできるなっと思って作ってみた。

調べたい言葉をコピーして、アプリを実行すると、ダイアログにこんな感じで表示します。

Dictionaryをコピー後実行。

富士山をコピー後実行。

メインの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/