我想知道如何以编程方式获得该值?
我问,因为我正在绘制一个带有自定义标题栏的窗口,我希望它尽可能地与正常(非自定义)可可窗口相似(在行为上).
也许一个终端命令我可以管道或是否有一个cocoa API这样做?
编辑:
回答(感谢NSGod)
- (void)mouseUp:(NSEvent *)event{ if ([event clickCount] == 2) { //Get settings from "System Preferences" > "Appearance" > "Double-click on windows title bar to minimize" NSString *const MDAppleMiniaturizeOnDoubleClickKey = @"AppleMiniaturizeOnDoubleClick"; NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; // [userDefaults addSuiteNamed:NSGlobalDomain]; // unnecessary BOOL shouldMiniaturize = [[userDefaults objectForKey:MDAppleMiniaturizeOnDoubleClickKey] boolValue]; if (shouldMiniaturize) { [self miniaturize:self]; } } }
后来我发现可以找到Appearance(Aqua / Graphite):
NSString * const kAppleAquaColorVariant = @"AppleAquaColorVariant"; NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; // [userDefaults addSuiteNamed:NSGlobalDomain]; // unnecessary NSNumber *color = [userDefaults objectForKey:kAppleAquaColorVariant]; if ([color intValue] == 6) {//graphite is 6 imageName = [imageName stringByAppendingFormat:@"_graphite"]; }else{//defaults to aqua,(aqua is 1) imageName = [imageName stringByAppendingFormat:@"_colorsryg"]; }
哪个也有帮助:)
解决方法
NSString * const MDAppleMiniaturizeOnDoubleClickKey = @"AppleMiniaturizeOnDoubleClick"; NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; // [userDefaults addSuiteNamed:NSGlobalDomain]; // unnecessary NSNumber *miniaturize = [userDefaults objectForKey:MDAppleMiniaturizeOnDoubleClickKey]; NSLog(@"AppleMiniaturizeOnDoubleClick == %@",([miniaturize boolValue] ? @"YES" : @"NO"));
(此首选项设置存储在〜/ Library / Preferences /文件夹中的不可见.GlobalPreferences.plist中).
请注意,默认情况下,“双击以最小化”选项已关闭,因此,如果您检查AppleMiniaturizeOnDoubleClick是否存在并且它返回nil,则表示它已关闭. (如果用户默认值与默认值不同,则用户默认仅开始存储值).
这个关键在Leopard和Snow Leopard中是一样的. (它在Lion或Mountain Lion中也没有变化).
当然,在NSWindow中有一个秘密(私有)方法,– (BOOL)_shouldMiniaturizeOnDoubleClick,但我不建议使用私有方法.
[更新]关于Catfish_Man的评论:你的行[userDefaults addSuiteNamed:NSGlobalDomain]是正确的.是不必要的,因为NSUserDefaults已经能够读取全局首选项. (我修改了上面的代码来反映这一点).
“Additionally,
NSGlobalDomain
is not translated to
.GlobalPreferences.plist for that method.”
我不确定我会跟着你. NSUserDefaults建立在CFPreferences之上,它定义了以下6个常量:
应用:
kcfPreferencesAnyApplication,kcfPreferencesCurrentApplication
主办:
kcfPreferencesAnyHost,kcfPreferencesCurrentHost
用户:
kcfPreferencesAnyUser,kcfPreferencesCurrentUser
给定虚构的应用程序包标识符“com.markdouma.App”和单个主机(基于您当前的网络位置,此示例不会更改),通常有8个位置可以在磁盘上存储首选项信息. (注意:显示的路径仅用于演示目的;确切的文件路径位置可能会发生变化). 8个不同的位置来自CFP参考常量的不同组合:
/Library/Preferences/.GlobalPreferences.plist
(kcfPreferencesAnyApplication,kcfPreferencesAnyUser,kcfPreferencesAnyHost)
/Library/Preferences/com.markdouma.App.plist
(kcfPreferencesCurrentApplication,
kcfPreferencesAnyHost)/Library/Preferences/ByHost/.GlobalPreferences.UNIQUE_HOST_IDENTIFIER.plist
(kcfPreferencesAnyApplication,kcfPreferencesCurrentHost)
/Library/Preferences/ByHost/com.markdouma.App.UNIQUE_HOST_IDENTIFIER.plist
(kcfPreferencesCurrentApplication,
kcfPreferencesAnyUser,kcfPreferencesCurrentHost)~/Library/Preferences/.GlobalPreferences.plist
(kcfPreferencesAnyApplication,kcfPreferencesCurrentUser,
kcfPreferencesAnyHost)~/Library/Preferences/com.markdouma.App.plist
(kcfPreferencesCurrentApplication,
kcfPreferencesAnyHost)~/Library/Preferences/ByHost/.GlobalPreferences.UNIQUE_HOST_IDENTIFIER.plist
(kcfPreferencesAnyApplication,
kcfPreferencesCurrentUser,kcfPreferencesCurrentHost)~/Library/Preferences/ByHost/com.markdouma.App.UNIQUE_HOST_IDENTIFIER.plist
(kcfPreferencesCurrentApplication,kcfPreferencesCurrentHost)
虽然NSUserDefaults只能写入以斜体显示的域组合,但它自动具有以粗体显示的域组合的读访问权限.换句话说,无需执行任何操作,我可以自动运行以下代码并打印结果:
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; NSNumber *miniaturize = [userDefaults objectForKey:@"AppleMiniaturizeOnDoubleClick"]; NSNumber *fastUserSwitching = [userDefaults objectForKey:@"MultipleSessionEnabled"]; NSLog(@"AppleMiniaturizeOnDoubleClick == %@",([miniaturize boolValue] ? @"YES" : @"NO")); NSLog(@"MultipleSessionEnabled == %@",([fastUserSwitching boolValue] ? @"YES" : @"NO"));
在我的系统上运行该代码将打印以下结果:
AppleMiniaturizeOnDoubleClick == YES MultipleSessionEnabled == YES
这是有道理的,因为我同时具有快速用户切换和双击以最小化启用的选项. MultipleSessionsEnabled存储在/Library/Preferences/.GlobalPreferences.plist的本地域中,AppleMiniaturizeOnDoubleClick存储在〜/ Library / Preferences / .GlobalPreferences.plist的用户域中.
示例项目:NSUserDefaultsFinagler.zip
“Additionally additionally,that’s slow. Please don’t do this.”
抱歉,这没有任何意义(假设我们已经同意我们不再使用addSuiteNamed :).用户默认值由应用程序缓存,使得调用在几毫秒内完成.在为代表本地应用程序值的键或代表全局值的键的值询问用户默认值之间,不会有明显的区别吗?
AFAIK,这是实现OP目标的唯一“合法”(App-store兼容)方式.如果还有另一种更有效的方法,请详细说明.