Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* 2>nul || title LockScreen_Mute.bat by AveYo v2018.11.20
- @echo off
- :: Compile Mute.cs snippet
- mkdir %APPDATA%\AveYo 2>nul
- pushd %APPDATA%\AveYo
- del /f /q Mute.exe >nul 2>nul
- for /f "tokens=* delims=" %%v in ('dir /b /s /a:-d /o:-n "%Windir%\Microsoft.NET\Framework\*csc.exe"') do set "csc="%%v""
- %csc% /out:Mute.exe /target:winexe /platform:anycpu /optimize /nologo "%~f0"
- if not exist Mute.exe echo ERROR! Failed compiling c# snippet & timeout /t -1 & exit /b
- :: Create scheduled tasks to run Mute on Lock and Unlock
- powershell -c "iex(([io.file]::ReadAllText('%~f0')-split':PS_MUTETASKS\:.*')[1]);"
- :: Test task
- schtasks /run /TN Mute_Lock
- timeout -1
- schtasks /run /TN Mute_UnLock
- :: Done!
- exit/b
- :PS_MUTETASKS:
- $state=@('Unlock','Lock'); 0..1 | foreach { Register-ScheduledTask -TaskName "Mute_$($state[$_])" -Force -Xml @"
- <?xml version="1.0" encoding="UTF-16"?>
- <Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
- <RegistrationInfo>
- <Author>AveYo</Author>
- <URI>\Mute_$($state[$_])</URI>
- </RegistrationInfo>
- <Triggers>
- <SessionStateChangeTrigger>
- <Enabled>true</Enabled>
- <StateChange>Session$($state[$_])</StateChange>
- <UserId>$((gwmi -query 'select UserName from Win32_ComputerSystem').UserName)</UserId>
- <Delay>PT$(2*$_+1)S</Delay>
- </SessionStateChangeTrigger>
- </Triggers>
- <Principals>
- <Principal id="Author">
- <UserId>$(([System.Security.Principal.NTAccount](gwmi -query 'select Username from Win32_ComputerSystem').UserName
- ).Translate([System.Security.Principal.SecurityIdentifier]).Value)</UserId>
- <LogonType>InteractiveToken</LogonType>
- <RunLevel>LeastPrivilege</RunLevel>
- </Principal>
- </Principals>
- <Settings>
- <MultipleInstancesPolicy>StopExisting</MultipleInstancesPolicy>
- <DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
- <StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>
- <AllowHardTerminate>true</AllowHardTerminate>
- <StartWhenAvailable>true</StartWhenAvailable>
- <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
- <IdleSettings>
- <StopOnIdleEnd>true</StopOnIdleEnd>
- <RestartOnIdle>false</RestartOnIdle>
- </IdleSettings>
- <AllowStartOnDemand>true</AllowStartOnDemand>
- <Enabled>true</Enabled>
- <Hidden>false</Hidden>
- <RunOnlyIfIdle>false</RunOnlyIfIdle>
- <WakeToRun>true</WakeToRun>
- <ExecutionTimeLimit>PT0S</ExecutionTimeLimit>
- <Priority>5</Priority>
- </Settings>
- <Actions Context="Author">
- <Exec>
- <Command>"%APPDATA%\AveYo\Mute.exe"</Command>
- <Arguments>$_</Arguments>
- </Exec>
- </Actions>
- </Task>
- "@ }
- :PS_MUTETASKS:
- Mute.cs */
- using System;
- using System.Runtime.InteropServices;
- using System.Reflection;
- [assembly:AssemblyTitle("Mute")]
- [assembly:AssemblyCompanyAttribute("AveYo")]
- [assembly:AssemblyVersionAttribute("2018.11.20")]
- namespace Mute
- {
- class Program
- {
- private const int WM_APPCOMMAND = 0x319;
- private const int APPCOMMAND_VOLUME_MUTE = 0x80000;
- private const int APPCOMMAND_VOLUME_UP = 0xA0000;
- private const int APPCOMMAND_VOLUME_DOWN = 0x90000;
- [DllImport("user32.dll", SetLastError = false)]
- public static extern IntPtr GetForegroundWindow();
- [DllImport("user32.dll", SetLastError = false)]
- public static extern IntPtr SendMessageW(IntPtr hWnd,int Msg,IntPtr wParam,IntPtr lParam);
- static void Main(string[] args)
- {
- bool Mute = (args.Length > 0 && args[0] == "1");
- bool Loud = (args.Length > 0 && args[0] == "0");
- try
- {
- IntPtr h = GetForegroundWindow();
- if (Mute)
- {
- SendMessageW(h,WM_APPCOMMAND,IntPtr.Zero,(IntPtr)APPCOMMAND_VOLUME_DOWN);
- SendMessageW(h,WM_APPCOMMAND,IntPtr.Zero,(IntPtr)APPCOMMAND_VOLUME_UP);
- SendMessageW(h,WM_APPCOMMAND,IntPtr.Zero,(IntPtr)APPCOMMAND_VOLUME_MUTE);
- }
- else if (Loud)
- {
- SendMessageW(h,WM_APPCOMMAND,IntPtr.Zero,(IntPtr)APPCOMMAND_VOLUME_DOWN);
- SendMessageW(h,WM_APPCOMMAND,IntPtr.Zero,(IntPtr)APPCOMMAND_VOLUME_UP);
- }
- else
- {
- SendMessageW(h,WM_APPCOMMAND,IntPtr.Zero,(IntPtr)APPCOMMAND_VOLUME_MUTE);
- }
- }
- catch (Exception)
- {
- }
- }
- }
- }
- /*_*/
Add Comment
Please, Sign In to add comment