OSX: Running multiple instances of an application with different arguments - macos

I need to run multiple instances of Google Chrome but with different arguments.
What I have tried:
open -n -a "Google Chrome" --args --some-param-A
Fine, a new instance of Chrome is launched. Checking the command line parameters using chrome://version is also OK, --some-param-A is there.
Now I would like to open a second instance but with different command line parameters.
open -n -a "Google Chrome" --args --some-param-B
A second instance is launched, but it is started with --some-param-A again. I need it to start with --some-param-B though.
I also tried the -F (--fresh) option, did not help either. Seems that arguments after --args are read only once.
How can I fix that?
Thanks!

Have you tried passing --user-data-dir to Chrome when you launch it from the command line?
It seems to be a known issue and using --user-data-dir (without a specified directory) is a workaround.
Works Cited:
https://askubuntu.com/questions/35392/how-to-launch-a-new-instance-of-google-chrome-from-the-command-line

Related

open -a Terminal filename argument doesn't work at all, on mac osx

I'm trying to open a C file already compiled using
open -a Terminal filename argument
It's actually a simple server.c file.
I need also to pass the port as argument, but i can't find out how to do it.
"open -a Terminal proxy 8000"
it returns: "The file /Users/...../8000 does not exist."
So i've tried:
"open -a Terminal proxy\ 8000"
it returns: "The file /Users/...../proxy 8000 does not exist."
Of course if i do "./proxy 8000"
It works.
So i definitely need to pass di parameter as argument in command line, but i can't find out any help even in stackoverflow.
Any help?
Thanks!
The open command is meant to open simple files, it doesn't really support your use case. There's an --args option that passes arguments to the application it uses to open the file (i.e. Terminal), but it doesn't look like Terminal passes those on.
AppleScript might work for you though, e.g. from the shell:
osascript -e 'tell application "Terminal" to do script "/path/to/proxy 8000"'

AppleScript: execution error -10810 when launching certain applications from shebang'ed scripts

I'm running OS X 10.10.2. I'm facing a weird issue where AppleScript won't launch applications from shebang'ed scripts while working fine everywhere else (Script Editor, piping to osascript, etc.). Specifically, consider the following example script named launch-app:
#!/usr/bin/osascript
launch application "TextEdit"
When TextEdit is not running and I do
./launch-app
I get
./launch-app:0:29: execution error: An error of type -10810 has occurred. (-10810)
When I do
<launch-app osascript
Well, it works just fine; which means the following Bash script will also work:
#!/usr/bin/env bash
osascript <<EOF
launch application "TextEdit"
EOF
Really weird. (By the way, a tell ... activate ... end tell block results in the same error. I'm using launch here just to keep to example minimal.)
I have some old scripts that involve activating an application (well, practically all my old scripts involve tell ... activate ... end tell) that definitely worked in the past. I can't tell when things began to fall apart because when I run those scripts, most often the applications to activate are already launched. I have the impression that the issue dates back at least to 10.10.1.
I have looked at several related posts here on SO, e.g., this one, but they don't help. I also tried to understand error -10810 by reading articles like this one, but my problem definitely doesn't look like a filled process table (otherwise why does directly calling osascript works while running osascript from a shebang doesn't?).
Update: The bug has been fixed in OSX 10.10.3.
Just to provide a state-of-the-union post:
The behavior observed is a bug in OSX 10.10 still unresolved as of OSX 10.10.2 (as of 10 Mar 2015):
Anyone interested in getting this fixed should file their own bug at http://bugreport.apple.com.
It applies to executable scripts that are directly or indirectly passed to osascript - whether:
explicitly (osascript launch-app)
or implicitly, via the shebang line, by direct invocation (./launch-app)
The specific form of the shebang line is irrelevant (whether #!/usr/bin/osascript or #!/usr/bin/env osascript or #!/usr/bin/env osascript -l JavaScript or ...), what matters is whether the file has the executable bit (permission) set (e.g., via chmod +x).
Workarounds:
As suggested by the OP, feed the file to osascript via stdin: osascript < launch-app
This has side effects; for instance, name of me will report msng instead of the name of the script.
Remove the executable bit from the script and invoke it explicitly with osascript:
chmod -x launch-app # a one-time operation
osascript launch-app # with the executable bit unset, this should work
Looking at the man page for osascript, when you send lines of applescript code you should put the "-e" option infront of each separate line.
So here's what I tested. I made a bash script with the -e option...
#!/bin/bash
osascript -e 'launch application "TextEdit"'
And one without.
#!/bin/bash
osascript 'launch application "TextEdit"'
The one without the -e option does not run. As such I think this could be a cause of your problem... there's no -e option in your code.
Note that I tested your code too and got the same error as you. There's a command line utility "/usr/bin/macerror" and I entered your error code into that. Here's the result.
Unknown error (-10810) at /usr/bin/macerror5.18 line 40, <DATA> line 1.
Good luck.
There is no need for using osascript to launch applications. There is a built in command line utility named open, that will open your app from the terminal commandline, or a shebanged script. For doucumentation, enter "man open" in a terminal window. It is a really nifty utility, with a lot of options. :)
The open utility, will lauch applications that are not running, but I also wonder out of curiosity: have you tried "tell application appname to run", or just "tell application appname to activate"?
The osascript below, works for me, on 10.9
#!/usr/bin/osascript
tell application "TextEdit" to launch
I guess you'll have to commmand the app to do something, and not just try to "launch" it. Maybe "tell me to launch application appname also works".
Edit
I prefer to use open -b "com.apple.textedit", because then I also get the front window of textEdit, brought to front.
By the way, with the open -e command, you can open a textfile directly into TextEdit from the commandline. open is not totally as good as the plumb utility of plan-9, but it is really nifty.

Run php file in different browser using terminal other than default browser

Hey guys is there a way to run php file in chrome or in any other browser using terminal.If I run my php file it automatically runs on my default browser but I just want to run on different browser using terminal(Mac OS).
Use the "open" command and specify a different browser with the "-a application" flag.
open -a /path/to/Chrome/binary yourfile.php
Manpage for "open" is here.
Of course, you can also make an alias for that to save typing:
alias chrome="open -a /path/to/Chrome/binary yourfile.php"
then you can just type
chrome
If you have spaces in the path to your Chrome binary, you will need to escape them when defining the alias, something like this:
alias chrome="open -a \"/Applications/Google Chrome.app/contents/MacOS/Google Chrome\" yourfile.php"

How can I launch Chrome with flags from command line more concisely?

I am developing a WebGL driven application and I want to launch chrome like this from the command line:
open -a Google\ Chrome --args --disable-web-security
I just don't want to have to type that in every single time. Is there a way to easily turn that into a one word command? I am using a mac if it matters.
Just make an alias in your .bashrc or .bash_profile
alias ogc='open -a Google\ Chrome --args --disable-web-security'
And then reload your shell.
exec $SHELL
Now, every time you type ogc (or whatever you want to call it) in your terminal, it will run the full command open -a Google\ Chrome --args --disable-web-security
This work for me:
Modify the .bash_profile
Write this alias:
alias cchrome='/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --user-data-dir="/tmp/chrome_dev_session" --disable-web-security'
Run
exec $SHELL
With the cchrome command open a new windows of chrome with the disable web security to solve the "access-control-allow-origin" problem
The Easiest way to Launch Chrome with flags in MAC using terminal is :
'/Applications/Google Chrome.app/Contents/MacOS/Google Chrome' --disable-features=CrossSiteDocumentBlockingAlways,CrossSiteDocumentBlockingIfIsolating
Any filter can come after -- in the above code --disable-features=CrossSiteDocumentBlockingAlways,CrossSiteDocumentBlockingIfIsolating is just an example.
Note: Make sure to close all instances of Chrome before running it.
Why not just run a webserver? Open a terminal and type
cd folder_with_html_and_assets
python -m SimpleHTTPServer
Now in your browser to go http://localhost:8000 When your done go to the terminal and press Ctrl-C
That seems much better than disabling your security and risking getting pwnd.

Linux equivalent of the DOS "start" command?

I'm writing a ksh script and I have to run a executable at a separate Command Prompt window.
xdg-open is a similar command line app in linux.
see https://superuser.com/questions/38984/linux-equivalent-command-for-open-command-on-mac-windows for details on its use.
I believe you mean something like xterm -e your.sh &
Don't forget the final &
maybe it´s not a seperate window that gets started, but you can run some executables in background using "&"
e.g.
./myexecutable &
means your script will not wait until myexecutable has finished but goes on immediately. maybe this is what you are looking for.
regards
xdg-open is a good equivalent for the MS windows commandline start command:
xdg-open file
opens that file or url with its default application
xdg-open .
opens the currect folder in the default file manager
One of the most useful terminal session programs is screen.
screen -dmS title executable
You can list all your screen sessions by running
screen -ls
And you can connect to your created screen session (also allowing multiple simultaneous/synchronized sessions) by running
screen -x title
This will open up the emulated terminal in the current window where executable is running. You can detach a screen session by pressing C-a C-d, and can reattach as many times as you wish.
If you really want your program started in a new terminal window, you could do something like this:
xterm yourtextmodeprogram
or
gnome-terminal -e yourtextmodeprogram
or
konsole -e mc
Trouble is that you cannot count on a particular terminal emulator being installed, so (again: if you really want to do this) you would need to look for the common ones and then execute the first one encountered.
As Joachim mentioned: The normal way to do this is to background the command (read about shell job control somewhere, if you want to dig deeper).
There are also cases where you want to start a persistent shell, i.e. a shell session which lives on when you close the terminal window. There are two ways to do this:
batch-oriented: nohup command-to-run &
interactive: screen
if you want a new windows, just start a new instance of your terminal application: in kde it's
konsole -e whatever
i'm sure the Gnome terminal has similar options
Some have recommended starting it in the background with &, but beware that that will still send all console output from the application you launch to the terminal you launched it from. Additionally, if you close the initial terminal the program you loaded will end.
If you're using a desktop environment like KDE or GNOME, I'd check the alt+f2 launching apps (gnome-open is the one for GNOME, I don't know the name of the KDE app) and see if you can pass them the command to launch as an argument.
Also, if your intention is to launch a daemon, you should check the nohup documentation.
I used nohup as the following command and it works:
nohup <your command> &
then press enter and enter!
don't forget the last &
for example, I ran a python code listening to port 5000:
nohup python3 -W ignore mycode.py &
then I made sure of running by netstat -tulnp | grep :5000 and it was ok.

Resources