Saturday 30 March 2019

macos - Run Safari from terminal with given URL address without open command


I'm having trouble with opening Safari from terminal. Command I tried is


/Applications/Safari/Contents/MacOS/Safari http://www.example.com

Safari opens and tries to navigate to the url:


file:///Current/Terminal/Path/Here/http:/www.example.com

I know, I could do it using open:


open -a safari http://www.example.com

But I can't. The bigger picture is quite complicated: I'm actually trying to run the browser from command-line .NET application (which is running using mono). I use System.Process .NET class to run the command and I have to be able to control the process (get process Id, check if it's active, kill it if necessary, etc.). Using open command does not return correct process Id (I tried using open with -W switch - does not help either).



Answer



One solution would doing this in 2 times



  • Open safari

  • Using AppleScript to open the url tell application "Safari" to open location "http://www.google.com"


Here is a oneliner :


/Applications/Safari.app/Contents/MacOS/Safari & sleep 1 && osascript -e 'tell application "Safari" to open location "http://www.google.com"'

NB: you need to wait before running the AppleScript because Safari is not started yet. The sleep 1might not work in every case since it will really depend on how much time Safari will need to start.


No comments:

Post a Comment

How can I VLOOKUP in multiple Excel documents?

I am trying to VLOOKUP reference data with around 400 seperate Excel files. Is it possible to do this in a quick way rather than doing it m...