Launch Google Chrome from the command line with specific window coordinates - bash

I'm trying to find a shell command that will open Google Chrome with specific x and y coordinates (so that I can set the position of the window when it opens.) Is it possible to do this using command line-arguments?
I need to modify the following command in order to achieve this:
google-chrome http://www.google.com/

When you're using Google's Chrome, there is a shorter way:
"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
--profile-directory="Default"
--app="data:text/html,<html><body><script>window.moveTo(580,240);window.resizeTo(800,600);window.location='http://www.test.de';</script></body></html>"
Pro:
Automatically opens the window
Avoids the popup-blocker
Opens multiple windows on different monitors (multi monitor setup, requires two or more Chrome profiles)
Con:
Only seems to work in "app" Mode
Not tested with other browsers

http://peter.sh/experiments/chromium-command-line-switches/ says --window-position=x,y is what you're looking for.
Updating this years later to include a small shell script I wrote years ago (but after answering this question) that provides an example of how to start chrome with custom window sizes/position and has the ability to create 'fake' user data directories by name.
It may or may not still work, and has some dangerous options set, but you get the idea.. Do not use this verbatim, some of the flags may have been renamed or been removed entirely.. (like the socks proxy commands did)
#!/bin/bash -x
FAKEUSER="${1:-fake-chrome-user}"
CHROMEROOT=$HOME/.chromeroot/
mkdir -p ${CHROMEROOT}
export PROFILE="${CHROMEROOT}/${FAKEUSER}-chromium-profile"
export DISK_CACHEDIR="${CHROMEROOT}/${FAKEUSER}-chromium-profile-cache"
export DISK_CACHESIZE=4096
export MEDIA_CACHESIZE=4096
PARANOID_OPTIONS="\
--no-displaying-insecure-content \
--no-referrers \
--disable-zero-suggest \
--disable-sync \
--cipher-suite-blacklist=0x0004,0x0005,0xc011,0xc007 \
--enable-sandbox-logging >/dev/null 2>&1
"
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome \
--remember-cert-error-decisions \
--ignore-certificate-errors \
--ignore-urlfetcher-cert-requests \
--allow-running-insecure-content \
--window-position=2400,400 \
--window-size=1500,1000 \
--no-pings \
--user-data-dir=${PROFILE} \
--disk-cache-dir=${DISK_CACHEDIR} \
--disk-cache-size=${DISK_CACHESIZE} \
--media-cache-size=${MEDIA_CACHESIZE} \
2>&1
#--proxy-server="socks4://localhost:30604" \
#--host-resolver-rules="MAP * 0.0.0.0 , EXCLUDE localhost" \

To build on #synthesizerpatel's answer, --window-position won't work on it's own.
You'll need to launch it as it's own new instance using --user-data-dir or --chrome-frame like:
"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --user-data-dir=XXXXXXXXXX --window-size=800,600 --window-position=580,240 --app="http://www.google.com/"
or
"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --chrome-frame --window-size=800,600 --window-position=580,240 --app="http://www.google.com/"
Unfortunately for me, having it as a new instance means it doesn't carry over the session/cookie/etc info from other instances, so I've had to open it normally (with only the --app parameter), then have javascript in the page I open do:
window.moveTo(580,240);
window.resizeTo(800,600);
I guess if you were opening a webpage owned by someone else, you could open your own webpage that has the above js, and then navigates to their webpage.

I've used this:
google-chrome "data:text/html;charset=ISO-8859-1,<html>
<head></head><body><script language=\"javascript\">
window.open('http://perso.f-hauri.ch/~felix/svg/dustin_w_Clock_autonom.svg',
'clock','toolbar=0,location=0,status=0,menubar=0,scrollbars=1,'
+'resizable=1,width=600,height=600,top=100,left=120');</script>"
but google-chrome block popup windows, so this:
google-chrome "data:text/html;charset=ISO-8859-1,<html><head></head><body>
<button onclick=\"javascript:window.open(
'http://perso.f-hauri.ch/~felix/svg/dustin_w_Clock_autonom.svg',
'clock','toolbar=0,location=0,status=0,menubar=0,scrollbars=1,'
+'resizable=1,width=600,height=600,top=100,left=120');\"> clock </button>"
give a nice way to do this.
Nota: This work as well with firefox too.

With my latest version of Chrome - I only needed the following. Everytime I closed the app, it remembered my window size and position.
"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --chrome-frame --app=https://mightytext.net/web8/?exp=1
This worked for me in Version 48.0.2564.48 beta-m (64-bit)
and Version 48.0.2564.48 beta-m (64-bit)

Related

WSL (Ubuntu): how to open localhost in browser from bash terminal

I am trying to open http://localhost in (any) browser from WSL bash terminal.
So far I have tried:
How can I open Google Chrome from the terminal with the URL "localhost:3000"?
"Couldn't find a file descriptor referring to the console" on Ubuntu bash on Windows
How to mention C:\Program Files in batchfile
No luck in setting up BROWSER variable for xdg-open, it responds to xdg-open http://localhost with /usr/bin/xdg-open: 851: /usr/bin/xdg-open: /c/"Program: not found.
I have tried escaping with \ and ^. Using %ProgramFiles(x86)% and ofcorse "Program Files (x86)". More or less it is the same answer every time... Any ideas how to set a work flow for opening browser in WSL?
So far I've ended up with:
/c/Program\ Files\ \(x86\)/Google/Chrome/Application/chrome.exe localhost
But I am looking for more elegant solution.
You can invoke the Windows command line from Bash and use Windows file association to open URL with the default Windows browser.
To do so just type in Bash:
cmd.exe /C start http://localhost
In my case this loads localhost in Chrome, note that the full URL is necessary for Windows to decide what to do.
This is similar to what open does in MacOS, hence you may find useful to directly alias the command and use it also for other type of files:
# add this to .bash_aliases
open='cmd.exe /C start'
Now you can open URL or open file.pdf directly from WSL.
Note: since you are simply redirecting commands to cmd.exe, it needs to have access to the file your working with. As a consequence the above solution will work when you find yourself in the Windows file system, but probably will fail when you are working with files in Linux partition (i.e. in the tmp or in the bin folder). This probably has been fixed in the new version of the WSL but I have not tested it.
You are almost there. Just add an alias for the windows chrome executable
http://www.linfo.org/alias.html
alias chrome="/c/Program\ Files\ \(x86\)/Google/Chrome/Application/chrome.exe"
Now you can simply run chrome localhost and open chrome in any web location you desire.
To open localhost in browser from bash terminal, you need to configure wsl so that it defaults to whatever browser has been set as default in your windows 10 system.
You can do this by using some tools from wslu ("A collection of utilities for WSL").
For this purpose you need.
wslview (-u, --unregister "remove wslview as the default WSL web browser.
-r, --register "register wslview as the default WSL web browser.)
wslpath (-a "force result to absolute path format",
-u "translate from a Windows path to a WSL path (default)")
You need to register your preferred browsers like this...
For Google Chrome:
wslview -r $(wslpath -au 'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe')
For Microsoft Edge:
wslview -r $(wslpath -au 'C:\Windows\SystemApps\Microsoft.MicrosoftEdge_8wekyb3d8bbwe\MicrosoftEdge.exe')
Now you can open localhost as x-www-browser localhost:8080 or www-browser localhost:8080 and x-www-browser or www-browser will default to whatever is your current windows 10 default browser provided it has been registered as described above.
Do not forget to indicate the port; localhost alone did not work for me.
To unregister any browser just change the -r flag to -u.
Have a look at wslview help: info wslview <enter> in the wsl terminal
and wslpath <enter> for help with wslpath.
Install wslu (A collection of utilities for WSL) https://github.com/wslutilities/wslu#feature and then add these two lines to your shell's RC file, e.g. .bashrc or .zshrc.
export DISPLAY=:0
export BROWSER=/usr/bin/wslview
You can set the BROWSER variable as you have done . But xdg-open won't work in WSL as the
xdg-openscripts are setup to work with unquoted environment variables ( in which case ,
the path breaks due to spaces in the pathname ).
You can use the wsl-opennpm utility to do the same for WSL .
Once you have npm installed , install wsl-open utility :
sudo npm install -g wsl-open
To open any URL with default Windows Browser :
wsl-open http://google.com
You can also set wsl-open as default program for a file type in WSL :
wsl-open -w // sets wsl-open as the Shell Browser
Then you can use the standard xdg-open for URLs as well with default windows browser :
xdg-open http://google.com
I created a script that basically forwards xdg-open to powershell -c start
Not tested much though.
sudo tee /usr/local/bin/xdg-open <<EOF
#!/bin/sh
powershell.exe -c start "'\$#'"
EOF
sudo chmod +x /usr/local/bin/xdg-open
Cheers
Oliver
Came across this article that worked for me:
https://towardsdatascience.com/running-jupyter-notebook-on-wsl-while-using-firefox-on-windows-5a47ebfae4c1
In short:
Step 1 - Generate config for Jupyter Notebook:
jupyter notebook --generate-config
Step 2 - Edit the config file using "nano" or other editor
The config fileshould be under your home directory under ".jupyter" folder:
~/.jupyter/jupyter_notebook_config.py
Step 3 - Disable launching browser by redirecting file
First comment out the line, then change True to False:
c.NotebookApp.use_redirect_file = False
Step 4 - add a line to your .bashrc file to set the BROWSER path
export BROWSER='/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe'
For me it was Chrome under my Windows Program File. Otherwise any linux installation under WSL doesn't have a native browser to launch, so need to set it to the Windows executable.
Step 5 - restart .bashrc
source .bashrc
That should work!
https://github.com/microsoft/WSL/issues/3632#issuecomment-690061348
export BROWSER='eval "/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe"'
xdg-open https://google.com # nice work
solved the spaced path problem.
it worked for me.
Now you can simple use :
sensible-browser http://www.google.com
it already comes with wsl and it opens the default browser in windows
ps: you can also use wslview . to open the file explorer from the bash terminal
i'll give you a suggestion, it could be opened via visual studio code in wsl. And install the live server plugin.
Ok so first of all, I don't use windows anymore so I can't post a full solution that I've personally tested, but back when I did use windows, I use to do this and it worked. (This should probably be a comment, but a while back I deleted some unaccepted answers and lot the associated reputation :/)
Solution:
Don't try to launch your windows programs from inside WSL, instead install the linux version of the program and an X server, such as Xming. Here is an example tutorial for forwarding X apps back to Xming on windows.
Summarized, install Xming (on Windows). Then export the DISPLAY variable:
export DISPLAY=:0
Install google-chrome inside WSL and launch it via the CLI. It should show up on your desktop.
Note: There's also a way to use PuTTY alongside XMing for remote viewing, but you'll need to disable Windows firewalls and install the full openssh-server inside WSL first.

How do I launch a browser via terminal and have it open to a specific webpage on Mac?

I'm working on a small personal project and could not find an answer anywhere. I'm brand new to the Mac world and so have 0 experience with BSD. I'm just looking to make it so that when a condition in an if statement is met (I know how to do that part) my default browser (Firefox) will open up to a specified webpage (ex $var = stackoverflow, opens to stackoverflow.com)
Any help appreciated!
Other users have already intimated the use of the open command using the syntax:
open /Path/To/browser.app http://example.com
which, in the case of Firefox, would resemble something like this:
open /Applications/Firefox.app https://stackoverflow.com
However, you can simplify this somewhat using the -a flag, which allows you to simply specify the name of the application you wish to open, like so:
open -a Firefox https://stackoverflow.com
Furthermore, since Firefox is your default browser, you can omit the application altogether. URLs automatically get opened in the system's default browser:
open https://stackoverflow.com
which, in your case, will open the Stack Overflow website in Firefox, whilst on my system, it would open it up in Safari (my default browser).
You can also specify multiple URLs to open up multiple webpages at once:
open https://stackoverflow.com https://imdb.com https://youtube.com
which, on my system, opens each URL in a separate tab of the most recently active Safari window. On yours, it will do a similar thing using Firefox, depending whether you've set the browser to open new pages in a separate window, or as a new tab in the same window, etc.
Scripting
Combining this with your if...then control statement, a simple bash script might look something like this:
?URL() { [[ -z "${#}" ]] && return 1 \
|| printf '%s\n' "${#}" \
| egrep -ix 'stackoverflow|imdb|youtube' \
| printf 'https://%s.com\n' $(cat) \
| open $(cat); }
Then, running ?URL stackoverflow would open https://stackoverflow.com, whereas running ?URL stackoverflow dropbox imdb would open https://stackoverflow.com and https://imdb.com, but not https://dropbox.com (as it is not in the list of valid website matches).
Use the open command, followed by the path to your browser in single quotes, followed by the URL to the site you want to open in single quotes. example
open '/Applications/Google Chrome.app' 'http://youtube.com'
open '/Applications/Firefox.app/' 'http://www.du.ac.in'

Geany editor "open explorer here" equivalent using nautilus

The intended functionality should be similar to what's seen in many windows editors e.g. "open explorer here". For those unfamiliar with windows, I just want to open nautilus to the directory of the active document.
I've tried two solutions so far, both which end up opening nautilus to the correct directory but without the window activating (not coming to the front with input focus).
Solution attempt 1 - Use the pre-existing "set build commands" and run the following command instead of make
nautilus %d; xdotools windowactivate $(xdotools search --name %d)
Solution attempt 2 - Use the Lua scripting plugin
dir = geany.dirname(geany.filename())
os.execute("nautilus " .. dir .. "; xdotools windowactivate $(xdotools search --name " .. dir .. ")")
I'm not worried about multiple windows having the same name, and I've tested the xdotools script in bash and it works fine. I'm really unsure what I'm missing here. I also don't want to use the explorer side-bar as a work-around.
Just another solution: Geany-Plugin treebrowser is able to do this.
I did not resolve the bug using Nautilus. Thanks to frlan's help though using Thunar as an alternative works great. There are better guides exising on the internet, but all I did was install thunar[1]
sudo aptitude install thunar
then set it as my default via another package which I had to install in order to run exo-preferred-applications[2]
sudo aptitude install exo-utils
exo-preferred-applications
[1]
[2]
I'm not going to keep the links up to date - so if they are broken just do a quick google search. There are plenty of resources surrounding this topic.

Making workspaces clickable to start Eclipse on them?

We use a lot of workspaces, and frequently switch between them. I was wondering if there is a trick that can allow me to simply click a workspace in Windows Explorer and have Eclipse started on it.
A "create shortcut for Eclipse" creator might also be interesting, but I was envisioning perhaps a special name for the workspace triggering this behaviour?
Suggestions?
(Edit: I am aware of the --data switch, but I'm looking for the bridge to Windows Explorer which may use that switch)
You can run Eclipse with a parameter do specify the workspace. I think it's -data - but check the documentation. So then you can just make a bunch of desktop shortcuts, one for each workspace.
Documentation can be found here
I now found a solution based on https://superuser.com/q/412312/7401, namely to craft a few registry entries, to get "Open Folder as Eclipse Project" to show up when right-clicking on a workspace.
"eclipse-folder.reg" looks like:
Windows Registry Editor Version 5.00
; This will make it appear when you right click ON a folder
; The "Icon" line can be removed if you don't want the icon to appear
[HKEY_CLASSES_ROOT\Directory\shell\eclipse]
#="Open Folder as &Eclipse Project"
"Icon"="\"C:\\Users\\tra\\Downloads\\eclipse-jee-juno-SR2-win32-x86_64\\eclipse\\eclipse.exe\",0"
[HKEY_CLASSES_ROOT\Directory\shell\eclipse\command]
#="\"C:\\Users\\tra\\Downloads\\eclipse-jee-juno-SR2-win32-x86_64\\eclipse\\eclipse.exe\" -data \"%1\""
Be very careful when editing to point at another location. All backslashes must be doubled inside the string. Works for me under Windows 7 Pro.
Maybe you could look at Yoxos 5 Beta. It is said to provide the shortcut functionalities - on the other hand I have never tried it.
I guess you are under Windows. Just in case someone would have the same problem but under Linux environment, i had the same question and am using right now a simple shell script which behaves like an "eclipse workspace launcher". It shows me all my workspaces, with the number of projects contained in each workspaces. So i can easily launch one dedicated workspace.
My folder structure is just something like :
~/java/workspaces/eclipse/workspace1/project1/
~/java/workspaces/eclipse/workspace1/project2/
~/java/workspaces/eclipse/workspace2/project3/
and so on. This then gives something like :
The script only needs the Linux "Zenity" package (Gnome) and should run on any shell interpreter :
#!/bin/ksh
windows_title="Eclipse Launcher"
WNG_ICON="/opt/gnome/share/pixmaps/gnome-warning.png"
ECLIPSE_ICON="/opt/Eclipse_DEV/Eclipse-V3-2-1/icon.xpm"
WORKSPACES_DIR="<path_to_my_workspaces_directory>"
WORKSPACES=""
ls -1d "${WORKSPACES_DIR}"/* | while read ITEM
do
WORKSPACE_NAME="$(basename ${ITEM})"
WORKSPACE_COUNT=$(ls -1d "${ITEM}"/* 2>/dev/null | grep -v total | wc -l)
WORKSPACES="${WORKSPACES}${WORKSPACE_NAME} ${WORKSPACE_COUNT}
"
done
ret=`zenity --list --width="280" --height="410" --window-icon="${ECLIPSE_ICON}" \
--text "Choisissez le workspace" \
--column "Workspace" \
--column "Projets" \
$WORKSPACES --title "${windows_title}"`
if [[ ! -z $ret ]] ; then
export JAVA_HOME="/<path_to_java_home>/"
export GTL_MODULES="";
cmd='<path_to_eclipse>/eclipse -showlocation -data "'${WORKSPACES_DIR}$ret'" -vm '${JAVA_HOME}'/bin/java -vmargs -Xss4m -Xms256m -Xmx384m -XX:PermSize=256m -XX:MaxPermSize=256m'
eval $cmd
fi
Otherwise, as stated above, Yoxos seems indeed a good choice (i've just discovered recently about this project on my own).

How to launch an app on OS X with command line - The best way

I want to launch an app on OSX from a script. I need to pass some command line arguments. Unfortunately, open doesn't accept command line args.
The only option I can think of is to use nohup myApp > /dev/null & to launch my app so it can exist independently of the script that launches it.
Any better suggestions?
As was mentioned in the question here, the open command in 10.6 now has an args flag, so you can call:
open -n ./AppName.app --args -AppCommandLineArg
In OS X 10.6, the open command was enhanced to allow passing of arguments to the application:
open ./AppName.app --args -AppCommandLineArg
But for older versions of Mac OS X, and because app bundles aren't designed to be passed command line arguments, the conventional mechanism is to use Apple Events for files like here for Cocoa apps or here for Carbon apps. You could also probably do something kludgey by passing parameters in using environment variables.
An application bundle (.app file) is actually a directory. Instead of using open and the .app filename, you can move into the app's directory and start the actual machine code program located inside. For instance:
$ cd /Applications/LittleSnapper.app/
$ ls
Contents
$ cd Contents/MacOS/
$ ./LittleSnapper
That is the actual binary executable that might accept arguments (or not, in LittleSnapper's case).
In case your app needs to work on files (what you would normally expect to pass as: ./myApp *.jpg), you would do it like this:
open *.jpg -a myApp
You can launch apps using open:
open -a APP_YOU_WANT
This should open the application that you want.
open also has an -a flag, that you can use to open up an app from within the Applications folder by it's name (or by bundle identifier with -b flag). You can combine this with the --args option to achieve the result you want:
open -a APP_NAME --args ARGS
To open up a video in VLC player that should scale with a factor 2x and loop you would for example exectute:
open -a VLC --args -L --fullscreen
Note that I could not get the output of the commands to the terminal. (although I didn't try anything to resolve that)
I would recommend the technique that MathieuK offers. In my case, I needed to try it with Chromium:
> Chromium.app/Contents/MacOS/Chromium --enable-remote-fonts
I realize this doesn't solve the OP's problem, but hopefully it saves someone else's time. :)
Lots of complex answers when you can simply access Applications folder and type:
open -a [APP NAME]
This is it!
I wanted to have two separate instances of Chrome running, each using its own profile. I wanted to be able to start them from Spotlight, as is my habit for starting Mac apps. In other words, I needed two regular Mac applications, regChrome for normal browsing and altChrome to use the special profile, to be easily started by keying ⌘-space to bring up Spotlight, then 'reg' or 'alt', then Enter.
I suppose the brute-force way to accomplish the above goal would be to make two copies of the Google Chrome application bundle under the respective names. But that's ugly and complicates updating.
What I ended up with was two AppleScript applications containing two commands each. Here is the one for altChrome:
do shell script "cd /Applications/Google\\ Chrome.app/Contents/Resources/; rm app.icns; ln /Users/garbuck/local/chromeLaunchers/Chrome-swirl.icns app.icns"
do shell script "/Applications/Google\\ Chrome.app/Contents/MacOS/Google\\ Chrome --user-data-dir=/Users/garbuck/altChrome >/dev/null 2>&1 &"
The second line starts Chrome with the alternate profile (the --user-data-dir parameter).
The first line is an unsuccessful attempt to give the two applications distinct icons. Initially, it appears to work fine. However, sooner or later, Chrome rereads its icon file and gets the one corresponding to whichever of the two apps was started last, resulting in two running applications with the same icon. But I haven't bothered to try to fix it — I keep the two browsers on separate desktops, and navigating between them hasn't been a problem.
Beginning with OS X Yosemite, we can now use AppleScript and Automator to automate complex tasks. JavaScript for automation can now be used as the scripting language.
This page gives a good example example script that can be written at the command line using bash and osascript interactive mode. It opens a Safari tab and navigates to example.com.
http://developer.telerik.com/featured/javascript-os-x-automation-example/
osascript -l JavaScript -i
Safari = Application("Safari");
window = Safari.windows[0];
window.name();
tab = Safari.Tab({url:"http://www.example.com"});
window.tabs.push(tab);
window.currentTab = tab;
Simple, here replace the "APP" by name of the app you want to launch.
export APP_HOME=/Applications/APP.app/Contents/MacOS
export PATH=$PATH:$APP_HOME
Thanks me later.
With applescript:
tell application "Firefox" to activate
Why not just set add path to to the bin of the app. For MacVim, I did the following.
export PATH=/Applications/MacVim.app/Contents/bin:$PATH
An alias, is another option I tried.
alias mvim='/Applications/MacVim.app/Contents/bin/mvim'
alias gvim=mvim
With the export PATH I can call all of the commands in the app. Arguments passed well for my test with MacVim. Whereas the alias, I had to alias each command in the bin.
mvim README.txt
gvim Anotherfile.txt
Enjoy the power of alias and PATH. However, you do need to monitor changes when the OS is upgraded.
To Create a New Text File OR open an existing one, in any folder, using a Text/Code Editor like the Free TextMate app on MACOSX, use this command on Terminal:
open -n /Applications/TextMate.app --args "$PWD/some file.txt"
Instead of a Text File, you can use any file type, based on your app's requirements and its support for this syntax.
This command also simulates the New Text Document Here Command on Windows and has been tested on MacBook Pro 2021 and Monterey 12.2.1 successfully.

Resources