【python】pythonでApplescript、引数(args)はどう渡すのか?
昨日、2時間悩んだのでメモっておく。
http://stackoverflow.com/questions/2940916/how-do-i-embed-an-applescript-in-in-a-python-script
のサイトの下に、
from subprocess import Popen, PIPE def run_this_scpt(scpt, args=[]): p = Popen(['osascript', '-'] + args, stdin=PIPE, stdout=PIPE, stderr=PIPE) stdout, stderr = p.communicate(scpt) return stdout #Example of how to run it. run_this_scpt("""tell application "System Events" to keystroke "m" using {command down}""") #Example of how to run with args. run_this_scpt(''' on run {x, y} return x + y end run''', ['2', '2'])
とあったので、やってみたが、エラーとなった。調査、試行錯誤すると、上記はpython2系の場合は動作する。
私の環境は3.3。どうするかというと、
stdout, stderr = p.communicate(scpt) の部分を
stdout, stderr = p.communicate(bytes(scpt, 'UTF-8')) とすると上手くいきました〜
【AppleScript】MavericksでSecurity API failed with error -60008
弊社製品、PDF検版ツールBeforeAfterは、AppleScriptで作られていて、10.5〜10.8まで、問題なく動作しています。ところが、10.9 Mavericksで動作させたところ、問題が発生しました。
4, 5ページの処理では問題ありませんが、8ページ位で処理で止まってしまいました。
調査をすると、do shell scriptの連続処理の箇所で、8ページ程度処理するとSecurity API failed with error -60008 が発生します。
この発生タイミングがよく分かりません。少し修正し、30ページの処理が上手く行き、これで大丈夫だろうと思って、160ページの処理を行うと、また発生するといった具合です。いろいろと試行錯誤を繰り返しました。
処理途中の状況を見るため、あるdo shell scriptの前に、display dialog を入れると、なぜかこのdo shell scriptのコマンドでエラーは発生しなくなりました。
いちいちダイアログを表示させるわけにもいかないので、どうしたものかと考えました。delay 1 ではだめでした。
で、シスログを書き出すよう、下記のようなコマンドを、do shell scriptの前に挿入しました。
do shell script “logger -p user.notice -t BeforeAfter @message”
これで、エラーが発生しなくなったのです。1000ページの処理を行っても問題が出なくなりました。
何故、Mavericks で Security API failed with error -60008 が発生するようになったのか? 何故 display dialog やシスログで回避できるのか? 原因は不明なのだが、とにかくこれで乗り切ったので報告しておきます(この調査〜対策まで3日もかかりました〜)。