简体   繁体   English

如何从 Python 脚本中运行 AppleScript?

[英]How to run an AppleScript from within a Python script?

How to run an AppleScript from within a Python script?如何从 Python 脚本中运行 AppleScript?

The questions says it all.. (On a Mac obviously)问题说明了一切……(显然在 Mac 上)

this nice article suggests the simple solution 这篇不错的文章提出了简单的解决方案

cmd = """osascript -e 'tell app "Finder" to sleep'"""
def stupidtrick():
    os.system(cmd)

though today you'd use the subprocess module instead of os.system , of course.尽管今天您当然会使用subprocess模块而不是os.system

Be sure to also check page 2 of the article for many more info and options, including appscript .请务必查看文章的第 2 页以获取更多信息和选项,包括appscript

A subprocess version which allows running an original apple script as-is, without having to escape quotes and other characters which can be tricky.一个subprocess版本,它允许按原样运行原始苹果脚本,而不必转义引号和其他可能很棘手的字符。 It is a simplified version of the script found here which also does parametrization and proper escaping (Python 2.x).它是此处找到的脚本的简化版本,它还进行参数化和适当的转义 (Python 2.x)。

import subprocess

script = '''tell application "System Events"
    activate
    display dialog "Hello Cocoa!" with title "Sample Cocoa Dialog" default button 2
end tell
'''

proc = subprocess.Popen(['osascript', '-'],
                        stdin=subprocess.PIPE,
                        stdout=subprocess.PIPE)
stdout_output = proc.communicate(script)[0]
print stdout_output

NOTE: If you need to execute more than one script with the same Popen instance then you'll need to write explicitly with proc.stdin.write(script) and read with proc.stdout.read() because communicate() will close the input and output streams.注意:如果您需要使用同一个 Popen 实例执行多个脚本,那么您需要使用proc.stdin.write(script)显式写入并使用proc.stdout.read() ) 读取,因为communicate()将关闭输入和输出流。

I got the Output folks... Here it's following:我得到了输出的人......这是以下内容:

import subprocess
import sys

for i in range(int(sys.argv[1])):
    ip = str(sys.argv[2])
    username = str(sys.argv[3])
    pwd = str(sys.argv[4])

    script = '''tell application "Terminal"
        activate
        do script with command "cd Desktop && python test_switch.py {ip} {username} {pwd}"
        delay 15
    end tell
    '''

    proc = subprocess.Popen(['osascript', '-'],
                        stdin=subprocess.PIPE,
                        stdout=subprocess.PIPE)
    stdout_output = proc.communicate(script.format(ip=ip, username=username, pwd=pwd))[0]

I was pretty frustrated at the lack of detail in Apple's own documentation regarding how to do this AND to also pass in arguments.我对 Apple 自己的文档中缺乏有关如何执行此操作以及如何传递参数的详细信息感到非常沮丧。 I had to send the desired arg (in this case a zoom id) as a string otherwise the argument didn't come through to the applescript app我必须将所需的 arg(在本例中为缩放 ID)作为字符串发送,否则该参数不会通过 applescript 应用程序

Here's my code running from python:这是我从 python 运行的代码:

f = script if os.path.exists(script) else _tempfile()

if not os.path.exists(script):
            open(f,'w').write(script)
args = ["osascript", f, str(zoom_id)]

kwargs = {'stdout':open(os.devnull, 'wb'),'stderr':open(os.devnull, 'wb')}
        #kwargs.update(params)
proc = subprocess.Popen(args,**kwargs)

and here is my applescript:这是我的 applescript:

on run argv
    set zoom_id to 0
    zoom_id = item 1 in argv
    tell application "zoom.us"
       --do stuff
    end tell
end run

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM