CGSSessionScreenIsLocked

CGSSessionScreenIsLocked

本文介绍了OSX:检查屏幕被锁定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有什么办法来检查,如果屏幕被锁定在外壳或AppleScript的?
不只是检查,如果屏幕保护程序运行,但屏幕由节能设置或pressing ^⇧⏏(移动 + 控制锁定+ 退出)。

Is there any way to check if screen is locked in shell or applescript?Not just check if screensaver is running, but screen is locked by energy saver settings or by pressing (++).

感谢您提前。

推荐答案

首先,有在你的问题有点困惑。两个Shift +控制+弹出和节能放在屏幕睡觉,这是不一样的东西将它们锁定。根据您的其他设置,这可能还需要锁定屏幕,但是这是一个单独的问题。 IIRC,在Lion上,默认情况下,没有人会锁定屏幕,但如果你离开屏幕睡了比安全性与设定的时间;隐私权,将其锁定。

First, there's a bit of confusion in your question. Both Shift+Control+Eject and Energy Saver put the screens to sleep, which isn't the same thing as locking them. Depending on your other settings, this may also entail locking the screen, but that's a separate issue. IIRC, on Lion, by default, neither one will ever lock the screen—but if you leave the screen asleep for longer than the time set in Security & Privacy, that will lock it.

反正API <$c$c>CGSessionCopyCurrentDictionary让您可以获得两个屏幕的睡眠和屏幕锁的信息,为您的GUI会话。如果你没有一个GUI会话(例如,因为您是在一个SSH shell中运行),或会话没有自己的控制台(例如,因为有人使用快速用户切换你),你赢了 T为能够得到这个信息,但是你至少能够检测到这些情况。

Anyway, the API CGSessionCopyCurrentDictionary allows you to get information about both screen sleep and screen lock, for your GUI session. If you don't have a GUI session (e.g., because you're running in an ssh shell), or your session doesn't own the console (e.g., because someone has fast-user-switched you out), you won't be able to get this information, but you will at least be able to detect those cases.

这是我所知道的,唯一的机制,适用于所有操作系统的10.5(实际上10.3)至10.8(但是,这并不意味着它的存在只有一个居然是......)。

This is the only mechanism I know of that works for all OS's from 10.5 (actually 10.3) to 10.8 (but that doesn't mean it's the only one there actually is…).

有没有直接的方法,从bash或AppleScript的调用它。但是,您可以使用自己喜欢的桥(PyObjC,MacRuby的,ASOC等)间接调用它。下面是使用Python的例子:

There's no direct way to call this from bash or AppleScript. However, you can use your favorite bridge (PyObjC, MacRuby, ASOC, etc.) to call it indirectly. Here's an example using Python:

#!/usr/bin/python
import Quartz
d = Quartz.CGSessionCopyCurrentDictionary()
print d

下面是如何跨preT响应:

Here's how to interpret the response:


  • 如果你什么也得不到回来,那么你没有一个UI会话。

  • 如果词典中 kCGSSessionOnConsoleKey = 0,或没有present,无论是你的GUI会话没有自己的控制台或控制台的屏幕是睡着了。

  • 如果词典中 CGSSessionScreenIsLocked = 1,屏幕被锁定。

  • If you get nothing back, then you don't have a UI session.
  • If the dictionary has kCGSSessionOnConsoleKey = 0, or not present, either your GUI session doesn't own the console, or the console's screens are asleep.
  • If the dictionary has CGSSessionScreenIsLocked = 1, the screens are locked.

在一个问题的情况是 kCGSSessionOnConsoleKey 0(或失踪)和 CGSSessionScreenIsLocked 1。在这种情况下, ,要么你已经把睡觉的画面,并锁定他们,或其他人采取了控制台和锁定屏幕(有或没有把他们睡觉)。我不知道是否有这些情况来区分的一种方式。但是,如果你要寻找的不要试图显示一个对话框,因为用户必须先解锁屏幕,无论这些案件中的意思是不显示一个对话框。

The one problem case is where kCGSSessionOnConsoleKey is 0 (or missing) and CGSSessionScreenIsLocked is 1. In that case, either you've put the screens to sleep and locked them, or someone else has taken the console and locked the screens (with or without putting them to sleep). And I'm not sure if there's a way to distinguish between these cases. But if you're looking for "don't try to display a dialog because the user will have to unlock the screen first", both of those cases mean "don't display a dialog".

所以,这应该给你你想要什么:

So, this should give you what you want:

#!/usr/bin/python
import sys
import Quartz
d=Quartz.CGSessionCopyCurrentDictionary()
sys.exit(d and
         d.get("CGSSessionScreenIsLocked", 0) == 0 and
         d.get("kCGSSessionOnConsoleKey", 0) == 1)

或者,把它变成一个班轮你可以直接在shell脚本放:

Or, turning it into a one-liner you can put directly in a shell script:

python -c 'import sys,Quartz; d=Quartz.CGSessionCopyCurrentDictionary(); sys.exit(d and d.get("CGSSessionScreenIsLocked", 0) == 0 and d.get("kCGSSessionOnConsoleKey", 0) == 1)'

现在,如果你以SSH方式登录到什么一台Mac,而你的的当前登录到该Mac的GUI控制台(相同的用户)?在这种情况下,你的ssh登录会话可以以完全相同的方式,一个本地终端登录会话将与控制台登录会话交流。因此,CGSessionCopyCurrentDictionary是会得到相同的值。

Now, what if you've ssh'd into a Mac, and you're also currently logged into that Mac's GUI console (as the same user)? In that case, your ssh login session can communicate with the console login session in exactly the same way that a local Terminal login session would. So, CGSessionCopyCurrentDictionary is going to get the same values.

这是介导的连接将应用一些限制(例如,安全授权-u富应该从终端工作,但不超过SSH),但那些AREN'的引导服务器ŧ完全记录,并从版本改为版本,所以这可能不是你想要依靠的东西。相反,要真正地阅读您的登录会话信息

The bootstrap server that mediates that connection will apply some restrictions (e.g., security authorize -u foo should work from the Terminal but not over ssh), but those aren't fully documented, and change from version to version, so that's probably not something you want to rely on. Instead, you want to actually read your login session information

如果您希望与此走得更远,开始读Multiple用户环境编程主题。但有些信息是不是真的记录任何地方(例如,马赫级会议通过 SessionGetInfo 如何引用和BSD级别会议通过引用utmpx下捆绑在一起)。许多相关的工具和库,这可能会有帮助。即使在所有的,阅读起来不会告诉你怎么做你想做的,它会告诉你,你想要什么,和正确的术语使用搜索和提问,这可能是不够好。

If you want to go further with this, start with reading Multiple User Environments Programming Topics. But some of the information isn't really documented anywhere (e.g., how the Mach-level sessions referenced by SessionGetInfo and the BSD-level sessions referenced by utmpx are tied together). Many of the relevant tools and libraries are open source, which may help. Even if reading up on all of that doesn't tell you how to do what you want, it will tell you exactly what you want, and the right terms to use to search and ask questions, which may be good enough.

这篇关于OSX:检查屏幕被锁定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 20:40