【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')) とすると上手くいきました〜
Leave a comment
記事投稿日
| 日 | 月 | 火 | 水 | 木 | 金 | 土 |
|---|---|---|---|---|---|---|
| « 9月 | ||||||
| 1 | ||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| 9 | 10 | 11 | 12 | 13 | 14 | 15 |
| 16 | 17 | 18 | 19 | 20 | 21 | 22 |
| 23 | 24 | 25 | 26 | 27 | 28 | 29 |
| 30 | ||||||


