I am relatively new to the mac world. My question concerns opening an application using python on mac osx. From what I’ve found so far, it seems as if applications are stored in app format that are actually directories. Are these parsed somehow by the OS when opening the app? I would like to open Safari using python and it is in my /Applications/Safari.app directory. Is there a specific binary I should be passing to os.system or should I be going about it in a completely different way? My end goal is to have safari open a local html file, close it then open another local html file.
Thanks,
-John
The Python standard library includes the webbrowser module which allows you to open a new browser window or tab in a platform-independent way. It does support Safari on OS X if it is the user’s default:
But
webbrowserdoes not support closing a browser window. For that level of control, you are best off using Safari’s Apple Event scripting interface by installing py-appscript.If you just want to change the web page displayed in the tab you opened:
Safari’s Apple Events interface is somewhat non-intuitive (unfortunately, that’s not unusual with Mac applications). There are references out there if you need to do more complex stuff. But Python and py-appscript give you a solid base to work from.