终端无法识别更新的用户默认设置

Terminal not recognizing updated User Defaults

我正在尝试从 Mac OS 应用程序中更改屏幕截图的保存位置,但这似乎不起作用。

在终端中执行以下命令完美无缺。正如预期的那样,所有屏幕截图都保存在名为 Screenshots 的文件夹中(位于我的桌面上)。

航站楼

defaults write com.apple.screencapture location ~/Desktop/Screenshots
killall SystemUIServer

每当我让我的应用程序执行以下代码片段时,com.apple.screencapture.plist 中的位置值将按预期更改。

Objective-C

NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:@"com.apple.screencapture.plist"];

[defaults setValue:@"/Users/Maarten/Desktop/Screenshots" forKey:@"location"];
[defaults synchronize];

值改变后,我用终端运行killall SystemUIServer

但不知何故这个命令似乎不起作用,因为所有屏幕截图都保存在其先前定义的(默认)位置。

自 10.9(我认为)以来,OS 缓存了您的偏好设置。要使缓存失效,需要kill cfprefsd。因此,首先,通过 defaults 设置您的首选项,然后 运行 此命令:

killall -u $USER cfprefsd

在那之后,您的偏好应该不需要重新启动或其他任何东西。

此外,您可以使用 screencapture 指定特定屏幕截图的位置,而不是更改系统偏好设置。您也可以使用此工具指定其他参数。使用 man screencapture 获取更多信息。

最后,根据 Apple 的文档,synchronize 对于 NSUserDefaults 来说是多余的:

synchronize

Waits for any pending asynchronous updates to the defaults database and returns; this method is unnecessary and shouldn't be used.