aveyo

LockScreen_Mute

Nov 20th, 2018
758
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 4.31 KB | None | 0 0
  1. /* 2>nul || title LockScreen_Mute.bat by AveYo v2018.11.20
  2. @echo off
  3.  
  4. :: Compile Mute.cs snippet
  5. mkdir %APPDATA%\AveYo 2>nul
  6. pushd %APPDATA%\AveYo
  7. del /f /q Mute.exe >nul 2>nul
  8. for /f "tokens=* delims=" %%v in ('dir /b /s /a:-d /o:-n "%Windir%\Microsoft.NET\Framework\*csc.exe"') do set "csc="%%v""
  9. %csc% /out:Mute.exe /target:winexe /platform:anycpu /optimize /nologo "%~f0"
  10. if not exist Mute.exe echo ERROR! Failed compiling c# snippet & timeout /t -1 & exit /b
  11.  
  12. :: Create scheduled tasks to run Mute on Lock and Unlock
  13. powershell -c "iex(([io.file]::ReadAllText('%~f0')-split':PS_MUTETASKS\:.*')[1]);"
  14.  
  15. :: Test task
  16. schtasks /run /TN Mute_Lock
  17. timeout -1
  18. schtasks /run /TN Mute_UnLock
  19.  
  20. :: Done!
  21. exit/b
  22.  
  23. :PS_MUTETASKS:
  24. $state=@('Unlock','Lock'); 0..1 | foreach { Register-ScheduledTask -TaskName "Mute_$($state[$_])" -Force -Xml @"
  25. <?xml version="1.0" encoding="UTF-16"?>
  26. <Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  27.   <RegistrationInfo>
  28.     <Author>AveYo</Author>
  29.     <URI>\Mute_$($state[$_])</URI>
  30.   </RegistrationInfo>
  31.   <Triggers>
  32.     <SessionStateChangeTrigger>
  33.       <Enabled>true</Enabled>
  34.       <StateChange>Session$($state[$_])</StateChange>
  35.       <UserId>$((gwmi -query 'select UserName from Win32_ComputerSystem').UserName)</UserId>
  36.       <Delay>PT$(2*$_+1)S</Delay>
  37.     </SessionStateChangeTrigger>
  38.   </Triggers>
  39.   <Principals>
  40.     <Principal id="Author">
  41.       <UserId>$(([System.Security.Principal.NTAccount](gwmi -query 'select Username from Win32_ComputerSystem').UserName
  42.       ).Translate([System.Security.Principal.SecurityIdentifier]).Value)</UserId>
  43.       <LogonType>InteractiveToken</LogonType>
  44.       <RunLevel>LeastPrivilege</RunLevel>
  45.     </Principal>
  46.   </Principals>
  47.   <Settings>
  48.     <MultipleInstancesPolicy>StopExisting</MultipleInstancesPolicy>
  49.     <DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
  50.     <StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>
  51.     <AllowHardTerminate>true</AllowHardTerminate>
  52.     <StartWhenAvailable>true</StartWhenAvailable>
  53.     <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
  54.     <IdleSettings>
  55.       <StopOnIdleEnd>true</StopOnIdleEnd>
  56.       <RestartOnIdle>false</RestartOnIdle>
  57.     </IdleSettings>
  58.     <AllowStartOnDemand>true</AllowStartOnDemand>
  59.     <Enabled>true</Enabled>
  60.     <Hidden>false</Hidden>
  61.     <RunOnlyIfIdle>false</RunOnlyIfIdle>
  62.     <WakeToRun>true</WakeToRun>
  63.     <ExecutionTimeLimit>PT0S</ExecutionTimeLimit>
  64.     <Priority>5</Priority>
  65.   </Settings>
  66.   <Actions Context="Author">
  67.     <Exec>
  68.       <Command>"%APPDATA%\AveYo\Mute.exe"</Command>
  69.       <Arguments>$_</Arguments>
  70.     </Exec>
  71.   </Actions>
  72. </Task>
  73. "@ }
  74. :PS_MUTETASKS:
  75.  
  76. Mute.cs */
  77. using System;
  78. using System.Runtime.InteropServices;
  79. using System.Reflection;
  80.  
  81. [assembly:AssemblyTitle("Mute")]
  82. [assembly:AssemblyCompanyAttribute("AveYo")]
  83. [assembly:AssemblyVersionAttribute("2018.11.20")]
  84.  
  85. namespace Mute
  86. {
  87.   class Program
  88.   {
  89.     private const int WM_APPCOMMAND = 0x319;
  90.     private const int APPCOMMAND_VOLUME_MUTE = 0x80000;
  91.     private const int APPCOMMAND_VOLUME_UP = 0xA0000;
  92.     private const int APPCOMMAND_VOLUME_DOWN = 0x90000;
  93.     [DllImport("user32.dll", SetLastError = false)]
  94.     public static extern IntPtr GetForegroundWindow();
  95.     [DllImport("user32.dll", SetLastError = false)]
  96.     public static extern IntPtr SendMessageW(IntPtr hWnd,int Msg,IntPtr wParam,IntPtr lParam);
  97.     static void Main(string[] args)
  98.     {
  99.       bool Mute = (args.Length > 0 && args[0] == "1");
  100.       bool Loud = (args.Length > 0 && args[0] == "0");
  101.       try
  102.       {
  103.         IntPtr h = GetForegroundWindow();
  104.         if (Mute)
  105.         {
  106.           SendMessageW(h,WM_APPCOMMAND,IntPtr.Zero,(IntPtr)APPCOMMAND_VOLUME_DOWN);
  107.           SendMessageW(h,WM_APPCOMMAND,IntPtr.Zero,(IntPtr)APPCOMMAND_VOLUME_UP);
  108.           SendMessageW(h,WM_APPCOMMAND,IntPtr.Zero,(IntPtr)APPCOMMAND_VOLUME_MUTE);
  109.         }
  110.         else if (Loud)
  111.         {
  112.           SendMessageW(h,WM_APPCOMMAND,IntPtr.Zero,(IntPtr)APPCOMMAND_VOLUME_DOWN);
  113.           SendMessageW(h,WM_APPCOMMAND,IntPtr.Zero,(IntPtr)APPCOMMAND_VOLUME_UP);
  114.         }
  115.         else
  116.         {
  117.           SendMessageW(h,WM_APPCOMMAND,IntPtr.Zero,(IntPtr)APPCOMMAND_VOLUME_MUTE);
  118.         }
  119.       }
  120.       catch (Exception)
  121.       {
  122.       }
  123.     }
  124.   }
  125. }
  126. /*_*/
Add Comment
Please, Sign In to add comment