13

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.

1
  • I improved your formatting. Please edit in what you have tried to do. Commented Mar 5, 2015 at 19:23

5 Answers 5

16

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Yeah using this code, even though I close textedit, the code is not exiting, it is still in blocking state
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.
Yeah I closed the application, but still it is blocking
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???
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
6

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

This won't work with native apps, e.g. Dictionary.app or FaceTime.app
This works just fine: os.system("open /System/Applications/FaceTime.app")
2

AppleScript:

tell app "Whatever you want" to open

Call from Python

import os
os.system("""osascript -e 'tell app "Safari" to open'""")

1 Comment

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
0

This works for me on Mac OS 11.1:

import os

os.system("open -a TextEdit")
print("Done and not blocking")

4 Comments

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 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.
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.
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.
-2

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

import os

os.system("pkill Chrome")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.