12

I want to open an application like TextEdit or Firefox in Mac OS using Python and wait till the applications exits. I can't figure out exact command to open an app and wait.

Rodrigo Taboada
  • 2,727
  • 4
  • 24
  • 27
Bharath
  • 311
  • 1
  • 3
  • 5

5 Answers5

15

I don't know how to do it in applescript, but you can do this by using the /usr/bin/open UNIX-level OS X command. This snippet will open TextEdit.app and block, waiting for it to quit before continuing:

import subprocess

subprocess.call(
    ["/usr/bin/open", "-W", "-n", "-a", "/Applications/TextEdit.app"]
    )

Look at the open man page (man open) and the python subprocess module documentation for more details.

Matt Anderson
  • 19,311
  • 11
  • 41
  • 57
  • Yeah using this code, even though I close textedit, the code is not exiting, it is still in blocking state – Bharath Mar 04 '15 at 15:46
  • Did you close TextEdit, or did you *exit* TextEdit? Per the documentation, the `-W` switch tells open to wait for the program to exit. And it worked in my testcase; the subprocess stopped blocking when I exited TextEdit. – Matt Anderson Mar 04 '15 at 16:06
  • Yeah I closed the application, but still it is blocking – Bharath Mar 04 '15 at 16:54
  • You don't "close" and application in Mac. you "close" a document. You Quit an application - and when I did so - the above subprocess.call() command returned as expected. This suggestion works fine. However, on the next level - If I'm not waiting for the app to exit, how can I grab the "subprocess" object of the TextEdit, and not the "open" process??? – Motti Shneor May 29 '16 at 13:56
  • 4
    on the Mac semantics, you can't "close" an application. You can only "close" a window of an application. The application remains running even with no open windows. The right semantics is "Quit" an application, which is a distinct action, available from the application's menu in the menu-bar (the menu named after the application itself) its key-abbreviation is Command-Q – Motti Shneor Jun 22 '16 at 04:10
5

You can open any application like this example

import os

os.system("open /Applications/Google\ Chrome.app")
os.system("open /Applications/Todoist.app")
os.system("open /Applications/WhatsApp.app")
Arthur Correa
  • 51
  • 1
  • 1
1

AppleScript:

tell app "Whatever you want" to open

Call from Python

import os
os.system("""osascript -e 'tell app "Safari" to open'""")
USERNAME GOES HERE
  • 692
  • 1
  • 15
  • 29
  • This is the error I see on Mac OS 11.1 (Python 3.9.6): 21:25: execution error: Safari got an error: AppleEvent handler failed. (-10000) 256 – user1766438 Aug 23 '21 at 18:38
-1

This works for me on Mac OS 11.1:

import os

os.system("open -a TextEdit")
print("Done and not blocking")
user1766438
  • 492
  • 4
  • 13
  • 1
    There is already an answer with `import os` and an `os.system(..)` by Arthur Correa, so I fail to see how this adds any value as a new answer. Also, this doesn't actually answer what was asked in the OP!. – user3439894 Aug 23 '21 at 19:40
  • @user3439894 I was looking for an answer to a similar question: "how to open an Mac OS application from Python, but without blocking". My answer doesn't require a full path to the application and doesn't block. – user1766438 Aug 23 '21 at 19:41
  • RE: "but without blocking"" -- But that is not what the OP is wanting! RE: "My answer doesn't require a full path to the application " -- Not enough to justify a separate answer, just comment on the existing answer. – user3439894 Aug 23 '21 at 19:42
  • It isn't 100% of what the original poster wanted but you never know. Maybe he changed his mind and wants a non-blocking way to open an application. – user1766438 Aug 23 '21 at 19:51
-2

You can close any app on osx (like Chrome or Safari) with this in python:

import os

os.system("pkill Chrome")