简体   繁体   中英

How to run an AppleScript from within a Python script?

How to run an AppleScript from within a Python script?

The questions says it all.. (On a Mac obviously)

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.

Be sure to also check page 2 of the article for many more info and options, including 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. It is a simplified version of the script found here which also does parametrization and proper escaping (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.

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. 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

Here's my code running from 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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