Skip to content

Instantly share code, notes, and snippets.

@esperecyan
Last active August 6, 2022 01:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save esperecyan/ca1468473e33bd30c4f490d08f277d45 to your computer and use it in GitHub Desktop.
Save esperecyan/ca1468473e33bd30c4f490d08f277d45 to your computer and use it in GitHub Desktop.
『suppress-auto-lock.ps1.jse』 指定したプロセスが起動中、画面の自動ロックを抑制します。 使い方: https://pokemori.booth.pm/items/1790917
#@~^AQAAAA==~IAAAAA==^#~@ function toPSString(str) { return "'" + str.replace(/%/g, '"%"').replace(/'/g, "''") + "'"; } /* -*- mode: powershell;-*-
<#*/ var command = 'param($Names, $IntervalSeconds = 60)'
+ '; $_PSCommandPath = ' + toPSString(WSH.ScriptFullName)
+ '; Invoke-Expression (Get-Content ' + toPSString(WSH.ScriptFullName) + ' -Encoding UTF8 -Raw)';
var namePattern = /^-(?!(?:b(?:and|or|xor|not)|sh[lr]|[ic]?(?:eq|ne|gt|ge|lt|le|(?:not)?(?:like|match|contains|in)|replace|split)|join|is(?:not)?|as|and|or|not|f)$)[0-9a-z]+$/i;
var args = ''; for (var i = 0; i < WSH.Arguments.Length; i++) {
var arg = WSH.Arguments(i); args += ' ' + (namePattern.test(arg) ? arg : toPSString(arg)); }
WSH.CreateObject('WScript.Shell').Run('PowerShell -Command &{' + command + '}' + args, 0); /*#>
<#
.SYNOPSIS
指定したプロセスが起動中、画面の自動ロックを抑制します。
.DESCRIPTION
スクリーンセーバーの「再開時にログイン画面に戻る」について、指定したプロセスが起動中はチェックを外し、起動していないときにはチェックを入れます。
通知領域 (タスクトレイ) のアイコンから終了できます。
ショートカットを作成し、以下の例のように「-Names」パラメータを追加して、スタートアップフォルダなどに置いておきます。
SPDX-License-Identifier: MPL-2.0
Version: 1.0.2
Author: 100の人
配布元: <https://gist.github.com/esperecyan>
【更新履歴】
v1.0.2 (2022-08-06)
タスクトレイなどに表示されるスクリプト名の誤りを修正
v1.0.1 (2020-01-21)
設定変更が適用されない問題を緩和
v1.0.0 (2020-01-20)
公開
.EXAMPLE
suppress-auto-lock.ps1.jse -Names vrserver
→ SteamVRが起動中、を抑制します。
.PARAMETER Names
拡張子を除いたプロセス名を、「,」区切りで指定します。
「,」を含むプロセス名などには対応していません。
.PARAMETER IntervalSeconds
チェック間隔。1以上の秒数。既定値は1分。
#>
using namespace System.Linq
using namespace System.Drawing
using namespace System.Windows.Forms
using namespace System.Management.Automation
using namespace Esperecyan.Net
# レジストリ設定反映コマンドの繰り返し回数 (1回では反映されないことがあるため)
$UPDATE_PER_USER_SYSTEM_PARAMETERS_REPEAT = 100
<#
.SYNOPSIS
例外の詳細を警告ダイアログで表示し、スクリプトを終了します。
.PARAMETER ErrorRecord
catch { } 内の $_ 。
#>
function Exit-Script([ErrorRecord]$ErrorRecord)
{
[MessageBox]::Show(
"例外が発生しました。スクリプトを終了します。`n`n【例外メッセージ】(Ctrl+Cでコピー可能)`n$('-' * 78)`n" `
+ "$($ErrorRecord | Out-String)`nScriptStackTrace:`n$($ErrorRecord.ScriptStackTrace)",
$SCRIPT_NAME,
[MessageBoxButtons]::OK,
[MessageBoxIcon]::Error,
[MessageBoxDefaultButton]::Button1,
[MessageBoxOptions]::DefaultDesktopOnly
) | Out-Null
try {
$notifyIcon.Visible = $false
} catch {
}
[Environment]::Exit(0)
}
try { Set-StrictMode -Version Latest; $ErrorActionPreference = 'Stop';
Add-Type -AssemblyName @('System.Drawing', 'System.Windows.Forms')
Add-Type -Name ('ShellAPI') -Namespace 'Esperecyan.Net' -MemberDefinition @('
[DllImport("shell32.dll")]
public static extern int ExtractIconEx(
string lpszFile,
int nIconIndex,
out System.IntPtr phiconLarge,
out System.IntPtr phiconSmall,
int nIcons
);
')
<#
.SYNOPSIS
ファイルからアイコンを取得します。
.PARAMETER Path
「shell32.dll」のようなファイルパス。
.PARAMETER Index
0から始まる、ファイル内のアイコンのインデックス。
.OUTPUTS
アイコン。
#>
function Get-IconFromFile
{
[OutputType([Icon])]
param([Parameter(Mandatory)][string]$Path, [Parameter(Mandatory)][int]$Index)
$phiconLarge = New-Object IntPtr
$phiconSmall = New-Object IntPtr
[ShellAPI]::ExtractIconEx($Path, $Index, [ref]$phiconLarge, [ref]$phiconSmall, 1) | Out-Null
[Icon]::FromHandle($phiconSmall).Dispose()
$icon = [Icon]::FromHandle($phiconLarge)
$icon.Dispose()
return $icon
}
$SCRIPT_NAME = 'suppress-auto-lock.ps1.jse'
$ENABLED_AUTO_LOCK_ICON = Get-IconFromFile -Path 'shell32.dll' -Index 211
$bitmap = $ENABLED_AUTO_LOCK_ICON.ToBitmap()
$graphics = [Graphics]::FromImage($bitmap)
$graphics.DrawIcon((Get-IconFromFile -Path 'shell32.dll' -Index 109), 0, 0)
$graphics.Dispose()
$DISABLED_AUTO_LOCK_ICON = [Icon]::FromHandle($bitmap.GetHicon())
$bitmap.Dispose()
<#
.SYNOPSIS
イベントを妨げない状態で、スクリプトを終了させずに待機します。
#>
function Wait-Exit
{
$form = New-Object Form
$form.Text = $SCRIPT_NAME
$form.ShowInTaskbar = $false
$form.StartPosition = [FormStartPosition]::Manual
$form.Location = New-Object Point([int]::MaxValue, [int]::MaxValue)
$form.Add_Closed($exit)
$form.ShowDialog()
}
$processNames = $Names -split ','
$exit = {
$timer.Stop()
& $confirmProcesses
$notifyIcon.Visible = $false
[Environment]::Exit(0)
}
$notifyIcon = New-Object NotifyIcon
$notifyIcon.Icon = $ENABLED_AUTO_LOCK_ICON
$notifyIcon.Text = $SCRIPT_NAME
$contextMenu = New-Object ContextMenuStrip
$contextMenu.Items.Add((New-Object ToolStripMenuItem('終了', $null, { & $exit })))
$notifyIcon.ContextMenuStrip = $contextMenu
$notifyIcon.Visible = $true
$confirmProcesses = {
try {
$isSecure = '1'
$icon = $ENABLED_AUTO_LOCK_ICON
foreach ($process in (Get-Process)) {
if ($process.Name -in $processNames) {
$isSecure = '0'
$icon = $DISABLED_AUTO_LOCK_ICON
break
}
}
if ((Get-ItemProperty -Path @('HKCU:\Control Panel\Desktop') -Name 'ScreenSaverIsSecure').ScreenSaverIsSecure `
-ne $isSecure) {
Set-ItemProperty -Path @('HKCU:\Control Panel\Desktop') -Name 'ScreenSaverIsSecure' -Value $isSecure
for ($i = 0; $i -lt $UPDATE_PER_USER_SYSTEM_PARAMETERS_REPEAT; $i++) {
rundll32 user32.dll,UpdatePerUserSystemParameters 1, True
}
}
$notifyIcon.Icon = $icon
} catch {
Exit-Script -ErrorRecord $_
}
}
$timer = New-Object Timer
$timer.Interval = [int]$IntervalSeconds * 1000
$timer.Add_Tick($confirmProcesses)
$timer.Start()
# 待機
Wait-Exit
} catch {
Exit-Script -ErrorRecord $_
} # */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment