Open firefox on top of everything with PowerShell












0














I´m running two scripts.



. C:path/Set-Window.ps1
Start-Process -FilePath 'C:/Program Files/Mozilla Firefox/firefox.exe' -ArgumentList https://stackoverflow.com
Start-Sleep -Seconds 0
Set-Window -ProcessName firefox -x 194 -y 18 -Width 1180 -Height 715 -Passthru


With Set-Window.ps1 being:



Function Set-Window {
<#
.SYNOPSIS
Sets the window size (height,width) and coordinates (x,y) of
a process window.

.DESCRIPTION
Sets the window size (height,width) and coordinates (x,y) of
a process window.

.PARAMETER ProcessName
Name of the process to determine the window characteristics

.PARAMETER X
Set the position of the window in pixels from the top.

.PARAMETER Y
Set the position of the window in pixels from the left.

.PARAMETER Width
Set the width of the window.

.PARAMETER Height
Set the height of the window.

.PARAMETER Passthru
Display the output object of the window.

.NOTES
Name: Set-Window
Author: Boe Prox
Version History
1.0//Boe Prox - 11/24/2015
- Initial build
1.1//JosefZ (https://superuser.com/users/376602/josefz) - 19.05.2018
- treats more process instances of supplied process name properly

.OUTPUT
System.Automation.WindowInfo

.EXAMPLE
Get-Process powershell | Set-Window -X 2040 -Y 142 -Passthru

ProcessName Size TopLeft BottomRight
----------- ---- ------- -----------
powershell 1262,642 2040,142 3302,784

Description
-----------
Set the coordinates on the window for the process PowerShell.exe

#>
[OutputType('System.Automation.WindowInfo')]
[cmdletbinding()]
Param (
[parameter(ValueFromPipelineByPropertyName=$True)]
$ProcessName,
[int]$X,
[int]$Y,
[int]$Width,
[int]$Height,
[switch]$Passthru
)
Begin {
Try{
[void][Window]
} Catch {
Add-Type @"
using System;
using System.Runtime.InteropServices;
public class Window {
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);

[DllImport("User32.dll")]
public extern static bool MoveWindow(IntPtr handle, int x, int y, int width, int height, bool redraw);
}
public struct RECT
{
public int Left; // x position of upper-left corner
public int Top; // y position of upper-left corner
public int Right; // x position of lower-right corner
public int Bottom; // y position of lower-right corner
}
"@
}
}
Process {
$Rectangle = New-Object RECT
$Handles = (Get-Process -Name $ProcessName).MainWindowHandle ### 1.1//JosefZ
foreach ( $Handle in $Handles ) { ### 1.1//JosefZ
if ( $Handle -eq [System.IntPtr]::Zero ) { Continue } ### 1.1//JosefZ
$Return = [Window]::GetWindowRect($Handle,[ref]$Rectangle)
If (-NOT $PSBoundParameters.ContainsKey('Width')) {
$Width = $Rectangle.Right - $Rectangle.Left
}
If (-NOT $PSBoundParameters.ContainsKey('Height')) {
$Height = $Rectangle.Bottom - $Rectangle.Top
}
If ($Return) {
$Return = [Window]::MoveWindow($Handle, $x, $y, $Width, $Height,$True)
}
If ($PSBoundParameters.ContainsKey('Passthru')) {
$Rectangle = New-Object RECT
$Return = [Window]::GetWindowRect($Handle,[ref]$Rectangle)
If ($Return) {
$Height = $Rectangle.Bottom - $Rectangle.Top
$Width = $Rectangle.Right - $Rectangle.Left
$Size = New-Object System.Management.Automation.Host.Size -ArgumentList $Width, $Height
$TopLeft = New-Object System.Management.Automation.Host.Coordinates -ArgumentList $Rectangle.Left, $Rectangle.Top
$BottomRight = New-Object System.Management.Automation.Host.Coordinates -ArgumentList $Rectangle.Right, $Rectangle.Bottom
If ($Rectangle.Top -lt 0 -AND $Rectangle.LEft -lt 0) {
Write-Warning "Window is minimized! Coordinates will not be accurate."
}
$Object = [pscustomobject]@{
ProcessName = $ProcessName
Size = $Size
TopLeft = $TopLeft
BottomRight = $BottomRight
}
$Object.PSTypeNames.insert(0,'System.Automation.WindowInfo')
$Object
}
}
}
}
}


All of this to open firefox in a specific position on screen. But I also want it to open on top of all the other windows that I have at the moment of execution.



What can I add to the scripts to make this work?










share|improve this question






















  • This happens by default, because starting a new process, especially an .exe is always the active window. If that is not happening for you, then that sounds very odd. What is your goal / use case for this kind of thing, since you are saying you are in a full desktop interactive session, vs just starting FF directly?
    – postanote
    Nov 30 at 21:26
















0














I´m running two scripts.



. C:path/Set-Window.ps1
Start-Process -FilePath 'C:/Program Files/Mozilla Firefox/firefox.exe' -ArgumentList https://stackoverflow.com
Start-Sleep -Seconds 0
Set-Window -ProcessName firefox -x 194 -y 18 -Width 1180 -Height 715 -Passthru


With Set-Window.ps1 being:



Function Set-Window {
<#
.SYNOPSIS
Sets the window size (height,width) and coordinates (x,y) of
a process window.

.DESCRIPTION
Sets the window size (height,width) and coordinates (x,y) of
a process window.

.PARAMETER ProcessName
Name of the process to determine the window characteristics

.PARAMETER X
Set the position of the window in pixels from the top.

.PARAMETER Y
Set the position of the window in pixels from the left.

.PARAMETER Width
Set the width of the window.

.PARAMETER Height
Set the height of the window.

.PARAMETER Passthru
Display the output object of the window.

.NOTES
Name: Set-Window
Author: Boe Prox
Version History
1.0//Boe Prox - 11/24/2015
- Initial build
1.1//JosefZ (https://superuser.com/users/376602/josefz) - 19.05.2018
- treats more process instances of supplied process name properly

.OUTPUT
System.Automation.WindowInfo

.EXAMPLE
Get-Process powershell | Set-Window -X 2040 -Y 142 -Passthru

ProcessName Size TopLeft BottomRight
----------- ---- ------- -----------
powershell 1262,642 2040,142 3302,784

Description
-----------
Set the coordinates on the window for the process PowerShell.exe

#>
[OutputType('System.Automation.WindowInfo')]
[cmdletbinding()]
Param (
[parameter(ValueFromPipelineByPropertyName=$True)]
$ProcessName,
[int]$X,
[int]$Y,
[int]$Width,
[int]$Height,
[switch]$Passthru
)
Begin {
Try{
[void][Window]
} Catch {
Add-Type @"
using System;
using System.Runtime.InteropServices;
public class Window {
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);

[DllImport("User32.dll")]
public extern static bool MoveWindow(IntPtr handle, int x, int y, int width, int height, bool redraw);
}
public struct RECT
{
public int Left; // x position of upper-left corner
public int Top; // y position of upper-left corner
public int Right; // x position of lower-right corner
public int Bottom; // y position of lower-right corner
}
"@
}
}
Process {
$Rectangle = New-Object RECT
$Handles = (Get-Process -Name $ProcessName).MainWindowHandle ### 1.1//JosefZ
foreach ( $Handle in $Handles ) { ### 1.1//JosefZ
if ( $Handle -eq [System.IntPtr]::Zero ) { Continue } ### 1.1//JosefZ
$Return = [Window]::GetWindowRect($Handle,[ref]$Rectangle)
If (-NOT $PSBoundParameters.ContainsKey('Width')) {
$Width = $Rectangle.Right - $Rectangle.Left
}
If (-NOT $PSBoundParameters.ContainsKey('Height')) {
$Height = $Rectangle.Bottom - $Rectangle.Top
}
If ($Return) {
$Return = [Window]::MoveWindow($Handle, $x, $y, $Width, $Height,$True)
}
If ($PSBoundParameters.ContainsKey('Passthru')) {
$Rectangle = New-Object RECT
$Return = [Window]::GetWindowRect($Handle,[ref]$Rectangle)
If ($Return) {
$Height = $Rectangle.Bottom - $Rectangle.Top
$Width = $Rectangle.Right - $Rectangle.Left
$Size = New-Object System.Management.Automation.Host.Size -ArgumentList $Width, $Height
$TopLeft = New-Object System.Management.Automation.Host.Coordinates -ArgumentList $Rectangle.Left, $Rectangle.Top
$BottomRight = New-Object System.Management.Automation.Host.Coordinates -ArgumentList $Rectangle.Right, $Rectangle.Bottom
If ($Rectangle.Top -lt 0 -AND $Rectangle.LEft -lt 0) {
Write-Warning "Window is minimized! Coordinates will not be accurate."
}
$Object = [pscustomobject]@{
ProcessName = $ProcessName
Size = $Size
TopLeft = $TopLeft
BottomRight = $BottomRight
}
$Object.PSTypeNames.insert(0,'System.Automation.WindowInfo')
$Object
}
}
}
}
}


All of this to open firefox in a specific position on screen. But I also want it to open on top of all the other windows that I have at the moment of execution.



What can I add to the scripts to make this work?










share|improve this question






















  • This happens by default, because starting a new process, especially an .exe is always the active window. If that is not happening for you, then that sounds very odd. What is your goal / use case for this kind of thing, since you are saying you are in a full desktop interactive session, vs just starting FF directly?
    – postanote
    Nov 30 at 21:26














0












0








0







I´m running two scripts.



. C:path/Set-Window.ps1
Start-Process -FilePath 'C:/Program Files/Mozilla Firefox/firefox.exe' -ArgumentList https://stackoverflow.com
Start-Sleep -Seconds 0
Set-Window -ProcessName firefox -x 194 -y 18 -Width 1180 -Height 715 -Passthru


With Set-Window.ps1 being:



Function Set-Window {
<#
.SYNOPSIS
Sets the window size (height,width) and coordinates (x,y) of
a process window.

.DESCRIPTION
Sets the window size (height,width) and coordinates (x,y) of
a process window.

.PARAMETER ProcessName
Name of the process to determine the window characteristics

.PARAMETER X
Set the position of the window in pixels from the top.

.PARAMETER Y
Set the position of the window in pixels from the left.

.PARAMETER Width
Set the width of the window.

.PARAMETER Height
Set the height of the window.

.PARAMETER Passthru
Display the output object of the window.

.NOTES
Name: Set-Window
Author: Boe Prox
Version History
1.0//Boe Prox - 11/24/2015
- Initial build
1.1//JosefZ (https://superuser.com/users/376602/josefz) - 19.05.2018
- treats more process instances of supplied process name properly

.OUTPUT
System.Automation.WindowInfo

.EXAMPLE
Get-Process powershell | Set-Window -X 2040 -Y 142 -Passthru

ProcessName Size TopLeft BottomRight
----------- ---- ------- -----------
powershell 1262,642 2040,142 3302,784

Description
-----------
Set the coordinates on the window for the process PowerShell.exe

#>
[OutputType('System.Automation.WindowInfo')]
[cmdletbinding()]
Param (
[parameter(ValueFromPipelineByPropertyName=$True)]
$ProcessName,
[int]$X,
[int]$Y,
[int]$Width,
[int]$Height,
[switch]$Passthru
)
Begin {
Try{
[void][Window]
} Catch {
Add-Type @"
using System;
using System.Runtime.InteropServices;
public class Window {
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);

[DllImport("User32.dll")]
public extern static bool MoveWindow(IntPtr handle, int x, int y, int width, int height, bool redraw);
}
public struct RECT
{
public int Left; // x position of upper-left corner
public int Top; // y position of upper-left corner
public int Right; // x position of lower-right corner
public int Bottom; // y position of lower-right corner
}
"@
}
}
Process {
$Rectangle = New-Object RECT
$Handles = (Get-Process -Name $ProcessName).MainWindowHandle ### 1.1//JosefZ
foreach ( $Handle in $Handles ) { ### 1.1//JosefZ
if ( $Handle -eq [System.IntPtr]::Zero ) { Continue } ### 1.1//JosefZ
$Return = [Window]::GetWindowRect($Handle,[ref]$Rectangle)
If (-NOT $PSBoundParameters.ContainsKey('Width')) {
$Width = $Rectangle.Right - $Rectangle.Left
}
If (-NOT $PSBoundParameters.ContainsKey('Height')) {
$Height = $Rectangle.Bottom - $Rectangle.Top
}
If ($Return) {
$Return = [Window]::MoveWindow($Handle, $x, $y, $Width, $Height,$True)
}
If ($PSBoundParameters.ContainsKey('Passthru')) {
$Rectangle = New-Object RECT
$Return = [Window]::GetWindowRect($Handle,[ref]$Rectangle)
If ($Return) {
$Height = $Rectangle.Bottom - $Rectangle.Top
$Width = $Rectangle.Right - $Rectangle.Left
$Size = New-Object System.Management.Automation.Host.Size -ArgumentList $Width, $Height
$TopLeft = New-Object System.Management.Automation.Host.Coordinates -ArgumentList $Rectangle.Left, $Rectangle.Top
$BottomRight = New-Object System.Management.Automation.Host.Coordinates -ArgumentList $Rectangle.Right, $Rectangle.Bottom
If ($Rectangle.Top -lt 0 -AND $Rectangle.LEft -lt 0) {
Write-Warning "Window is minimized! Coordinates will not be accurate."
}
$Object = [pscustomobject]@{
ProcessName = $ProcessName
Size = $Size
TopLeft = $TopLeft
BottomRight = $BottomRight
}
$Object.PSTypeNames.insert(0,'System.Automation.WindowInfo')
$Object
}
}
}
}
}


All of this to open firefox in a specific position on screen. But I also want it to open on top of all the other windows that I have at the moment of execution.



What can I add to the scripts to make this work?










share|improve this question













I´m running two scripts.



. C:path/Set-Window.ps1
Start-Process -FilePath 'C:/Program Files/Mozilla Firefox/firefox.exe' -ArgumentList https://stackoverflow.com
Start-Sleep -Seconds 0
Set-Window -ProcessName firefox -x 194 -y 18 -Width 1180 -Height 715 -Passthru


With Set-Window.ps1 being:



Function Set-Window {
<#
.SYNOPSIS
Sets the window size (height,width) and coordinates (x,y) of
a process window.

.DESCRIPTION
Sets the window size (height,width) and coordinates (x,y) of
a process window.

.PARAMETER ProcessName
Name of the process to determine the window characteristics

.PARAMETER X
Set the position of the window in pixels from the top.

.PARAMETER Y
Set the position of the window in pixels from the left.

.PARAMETER Width
Set the width of the window.

.PARAMETER Height
Set the height of the window.

.PARAMETER Passthru
Display the output object of the window.

.NOTES
Name: Set-Window
Author: Boe Prox
Version History
1.0//Boe Prox - 11/24/2015
- Initial build
1.1//JosefZ (https://superuser.com/users/376602/josefz) - 19.05.2018
- treats more process instances of supplied process name properly

.OUTPUT
System.Automation.WindowInfo

.EXAMPLE
Get-Process powershell | Set-Window -X 2040 -Y 142 -Passthru

ProcessName Size TopLeft BottomRight
----------- ---- ------- -----------
powershell 1262,642 2040,142 3302,784

Description
-----------
Set the coordinates on the window for the process PowerShell.exe

#>
[OutputType('System.Automation.WindowInfo')]
[cmdletbinding()]
Param (
[parameter(ValueFromPipelineByPropertyName=$True)]
$ProcessName,
[int]$X,
[int]$Y,
[int]$Width,
[int]$Height,
[switch]$Passthru
)
Begin {
Try{
[void][Window]
} Catch {
Add-Type @"
using System;
using System.Runtime.InteropServices;
public class Window {
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);

[DllImport("User32.dll")]
public extern static bool MoveWindow(IntPtr handle, int x, int y, int width, int height, bool redraw);
}
public struct RECT
{
public int Left; // x position of upper-left corner
public int Top; // y position of upper-left corner
public int Right; // x position of lower-right corner
public int Bottom; // y position of lower-right corner
}
"@
}
}
Process {
$Rectangle = New-Object RECT
$Handles = (Get-Process -Name $ProcessName).MainWindowHandle ### 1.1//JosefZ
foreach ( $Handle in $Handles ) { ### 1.1//JosefZ
if ( $Handle -eq [System.IntPtr]::Zero ) { Continue } ### 1.1//JosefZ
$Return = [Window]::GetWindowRect($Handle,[ref]$Rectangle)
If (-NOT $PSBoundParameters.ContainsKey('Width')) {
$Width = $Rectangle.Right - $Rectangle.Left
}
If (-NOT $PSBoundParameters.ContainsKey('Height')) {
$Height = $Rectangle.Bottom - $Rectangle.Top
}
If ($Return) {
$Return = [Window]::MoveWindow($Handle, $x, $y, $Width, $Height,$True)
}
If ($PSBoundParameters.ContainsKey('Passthru')) {
$Rectangle = New-Object RECT
$Return = [Window]::GetWindowRect($Handle,[ref]$Rectangle)
If ($Return) {
$Height = $Rectangle.Bottom - $Rectangle.Top
$Width = $Rectangle.Right - $Rectangle.Left
$Size = New-Object System.Management.Automation.Host.Size -ArgumentList $Width, $Height
$TopLeft = New-Object System.Management.Automation.Host.Coordinates -ArgumentList $Rectangle.Left, $Rectangle.Top
$BottomRight = New-Object System.Management.Automation.Host.Coordinates -ArgumentList $Rectangle.Right, $Rectangle.Bottom
If ($Rectangle.Top -lt 0 -AND $Rectangle.LEft -lt 0) {
Write-Warning "Window is minimized! Coordinates will not be accurate."
}
$Object = [pscustomobject]@{
ProcessName = $ProcessName
Size = $Size
TopLeft = $TopLeft
BottomRight = $BottomRight
}
$Object.PSTypeNames.insert(0,'System.Automation.WindowInfo')
$Object
}
}
}
}
}


All of this to open firefox in a specific position on screen. But I also want it to open on top of all the other windows that I have at the moment of execution.



What can I add to the scripts to make this work?







windows powershell






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 30 at 17:37









daniel gon

1




1












  • This happens by default, because starting a new process, especially an .exe is always the active window. If that is not happening for you, then that sounds very odd. What is your goal / use case for this kind of thing, since you are saying you are in a full desktop interactive session, vs just starting FF directly?
    – postanote
    Nov 30 at 21:26


















  • This happens by default, because starting a new process, especially an .exe is always the active window. If that is not happening for you, then that sounds very odd. What is your goal / use case for this kind of thing, since you are saying you are in a full desktop interactive session, vs just starting FF directly?
    – postanote
    Nov 30 at 21:26
















This happens by default, because starting a new process, especially an .exe is always the active window. If that is not happening for you, then that sounds very odd. What is your goal / use case for this kind of thing, since you are saying you are in a full desktop interactive session, vs just starting FF directly?
– postanote
Nov 30 at 21:26




This happens by default, because starting a new process, especially an .exe is always the active window. If that is not happening for you, then that sounds very odd. What is your goal / use case for this kind of thing, since you are saying you are in a full desktop interactive session, vs just starting FF directly?
– postanote
Nov 30 at 21:26










1 Answer
1






active

oldest

votes


















0














Issue an AppActivate command after your other commands.



(New-Object -ComObject WScript.Shell).AppActivate((Get-Process firefox).MainWindowTitle)


Source






share|improve this answer





















    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "3"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1379790%2fopen-firefox-on-top-of-everything-with-powershell%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Issue an AppActivate command after your other commands.



    (New-Object -ComObject WScript.Shell).AppActivate((Get-Process firefox).MainWindowTitle)


    Source






    share|improve this answer


























      0














      Issue an AppActivate command after your other commands.



      (New-Object -ComObject WScript.Shell).AppActivate((Get-Process firefox).MainWindowTitle)


      Source






      share|improve this answer
























        0












        0








        0






        Issue an AppActivate command after your other commands.



        (New-Object -ComObject WScript.Shell).AppActivate((Get-Process firefox).MainWindowTitle)


        Source






        share|improve this answer












        Issue an AppActivate command after your other commands.



        (New-Object -ComObject WScript.Shell).AppActivate((Get-Process firefox).MainWindowTitle)


        Source







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 3 at 18:50









        root

        2,31251535




        2,31251535






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Super User!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1379790%2fopen-firefox-on-top-of-everything-with-powershell%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Сан-Квентин

            8-я гвардейская общевойсковая армия

            Алькесар