Advertisement
sintrode

Batch/Powershell/C# chimera

May 28th, 2023
4,790
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 2.34 KB | None | 0 0
  1. <# :
  2. :: Based on https://gist.github.com/coldnebo/1148334
  3. :: Converted to a batch/powershell hybrid via http://www.dostips.com/forum/viewtopic.php?p=37780#p37780
  4. @echo off
  5. setlocal
  6. cls
  7. set "POWERSHELL_BAT_ARGS=%*"
  8. if defined POWERSHELL_BAT_ARGS set "POWERSHELL_BAT_ARGS=%POWERSHELL_BAT_ARGS:"=\"%"
  9. endlocal & powershell -NoLogo -NoProfile -Command "$_ = $input; Invoke-Expression $( '$input = $_; $_ = \"\"; $args = @( &{ $args } %POWERSHELL_BAT_ARGS% );' + [String]::Join( [char]10, $( Get-Content \"%~f0\" ) ) )"
  10. goto :EOF
  11. #>
  12.  
  13. # Add the relevant section of the Win32 API to the PowerShell session
  14. # Allows windows to be moved and resized
  15. Add-Type @"
  16.     using System;
  17.     using System.Runtime.InteropServices;
  18.    
  19.     public class Win32 {
  20.         [DllImport("user32.dll")]
  21.         [return: MarshalAs(UnmanagedType.Bool)]
  22.         public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
  23.     }
  24. "@
  25.  
  26. ################################################################################
  27. # Moves and resizes the window based the broswer
  28. #
  29. # Arguments: $browser - the browser being moved and resized
  30. # Returns:   None
  31. ################################################################################
  32. Function MoveAndResize ($browser)
  33. {
  34.     # $browser_path is the full path to the browser
  35.     # $screen_x is the horizontal location of the window on the screen
  36.     # $screen_y is the vertical location of the window on the screen
  37.     # $win_x is the width of the target window
  38.     # $win_y is the height of the target window
  39.     Switch($browser){
  40.         InternetExplorer{
  41.             $browser_path="C:\Program Files\Internet Explorer\IEXPLORE.EXE"
  42.             $screen_x = 0
  43.             $screen_y = 0
  44.             $win_x = 960
  45.             $win_y = 1080
  46.             break
  47.         }
  48.         Firefox{
  49.             $browser_path="C:\Program Files (x86)\Mozilla Firefox\firefox.exe"
  50.             $screen_x = 960
  51.             $screen_y = 0
  52.             $win_x = 960
  53.             $win_y = 1080
  54.             break
  55.         }
  56.         default {continue}
  57.     }
  58.    
  59.     # Start the desired browser
  60.     Start-Process $browser_path
  61.    
  62.     # Wait one second until the browser is fully loaded
  63.     Start-Sleep -S 1
  64.    
  65.     # Find the running process where the application path matches $browser_path
  66.     $browser = (Get-Process | where {$_.Path -eq $browser_path}).MainWindowHandle
  67.    
  68.     [Win32]::MoveWindow($browser, $screen_x, $screen_y, $win_x, $win_y, $true)
  69. }
  70.  
  71. MoveAndResize "InternetExplorer"
  72. MoveAndResize "Firefox"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement