Skip to content

Commit

Permalink
New scripts for reactivate window scenario.
Browse files Browse the repository at this point in the history
  • Loading branch information
sw3103 committed Jan 28, 2023
1 parent 6dbcebf commit f6304fe
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
18 changes: 18 additions & 0 deletions Utils/Activate Window.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# https://github.com/sw3103/movemouse/wiki/Scenarios#reactivate-previous-active-window-on-pausestop

$TextFilePath = Join-Path -Path:$env:TEMP -ChildPath:"MoveMouseWindowPid.txt"

if (Test-Path -Path:$TextFilePath) {
$WindowPid = Get-Content -Path:$TextFilePath

if (![System.String]::IsNullOrWhiteSpace($WindowPid)) {
Add-Type -AssemblyName:"Microsoft.VisualBasic"
[Microsoft.VisualBasic.Interaction]::AppActivate([int]$WindowPid)
}
else {
throw "$TextFilePath was empty."
}
}
else {
throw "$TextFilePath does not exist. You need to run this script with the -Capture parameter first."
}
28 changes: 28 additions & 0 deletions Utils/Capture Active Window.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# https://github.com/sw3103/movemouse/wiki/Scenarios#reactivate-previous-active-window-on-pausestop

Add-Type -TypeDefinition @"
using System;
using System.Runtime.InteropServices;
public class NativeMethods
{
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", SetLastError=true)]
public static extern uint GetWindowThreadProcessId(
IntPtr hWnd,
out uint lpdwProcessId);
}
"@

$TextFilePath = Join-Path -Path:$env:TEMP -ChildPath:"MoveMouseWindowPid.txt"
$Hwnd = [NativeMethods]::GetForegroundWindow()
$WindowPid = 0

if ([NativeMethods]::GetWindowThreadProcessId($Hwnd, [ref]$WindowPid) -gt 0) {
$WindowPid | Out-File -FilePath:$TextFilePath -Append:$false -Force
}
else {
throw "Error capturing active window process ID."
}
4 changes: 3 additions & 1 deletion Utils/Watch Idle TIme.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
Add-Type @'
# https://github.com/sw3103/movemouse/wiki/Troubleshooting#idle-time

Add-Type @'
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
Expand Down

0 comments on commit f6304fe

Please sign in to comment.