如何将CFPreferencesSetAppValue与容器一起使用?

时间:2015-10-21 11:07:01

标签: cocoa pyobjc cfpreferences

OS X + Excel 2016

我们非英语使用者希望我们的计算机本地化,但我们想使用Excel英语公式。为此,我们需要让Excel说另一种语言,从而为应用程序设置AppleLanguages GlobalPreference以覆盖系统设置。

学习和讨论= SUM()比= SOMME()或= NPV()比= VAN()

更容易

为了让我的用户更容易,我正在编写一个Python + Cocoa应用程序,它基本上会询问用户他/她想要的语言并将其存储在首选项文件中。

在CLI中,要将Excel(仅)设置为英语,请运行:

 $ defaults write com.microsoft.Excel AppleLanguages "('en’)”

我的问题

我跑:

newLanguageCode = ['zh-CN']
CoreFoundation.CFPreferencesSetAppValue('AppleLanguages', newLanguageCode, 'com.microsoft.Excel')

问题是它写入〜/ Library / Preferences / com.microsoft.Excel.plist而不是〜/ Library / Containers / com.microsoft.Excel / Data / Library / Preferences / com.microsoft.Excel.plist

如何告诉CFPreferencesSetAppValue写入Container而不是home?

附录

minidefrancois:Library francois$ defaults read ~/Library/Preferences/com.microsoft.Excel.plist 
{
    AppleLanguages =     (
        "zh-CN"
    );
}


minidefrancois:Library francois$ defaults read -app /Applications/Microsoft\ Excel.app/
{
    AppExitGraceful = 0;
    AppleLanguages =     (
        en
    );
    ExceptionEnum = 0;
    NSRecentDocumentsLimit = 0;
    "NSWindow Frame FileUIWindowFrameAutoSaveName" = "276 322 1046 626 0 0 1680 1027 ";
    OCModelLanguage = "fr-FR";
    OCModelVersion = "0.838";
    OUIWhatsNewLastShownLink = 624954;
    SendAllTelemetryEnabled = 1;
    SessionBuildNumber = 151008;
    SessionId = "55FDAD65-D013-4184-A5A5-CBF747DC563D";
    SessionStartTime = "10/21/2015 10:49:43.997";
    SessionVersion = "15.15";
    TemplateDownload = 1;
    kFileUIDefaultTabID = 1;
    kOUIRibbonDefaultCollapse = 0;
    kSubUIAppCompletedFirstRunSetup1507 = 1;
}


minidefrancois:Library francois$ defaults read ~/Library/Containers/com.microsoft.Excel/Data/Library/Preferences/com.microsoft.Excel.plist
{
    AppExitGraceful = 0;
    AppleLanguages =     (
        en
    );
    ExceptionEnum = 0;
    NSRecentDocumentsLimit = 0;
    "NSWindow Frame FileUIWindowFrameAutoSaveName" = "276 322 1046 626 0 0 1680 1027 ";
    OCModelLanguage = "fr-FR";
    OCModelVersion = "0.838";
    OUIWhatsNewLastShownLink = 624954;
    SendAllTelemetryEnabled = 1;
    SessionBuildNumber = 151008;
    SessionId = "55FDAD65-D013-4184-A5A5-CBF747DC563D";
    SessionStartTime = "10/21/2015 10:49:43.997";
    SessionVersion = "15.15";
    TemplateDownload = 1;
    kFileUIDefaultTabID = 1;
    kOUIRibbonDefaultCollapse = 0;
    kSubUIAppCompletedFirstRunSetup1507 = 1;
}
minidefrancois:Library francois$ 

1 个答案:

答案 0 :(得分:0)

感谢Thomas Burgin和Greg Neagle:

需要将应用ID设置为:

NSHomeDirectory() + "/Library/Containers/com.microsoft.Excel/Data/Library/Preferences/com.microsoft.Excel"

我把它推到https://github.com/ftiff/Office8n

以下是相关代码:

homeDirectory = NSHomeDirectory()
appName = homeDirectory + "/Library/Containers/com.microsoft.Excel/Data/Library/Preferences/com.microsoft.Excel”
propertyName = ‘AppleLanguages’ (unused here, will later)
_dropDown = objc.IBOutlet()

   def getPreference(self, appName, propertyName, _dropDown):
       result = CoreFoundation.CFPreferencesCopyAppValue('AppleLanguages', appName)
       # if it's an array, return only first value
       if isinstance(result, NSArray):
           result = result[0]
       language = self.codeToLanguage.get(result, "English")
       _dropDown.selectItemWithTitle_(language)
       return result

_label = @objc.IBAction

   def savePreference(self, propertyName, _value, appName, _label):
       propertyValue = []
       propertyValue.append(_value)
       CoreFoundation.CFPreferencesSetAppValue(propertyName, propertyValue, appName)
       CoreFoundation.CFPreferencesAppSynchronize(appName)
       NSLog("{appName}: {propertyName} = {propertyValue}".format(
                                                                  appName=appName,
                                                                  propertyName=propertyName,
                                                                  propertyValue=propertyValue
                                                                  ))
       _label.setStringValue_("Set to: " + self.codeToLanguage.get(propertyValue[0], "None"))