Only run script when pc is locked (lock screen)

Example scripts and tips (replaces Old Scripts & Tips archive)

Moderators: Dorian (MJT support), JRL, Phil Pendlebury

Post Reply
User avatar
Grovkillen
Automation Wizard
Posts: 1131
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Only run script when pc is locked (lock screen)

Post by Grovkillen » Wed Jan 17, 2024 5:11 pm

Here's a proof of concept to have a script run only when the pc is locked. I just look for the process "LogonUI.exe" which is the lock screen. If it's running then the pc is locked. I hope it's help someone.

Code: Select all

OnEvent>CUSTOM,LOOP_TO_CHECK_IF_LOCKED_WORKSTATION,LOGONUI_IS_ACTIVE,RUN_THIS_ONLY_WHEN_SCREEN_LOCKED

Label>start_of_main_loop
Wait>10
Goto>start_of_main_loop

SRT>LOOP_TO_CHECK_IF_LOCKED_WORKSTATION
  Let>PID_array_count=0
  GetProcessIDs>LogonUI.exe,PID_array
  If>PID_array_count>0
    Let>LOGONUI_IS_ACTIVE=true
  Else>
    Let>SECONDS_SINCE_LOCKED=0
    Let>LOGONUI_IS_ACTIVE=false
  Endif>
END>LOOP_TO_CHECK_IF_LOCKED_WORKSTATION

SRT>RUN_THIS_ONLY_WHEN_SCREEN_LOCKED
  Wait>1
  Add>SECONDS_SINCE_LOCKED,1
  Message>SECONDS_SINCE_LOCKED
END>RUN_THIS_ONLY_WHEN_SCREEN_LOCKED
Let>ME=%Script%

Running: 15.0.27
version history

User avatar
Grovkillen
Automation Wizard
Posts: 1131
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: Only run script when pc is locked (lock screen)

Post by Grovkillen » Wed Mar 20, 2024 9:26 pm

@Marcus: the GetProcessIDs>LogonUI.exe,PID_array will not return PID_array_count=0 if no processes are found. You might want to add that to the next release. Currently we need to add a Let>PID_array_count=0 before the call just to be sure.
Let>ME=%Script%

Running: 15.0.27
version history

User avatar
Marcus Tettmar
Site Admin
Posts: 7394
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: Only run script when pc is locked (lock screen)

Post by Marcus Tettmar » Thu Mar 21, 2024 3:12 pm

Noted, thanks.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
Grovkillen
Automation Wizard
Posts: 1131
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: Only run script when pc is locked (lock screen)

Post by Grovkillen » Sat Sep 21, 2024 5:37 pm

The issue with the array count missing was fixed in 15.0.25
Let>ME=%Script%

Running: 15.0.27
version history

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts