Programmatically enabling Low Power Mode
I am trying to programmatically enable/disable low power mode using objc_util. I found an example on Stack Exchange (https://apple.stackexchange.com/a/244040/294117) which uses a private objective c api to do this. I am unfamiliar with objective c, so I am having trouble translating the part of the code block I need into Pythonista. Do I need to import a header file? I tried importing the class using the following (this was as far as I could get):
from objc_util import * cdbatterysaver = ObjCClass('_CDBatterySaver')
I don’t know which method or property I need to use or change. Is there a way to generate a list of this in Pythonista?
How would the Stack Exchange code translate to Pythonista? Thanks in advance!
@az not tested, try it, no low power mode on an ipad
from objc_util import * cdbatterysaver = ObjCClass('_CDBatterySaver') print(dir(cdbatterysaver)) print(dir(cdbatterysaver.batterySaver())) print(cdbatterysaver.batterySaver().getPowerMode()) cdbatterysaver.batterySaver().setMode_(0) # or 1
@cvp I didn’t know about the dir command- that’s really cool!
Unfortunately, while I can use this api to retrieve the current power mode, it appears that I cannot successfully set the power mode. Setting the power mode always has no effect and returns -1 for me. There are several other power mode related methods, but all of them appear to require a second argument or third arguments I wasn’t able to provide (setPowerMode_error, and setPowerMode_fromSource_ returned False with ‘’ for a second argument. I could not get setPowerMode_fromSource_withCompletion_ or setPowerMode_withCompletion to execute (How do I provide a “block” type argument?)).
I tried running cdbatterysaver.batterySaver().init(), but that crashed Pythonista.
I’m wondering if this api requires a higher-privileged process. (Would running it as a Notification Center widget work better?)
Does anyone else have any other suggestions?
from objc_util import * cdbatterysaver = ObjCClass('_CDBatterySaver') print(dir(cdbatterysaver)) print(dir(cdbatterysaver.batterySaver())) print(cdbatterysaver.batterySaver().getPowerMode()) err_ptr = c_void_p() cdbatterysaver.batterySaver().setPowerMode_error_(1,byref(err_ptr)) # or 1 if err_ptr: err=ObjCInstance(err_ptr) print(err) else: print('no error')
ObjCBlocklets you define blocks that can be used as completions. see for example https://forum.omz-software.com/topic/2152/help-with-blocks-again/4
Often, you can use None as the completion, in which case just nothing gets called.
It is entirely possible that this api has been locked down since that stack overflow post was written.
@JonB It's when I read such posts that I recognize that I really don't know anything about Objective-C 😢