【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')) とすると上手くいきました〜