Search code examples
pythonsubprocesspopenquicktimeosascript

use command `osascript -e 'quit app "Quicktime Player 7"'` with python


I am using osascript -e 'quit app "Quicktime Player 7"' in OSX Terminal to close the Quicktime Player 7 application, which works well, but can't get this same command working using python. What am I doing wrong?

This just runs, but does nothing:

command = ['osascript', '-e', 'quit app', 'Quicktime Player 7']
p = subprocess.Popen(command)

Solution

  • Having quit app and Quicktime Player 7 as two list elements will transform the command subprocess.Popen executes into something like this:

    osascript -e 'quit app' 'Quicktime Player 7'
    

    osascript expects the parameter following -e to be "one line of a script" (see osascript's man-page). Splitting up the parameters causes osascript to execute quit app and interpret Quicktime Player 7 as an argument, thus probably ignoring it.

    A simple fix would be the following:

    command = ['osascript', '-e', 'quit app "Quicktime Player 7"']
    p = subprocess.Popen(command)
    

    If you don't want to work with lists/splitting up your commands in the first place, you can use shlex.split to do the work for you:

    import shlex
    command = shlex.split("osascript -e 'quit app \"Quicktime Player 7\"'")
    p = subprocess.Popen(command)