首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Mac/Python中查看屏幕是否关闭?

如何在Mac/Python中查看屏幕是否关闭?
EN

Stack Overflow用户
提问于 2013-01-10 15:12:24
回答 2查看 1.9K关注 0票数 6

如何检查屏幕是否由于Mac/Python下系统偏好设置中的节能器设置而关闭?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-01-17 10:55:48

快速而肮脏的解决方案:调用ioreg并解析输出。

代码语言:javascript
复制
import subprocess
import re

POWER_MGMT_RE = re.compile(r'IOPowerManagement.*{(.*)}')

def display_status():
    output = subprocess.check_output(
        'ioreg -w 0 -c IODisplayWrangler -r IODisplayWrangler'.split())
    status = POWER_MGMT_RE.search(output).group(1)
    return dict((k[1:-1], v) for (k, v) in (x.split('=') for x in
                                            status.split(',')))

在我的电脑中,当屏幕打开时,CurrentPowerState的值为4,当屏幕关闭时,值为1

Better solution:使用ctypes直接从IOKit获取信息。

票数 5
EN

Stack Overflow用户

发布于 2013-01-14 09:39:17

我唯一能想到的方法就是使用

描述

pmset更改和读取电源管理设置,例如空闲睡眠定时、管理访问时唤醒、掉电时自动重新启动等。

请参考下面的链接,它将提供大量信息,帮助您准确地完成您正在寻找的内容。

http://managingamac.blogspot.com/2012/12/power-assertions-in-python.html

我将包含链接提供的代码,用于“保存和记录”目的:

代码语言:javascript
复制
#!/usr/bin/python

import ctypes
import CoreFoundation
import objc
import subprocess
import time

def SetUpIOFramework():
  # load the IOKit library
  framework = ctypes.cdll.LoadLibrary(
      '/System/Library/Frameworks/IOKit.framework/IOKit')

  # declare parameters as described in IOPMLib.h
  framework.IOPMAssertionCreateWithName.argtypes = [
      ctypes.c_void_p,  # CFStringRef
      ctypes.c_uint32,  # IOPMAssertionLevel
      ctypes.c_void_p,  # CFStringRef
      ctypes.POINTER(ctypes.c_uint32)]  # IOPMAssertionID
  framework.IOPMAssertionRelease.argtypes = [
      ctypes.c_uint32]  # IOPMAssertionID
  return framework

def StringToCFString(string):
  # we'll need to convert our strings before use
  return objc.pyobjc_id(
      CoreFoundation.CFStringCreateWithCString(
          None, string,
          CoreFoundation.kCFStringEncodingASCII).nsstring())

def AssertionCreateWithName(framework, a_type,
                            a_level, a_reason):
  # this method will create an assertion using the IOKit library
  # several parameters
  a_id = ctypes.c_uint32(0)
  a_type = StringToCFString(a_type)
  a_reason = StringToCFString(a_reason)
  a_error = framework.IOPMAssertionCreateWithName(
      a_type, a_level, a_reason, ctypes.byref(a_id))

  # we get back a 0 or stderr, along with a unique c_uint
  # representing the assertion ID so we can release it later
  return a_error, a_id

def AssertionRelease(framework, assertion_id):
  # releasing the assertion is easy, and also returns a 0 on
  # success, or stderr otherwise
  return framework.IOPMAssertionRelease(assertion_id)

def main():
  # let's create a no idle assertion for 30 seconds
  no_idle = 'NoIdleSleepAssertion'
  reason = 'Test of Pythonic power assertions'

  # first, we'll need the IOKit framework
  framework = SetUpIOFramework()

  # next, create the assertion and save the ID!
  ret, a_id = AssertionCreateWithName(framework, no_idle, 255, reason)
  print '\n\nCreating power assertion: status %s, id %s\n\n' % (ret, a_id)

  # subprocess a call to pmset to verify the assertion worked
  subprocess.call(['pmset', '-g', 'assertions'])
  time.sleep(5)

  # finally, release the assertion of the ID we saved earlier
  AssertionRelease(framework, a_id)
  print '\n\nReleasing power assertion: id %s\n\n' % a_id

  # verify the assertion has been removed
  subprocess.call(['pmset', '-g', 'assertions'])

if __name__ == '__main__':
  main()

http://opensource.apple.com/source/PowerManagement/PowerManagement-211/pmset/pmset.c

代码依赖于IOPMLib,它可以执行断言、调度电源事件、测量温度等功能。

https://developer.apple.com/library/mac/#documentation/IOKit/Reference/IOPMLib_header_reference/

要通过Python调用这些函数,我们必须通过IOKit框架。

http://developer.apple.com/library/mac/#documentation/devicedrivers/conceptual/IOKitFundamentals/

为了在Python中操作C数据类型,我们将使用一个名为ctype的外部函数接口。

http://python.net/crew/theller/ctypes/

这是作者在页面上描述的包装器;由Michael Lynn编写。我在上面的作者链接中发布的代码是对此代码的重写,以使其更易于理解。

https://github.com/pudquick/pypmset/blob/master/pypmset.py

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14252588

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档