简体   繁体   中英

Python osascript using extra quotation marks

Trying to use the following code (which works fine in applescript) to work in Python , fairly new to Python so I am not sure how I can get this string to work properly.

def getChromeSource():
    cmdGetSource = """
    osascript -e 'tell application "Google Chrome" to set source to execute front window's active tab javascript "document.documentElement.outerHTML"'
    """
    proc = subprocess.Popen([cmdGetSource], stdout=subprocess.PIPE, shell=True)
    (source, err) = proc.communicate()

I am confident the problem is with

window's

I have tried:

window\s

but that doesn't work I think I just have too many quotation marks and I am not sure how to write the string correctly, probably a really easy one so hopefully someone can steer me in the right direction.

You should pass the list of arguments you want to execute rather than creating a string with all the arguments put together. You also shouldn't use the shell=true flag.

cmd_args = ['osascript', '-e', 'tell application "Google Chrome" to set source to execute front window\'s active tab javascript "document.documentElement.outerHTML"']
proc = subprocess.Popen(cmd_args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
source, err = proc.communicate()

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