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.
5 Answers
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.
5 Comments
Bharath
Yeah using this code, even though I close textedit, the code is not exiting, it is still in blocking state
Matt Anderson
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.Bharath
Yeah I closed the application, but still it is blocking
Motti Shneor
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
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
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")
2 Comments
Bn.F76
This won't work with native apps, e.g.
Dictionary.app or FaceTime.appJamie Forrest
This works just fine:
os.system("open /System/Applications/FaceTime.app") AppleScript:
tell app "Whatever you want" to open
Call from Python
import os
os.system("""osascript -e 'tell app "Safari" to open'""")
1 Comment
user1766438
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
This works for me on Mac OS 11.1:
import os
os.system("open -a TextEdit")
print("Done and not blocking")
4 Comments
user3439894
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!.user1766438
@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.
user3439894
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.
user1766438
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.