0

I'm a beginner programmer and have been writing a few very simple scripts in python to automate opening up a few apps and chrome tabs for a workflow session. Apologies if my question is very simple but have been looking everywhere for an answer, trying everything and have not got to the point of just randomly trying things and not sure where to go next... I've basically hit a complete block.

The bit that I can't get to work is calling an apple script that I found online to maximise a chrome window from within python. The code I have works exactly as I want it to if I paste and run it directly in the terminal (I'm on Mac OS X), but for some reason, when I try to call the same piece of code from within python using os.system or subprocess.call (which I believe is the more modern version), it doesn't work.

The code that works in the terminal is as follows:

osascript -e "tell application \"google chrome\"" -e "activate" -e "tell application \"System Events\"" -e "keystroke \"f\" using {control down, command down}" -e "end tell" -e "end tell"

However, when called as follows in python, it doesn't work - and gives the error below in the terminal.

import subprocess

args = ' -e "tell application \"google chrome\"" -e "activate" -e "tell application \"System Events\"" -e "keystroke \"f\" using {control down, command down}" -e "end tell" -e "end tell"'

subprocess.call("osascript" + args, shell=True)
syntax error: Expected end of line but found end of script. (-2741)

Any help really appreciated!

Thanks

  • Have a look here https://stackoverflow.com/a/59967377/2836621 and https://stackoverflow.com/a/58283898/2836621 – Mark Setchell Feb 21 '20 at 11:03
  • `subprocess.call(["osascript", "-e", "tell application \"google chrome\" to activate", "-e", "tell application \"System Events\" to keystroke \"f\" using {control down, command down}"])` - note: I assume you already have a window open in google chrome. – RobC Feb 21 '20 at 11:42
  • If you wanted to keep the `args` variable then assign the array as follows: `args = ["-e", "tell application \"google chrome\" to activate", "-e", "tell application \"System Events\" to keystroke \"f\" using {control down, command down}"]`, then invoke `subprocess.call(["osascript"] + args)` - the aforementioned plus operator (`+`) essentially combines the `args` array with the `["osascript"]` array to form one. – RobC Feb 21 '20 at 12:40

0 Answers0