Mac OS X - Send the OS to sleep

Mac OSX specific forum
User avatar
fsw
Addict
Addict
Posts: 1572
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Mac OS X - Send the OS to sleep

Post by fsw »

Code: Select all

RunProgram("/usr/bin/pmset", "sleepnow", "")
Last edited by fsw on Sun Dec 06, 2015 5:45 pm, edited 1 time in total.

I am to provide the public with beneficial shocks.
Alfred Hitshock
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Mac OS X - Send the OS to sleep

Post by IdeasVacuum »

... I though that was the default for Apple products :mrgreen:
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: Mac OS X - Send the OS to sleep

Post by davido »

@fsw,
Nice demo. Thank you. :D
DE AA EB
Wolfram
Enthusiast
Enthusiast
Posts: 568
Joined: Thu May 30, 2013 4:39 pm

Re: Mac OS X - Send the OS to sleep

Post by Wolfram »

Thanks for this example.

Can you give us also an example to wakeup the computer. ;-)
macOS Catalina 10.15.7
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Mac OS X - Send the OS to sleep

Post 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
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
Danilo
Addict
Addict
Posts: 3037
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Mac OS X - Send the OS to sleep

Post 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.
User avatar
fsw
Addict
Addict
Posts: 1572
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Re: Mac OS X - Send the OS to sleep

Post 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.

I am to provide the public with beneficial shocks.
Alfred Hitshock
User avatar
fsw
Addict
Addict
Posts: 1572
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Re: Mac OS X - Send the OS to sleep

Post by fsw »

Modified my first post.
Hope it's better now.

I am to provide the public with beneficial shocks.
Alfred Hitshock
Post Reply