sintrode icon

Batch/Powershell/C# chimera

sintrode | PRO | 05/28/23 07:18:50 PM UTC | 0 ⭐ | 18369 👁️ | Never ⏰ | []
Batch |

2.34 KB

|

None

|

0 👍

/

0 👎

<# :
:: Based on https://gist.github.com/coldnebo/1148334
:: Converted to a batch/powershell hybrid via http://www.dostips.com/forum/viewtopic.php?p=37780#p37780
@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 the relevant section of the Win32 API to the PowerShell session 
# Allows windows to be moved and resized
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);
    }
"@
 
################################################################################
# Moves and resizes the window based the broswer
#
# Arguments: $browser - the browser being moved and resized
# Returns:   None
################################################################################
Function MoveAndResize ($browser)
{
    # $browser_path is the full path to the browser
    # $screen_x is the horizontal location of the window on the screen
    # $screen_y is the vertical location of the window on the screen
    # $win_x is the width of the target window
    # $win_y is the height of the target window
    Switch($browser){
        InternetExplorer{
            $browser_path="C:\Program Files\Internet Explorer\IEXPLORE.EXE"
            $screen_x = 0
            $screen_y = 0
            $win_x = 960
            $win_y = 1080
            break
        }
        Firefox{
            $browser_path="C:\Program Files (x86)\Mozilla Firefox\firefox.exe"
            $screen_x = 960
            $screen_y = 0
            $win_x = 960
            $win_y = 1080
            break
        }
        default {continue}
    }
    
    # Start the desired browser
    Start-Process $browser_path
    
    # Wait one second until the browser is fully loaded
    Start-Sleep -S 1
    
    # Find the running process where the application path matches $browser_path
    $browser = (Get-Process | where {$_.Path -eq $browser_path}).MainWindowHandle
    
    [Win32]::MoveWindow($browser, $screen_x, $screen_y, $win_x, $win_y, $true)
}
 
MoveAndResize "InternetExplorer"
MoveAndResize "Firefox"

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎