Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open Safari with URL from command line and get process handle

I know there are several ways in MacOS to open Safari with a given URL from the command line like

open -a Safari http://stackoverflow.com

Or as an AppleScript

tell application "Safari" to open location "http://stackoverflow.com/"

But if I am starting the process e.g. programmatically I will only get the handle to the open/applescript process (which exits right after Safari is started).

How can I start Safari with a given URL and get the handle to the Safari process (it is still beyond me why it can't just pass the URL as a command line argument to the Safari executable)?

like image 503
Daff Avatar asked Aug 27 '12 22:08

Daff


People also ask

How do I open Safari from terminal?

Apple Safari You can also use the shortcut Option + ⌘ + C . The console will either open up within your existing Safari window, or in a new window.

How do I open Safari with CMD?

From the top level / you should type open Applications/safari This works fine for me. cd up to / and do ls. The Application folder should show up.


1 Answers

in AppleScript, after opening Safari with URL

tell application "Safari" to open location "http://stackoverflow.com/"

you may obtain the process object by

tell application "System Events" to set proc to application process "Safari"

the proc object will have pid, bundle id and etc attached to it.

Also please note that Safari 5+ runs in a multi-process architecture: a master process and several rendering process, the above code will get you the master process only; rendering processes are not easily accessible, nor does it make much sense to control any individual rendering process.

like image 133
Du Song Avatar answered Sep 25 '22 02:09

Du Song