Skip to content

Instantly share code, notes, and snippets.

@pypykim
Forked from typcn/lock.m
Created March 29, 2016 06:59
Show Gist options
  • Select an option

  • Save pypykim/92665b39970ea559743d to your computer and use it in GitHub Desktop.

Select an option

Save pypykim/92665b39970ea559743d to your computer and use it in GitHub Desktop.

Revisions

  1. @typcn typcn created this gist Mar 29, 2016.
    3 changes: 3 additions & 0 deletions lock.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    extern void SACLockScreenImmediate ( );

    SACLockScreenImmediate();
    18 changes: 18 additions & 0 deletions unlock.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    io_registry_entry_t r = IORegistryEntryFromPath(kIOMasterPortDefault, "IOService:/IOResources/IODisplayWrangler");
    if (r) {
    IORegistryEntrySetCFProperty(r, CFSTR("IORequestIdle"), kCFBooleanFalse);
    IOObjectRelease(r);
    }

    NSString *pw = [SSKeychain passwordForService:@"login" account:@"login"];

    NSString *s = @"tell application \"System Events\"\n\
    tell application \"ScreenSaverEngine\" to quit\n\
    delay 0.5\n\
    keystroke \"%@\"\n\
    keystroke return\n\
    end tell";

    NSDictionary *err;
    NSAppleScript *script = [[NSAppleScript alloc] initWithSource:[NSString stringWithFormat:s,pw]];
    [script executeAndReturnError:&err];