Launch Google Chrome from the command line with specific window coordinates
Asked Answered
M

7

37

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/

Miru answered 18/11, 2012 at 2:11 Comment(1)
The problem is not solved on ubuntu 18.04 now.Vaporization
B
29

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" \
Basutoland answered 18/11, 2012 at 2:18 Comment(3)
Can it also be done for Firefox and IE? i posted a question here:#26627384Orva
Saddly this doesn't always work. See #14592520Scottie
It does work, @GregBray, but you need to set the --user-data-dir flag, too.Disencumber
O
52

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
Oestrin answered 16/11, 2014 at 22:30 Comment(6)
Another "pro" -- it works with ordinary browser profiles, as of Chrome 51; the other answer apparently does not.Laney
Great, thank you a lot! This is the only solution that worked for me.Treatment
Would like to have an address bar. How did this window even become a popup?Buckle
Does this work on osx?Shrewmouse
@avgJoe, yes it works on all operating systems including OSX. an example of how to call Chrome on OSX you see in the other answer.Oestrin
This will not work if there is another window already opened using the same profile! There will be only a screen flicker but no window is opened.Colpitis
B
29

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" \
Basutoland answered 18/11, 2012 at 2:18 Comment(3)
Can it also be done for Firefox and IE? i posted a question here:#26627384Orva
Saddly this doesn't always work. See #14592520Scottie
It does work, @GregBray, but you need to set the --user-data-dir flag, too.Disencumber
N
28

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.

Na answered 5/11, 2013 at 12:48 Comment(5)
the option --chrome-frame didn't work for me, but with --user-data-dir i got it working. thanksPreform
Looks like it has been deprecatedPseudohermaphrodite
Can it also be done for Firefox and IE? i posted a question here:#26627384Orva
Works for me on a Raspberry Pi running Raspbian and lightdm.Breakfront
The --user-data-dir option works for me (on MacOS), but the command will now block. To work around this, the command has to be suffixed by an & on the command line, or the user has to press Ctrl+C after they're done (closing the window does not unblock the process)Amora
I
4

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.

Illusionary answered 18/11, 2012 at 10:24 Comment(1)
If you replace onclick by onfocus, this will work by simply hit <tab> key, in chrome but not in firefox... May be a security issue!?Illusionary
A
1

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)

Allsun answered 7/1, 2016 at 23:8 Comment(1)
The problem is that it requires manually adjusting the position and size first; they cannot be incorporated into the launching command.comAnstus
A
0

This worked for me.

"C:\Program Files\Google\Chrome\Application\chrome.exe" --user-data-dir="C:\Users\hello\gtemp\123" --window-size=100,1000 --window-position=3000,0 "https://www.openchromeonadifferentwindow.com"
  1. Need user-data-dir.

In order to run multiple separate Chrome browser processes, each of them needs its own profile. You can specify --user-data-dir="path/to/profile/#" (change # to a different number or use an entirely different path) when you run each browser instance. Note - the path does not have to exist, it only has to be legal (and Chrome should have permissions to create it in case it does not exist). Chrome will create profile paths that do not exist yet

.. Found it on this link helpful.

  1. The window-position=3000,0 helps to move the window position to another monitor
  2. Need to change the user-data-dir # to a different number if you want to open another instances with different settings
Agonized answered 30/3, 2023 at 12:53 Comment(0)
A
0

In year of 2024; window.location command closes the newly opened Chrome window. Below is my workaround to simulate WhatsApp Web desktop app:

  1. Create a local html file called WhatsApp-Web.html:

    <html>
    <body>
       <script> 
         window.resizeTo(800,1000); // This should be the first to prevent screen overflow
         window.moveTo(1000,40);
         window.location='https://web.whatsapp.com/';
     </script>
     </body>
     </html>
    
  2. Create a shortcut.lnk

    "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --app="file:///D:/WhatsApp-Web.html"
    
Aerobic answered 17/7 at 15:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.