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/

Leave a comment