简体   繁体   中英

How can i use a .bat file to open new Chrome Tabs

I've been using the following scrip (links changed) to open, resize and move two IE tabs on the screen. I've tried adapting the same script for Chrome/Firefox but i am missing something and i am not able to either open both pages in new tabs, or even move/resize the windows.

<# :
@echo off
setlocal
cls
set "POWERSHELL_BAT_ARGS=%*"
if defined POWERSHELL_BAT_ARGS set "POWERSHELL_BAT_ARGS=%POWERSHELL_BAT_ARGS:"=\"%"
endlocal & powershell -NoLogo -NoProfile -Command "$_ = $input; Invoke-Expression $( '$input = $_; $_ = \"\"; $args = @( &{ $args } %POWERSHELL_BAT_ARGS% );' + [String]::Join( [char]10, $( Get-Content \"%~f0\" ) ) )"
goto :EOF
#>

Add-Type @"
    using System;
    using System.Runtime.InteropServices;

    public class Win32 { 
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
    }
"@

Function MoveAndResize ($browser)
{

    Switch($browser){
        WINDOW1{
            $browser_path="C:\Program Files\Internet Explorer\IEXPLORE.EXE"
            $url = "https://www.google.com/" 
            $pos_x = 0
            $pos_y = 0
            $window_x = 975
            $window_y = 1080
            break
        }
        WINDOW2{
            $browser_path="C:\Program Files\Internet Explorer\IEXPLORE.EXE"
            $url = "https://www.bing.com/"
            $pos_x = 960
            $pos_y = 0
            $window_x = 975
            $window_y = 1080
            break
        }
        default {continue}
    }

    Start-Process $browser_path $url
    Start-Sleep -S 1

    $browser = (Get-Process | where {$_.Path -eq $browser_path}).MainWindowHandle

    [Win32]::MoveWindow($browser, $pos_x, $pos_y, $window_x, $window_y, $true)
}

MoveAndResize "WINDOW1"
MoveAndResize "WINDOW2"

As i am not a mastermind, and this is way over my batch knowledge i've got the following questions: Is there any reworking over the code/ anything or any tips to get it working? Any code that could be removed to make it smaller? How could i make it open the pages in new tabs?

The best way to easily open a new tab in any browser:

START "<ApplicationName>" <URL>

Leaving the Application Name empty will open the default browser.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM