Page 1 of 1

Mac OS X - Send the OS to sleep

Posted: Sun Dec 06, 2015 2:27 am
by fsw

Code: Select all

RunProgram("/usr/bin/pmset", "sleepnow", "")

Re: Mac OS X - Send the OS to sleep

Posted: Sun Dec 06, 2015 3:35 am
by IdeasVacuum
... I though that was the default for Apple products :mrgreen:

Re: Mac OS X - Send the OS to sleep

Posted: Sun Dec 06, 2015 11:17 am
by davido
@fsw,
Nice demo. Thank you. :D

Re: Mac OS X - Send the OS to sleep

Posted: Sun Dec 06, 2015 11:30 am
by Wolfram
Thanks for this example.

Can you give us also an example to wakeup the computer. ;-)

Re: Mac OS X - Send the OS to sleep

Posted: Sun Dec 06, 2015 1:17 pm
by wilbert
Based on the source code of pmset

Code: Select all

; PB 5.40+

; For security purposes, caller must be root or the console user

#MACH_PORT_NULL = 0

ImportC "-framework IOKit"
  IOPMFindPowerManagement(master_device_port)
  IOPMSleepSystem(fb)
  IOServiceClose(connect)
EndImport

fb = IOPMFindPowerManagement(#MACH_PORT_NULL)
If #MACH_PORT_NULL <> fb
  err = IOPMSleepSystem(fb)
  If err
    Debug "error"
  EndIf
  IOServiceClose(fb)
EndIf

Re: Mac OS X - Send the OS to sleep

Posted: Sun Dec 06, 2015 2:11 pm
by Danilo
fsw wrote:In ObjectiveC:

Code: Select all

   NSTask *pmsetTask = [[NSTask alloc] init];
   pmsetTask.launchPath = @"/usr/bin/pmset";
   pmsetTask.arguments = @[@"sleepnow"];
   [pmsetTask launch];
In PureBasic:

Code: Select all

RunProgram("/usr/bin/pmset", "sleepnow", "")
Easy.

:mrgreen:
And what's the Code behind the RunProgram function? Yes, it's such ObjC code -
a wrapper on top of the real OS method. You can do this with every programming language.

Re: Mac OS X - Send the OS to sleep

Posted: Sun Dec 06, 2015 5:44 pm
by fsw
Danilo, I'm truly sorry if I hurt your feelings.
It was not my intent to put ObjectiveC down.
ObjectiveC, I'm sorry.
Will not do it again.
As sign of regret I will modify my initial post.

Re: Mac OS X - Send the OS to sleep

Posted: Sun Dec 06, 2015 5:46 pm
by fsw
Modified my first post.
Hope it's better now.