osx – 是否可以以编程方式打开关闭OS X“不要打扰”

osx – 是否可以以编程方式打开关闭OS X“不要打扰”,第1张

概述可以通过编程方式打开/关闭Mac OS X的“不要打扰”,这意味着通过代码.我已经通过google做了一些研究,比如: >通过Automator脚本applescripting notification center scheduling do not disturb. 顺便说一下,我没有使它工作,当我杀死通知中心,不要打扰开关仍然关闭 >通过代码编写默认值programmatic equival 可以通过编程方式打开/关闭Mac OS X的“不要打扰”,这意味着通过代码.我已经通过Google做了一些研究,比如:

>通过automator脚本applescripting notification center scheduling do not disturb.
顺便说一下,我没有使它工作,当我杀死通知中心,不要打扰开关仍然关闭
>通过代码编写默认值programmatic equivalent of defaults write command e.g. how to use NSUserDefaults,但是如何使用args -currentHost(在上面链接的文章中提到)

解决方法 不幸的是,没有公开的API来处理用户的通知偏好设置,其中之一是免打扰模式(DND).

然而,如果您想要提供在应用程序中打开和关闭DND的功能,那么您实际上并不幸运:您可以选择三种方式.

#1.运行这个微小的AppleScript并查看结果

这是一个AppleScript by philastokes,它考虑到这个选项 – 点击在menubar中的通知中心图标完全正是我们想要的:它切换了DND模式!

(* copyright © philastokes from applehelpwriter.com *)(* link: http://applehelpwriter.com/2014/12/10/applescript-toggle-notification-centre-yosemite *)tell application "System Events"    tell application process "systemUIServer"        try            (* Replace "Notification Center" with "NotificationCenter"            here if you're targeting OS X 10.10 *)            if exists menu bar item "Notification Center,Do Not disturb enabled" of menu bar 2 then                key down option                (* Replace "Notification Center" with "NotificationCenter"                here if you're targeting OS X 10.10 *)                click menu bar item "Notification Center,Do Not disturb enabled" of menu bar 2                key up option            else                key down option                click menu bar item "Notification Center" of menu bar 2                key up option            end if        on error            key up option        end try    end tellend tell

Please note that you need to replace "Notification Center" with "NotificationCenter" everywhere if you’re targeting OS X 10.10

Also,executing this code requires your application to have Accessibility enabled for it.

最后一步是将其包含到Objctive-C / Swift代码中:

Nsstring *source  = ... // the AppleScript codeNSAppleScript *script = [[NSAppleScript alloc] initWithSource: source];NSDictionary *errorInfo = nil;[script executeAndReturnError: &errorInfo];

#2.直接使用Accessibility API

我们可以使用系统中提供的Accessibility API,而不是让AppleScript引擎处理用户交互:

Executing this code requires your application to have Accessibility enabled for it.

pID_t systemUIServerPID = [[NSRunningApplication runningApplicationsWithBundleIDentifIEr:                              @"com.apple.systemUIserver"].firstObject processIDentifIEr];assert(systemUIServerPID != 0);AXUIElementRef target = AXUIElementCreateApplication(systemUIServerPID);assert(target != nil);CFArrayRef attributes = nil;AXUIElementcopyAttributenames(target,&attributes);assert([(__brIDge NSArray *)attributes containsObject: @"AXExtrasMenubar"]);CFTypeRef menubar;AXUIElementcopyAttributeValue(target,CFSTR("AXExtrasMenubar"),&menubar);CFTypeRef children;AXUIElementcopyAttributeValue(menubar,CFSTR("AXChildren"),&children);// XXX: I hate mixing CF and Objective-C like this but it's just a PoC code.// Anyway,I'm sorryNSArray *items = (__brIDge NSArray *)children;for (ID x in items) {    AXUIElementRef child = (__brIDge AXUIElementRef)x;    CFTypeRef Title;    AXUIElementcopyAttributeValue(child,CFSTR("AXTitle"),&Title);    assert(CfgetTypeID(Title) == CFStringGetTypeID());    // XXX: the proper check would be to match the whole "Notification Center" string,// but on OS X 10.10 it's "NotificationCenter" (without the space in-between) and    // I don't feel like having two conditionals here    if (CFStringHasPrefix(Title,CFSTR("Notification"))) {        optionKeyDown();        AXUIElementPerformAction(child,kAXPressAction);        optionKeyUp();        break;    }}

其中optionKeyDown()和optionKeyUp()是

#define kOptionKeyCode (58)static voID optionKeyDown(voID){    CGEventRef e = CGEventCreateKeyboardEvent(NulL,kOptionKeyCode,true);    CGEventPost(kCGSessionEventTap,e);    CFRelease(e);}static voID optionKeyUp(voID){    CGEventRef e = CGEventCreateKeyboardEvent(NulL,false);    CGEventPost(kCGSessionEventTap,e);    CFRelease(e);}

#3.我们假装我们是Notifications.prefPane

您可能会注意到,您可以通过通知首选项窗格启用DND模式,方法是将模式的范围从00:00设置为23:59.禁用DND只会取消选中该复选框.

这里是什么内容Notifications.prefPane:

voID turnDoNotdisturbOn(voID){    // The trick is to set DND time range from 00:00 (0 minutes) to 23:59 (1439 minutes),// so it will always be on    CFPreferencesSetValue(CFSTR("dndStart"),(__brIDge CFPropertyListRef)(@(0.0f)),CFSTR("com.apple.notificationcenterui"),kcfPreferencesCurrentUser,kcfPreferencesCurrentHost);    CFPreferencesSetValue(CFSTR("dndEnd"),(__brIDge CFPropertyListRef)(@(1440.f)),kcfPreferencesCurrentHost);    CFPreferencesSetValue(CFSTR("doNotdisturb"),(__brIDge CFPropertyListRef)(@(YES)),kcfPreferencesCurrentHost);    // Notify all the related daemons that we have changed Do Not disturb preferences    commitDoNotdisturbChanges();}voID turnDoNotdisturbOff(){    CFPreferencesSetValue(CFSTR("dndStart"),NulL,(__brIDge CFPropertyListRef)(@(NO)),kcfPreferencesCurrentHost);    commitDoNotdisturbChanges();}voID commitDoNotdisturbChanges(voID){    /// XXX: I'm using kcfPreferencesCurrentUser placeholder here which means that this code must    /// be run under regular user's account (not root/admin). If you're going to run this code    /// from a privileged helper,use kcfPreferencesAnyUser in order to toggle DND for all users    /// or drop privileges and use kcfPreferencesCurrentUser.    CFPreferencesSynchronize(CFSTR("com.apple.notificationcenterui"),kcfPreferencesCurrentHost);    [[NSdistributedNotificationCenter defaultCenter] postNotificationname: @"com.apple.notificationcenterui.dndprefs_changed"                                                               object: nil userInfo: nil                                                   deliverImmediately: YES];}
总结

以上是内存溢出为你收集整理的osx – 是否可以以编程方式打开/关闭OS X“不要打扰”全部内容,希望文章能够帮你解决osx – 是否可以以编程方式打开/关闭OS X“不要打扰”所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1030306.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存