简体   繁体   中英

How do I retrieve a Mac OS setting using Java?

Mac OS has a setting that allows users to define when scroll bars should be visible (automatically, when scrolling, always).

Is there a way to find out the current setting using Java?

For me, running on macOS 10.14.6, the setting (when not set to "automatic") can be found in the current user's global preferences ~/Library/Preferences/.GlobalPreferences.plist . This file has content such as:

{
    AKDeviceUnlockState = :false;
    AKLastIDMSEnvironment = 0;
    AppleActionOnDoubleClick = "Maximize";
    AppleAntiAliasingThreshold = 4;
    AppleInterfaceStyle = "Dark";
    AppleLanguages = ( "en-US" );
    AppleLanguagesDidMigrate = "10.14.6";
    AppleLocale = "en_US";
    AppleMiniaturizeOnDoubleClick = :false;
    AppleShowScrollBars = "Always";
    ...
}

I'm not 100% sure if this covers all possible scenarios, but you could start by parsing this file, finding the AppleShowScrollBars value and using that to drive your desired functionality.

There's a handy library called dd-plist that allows you to use the following code:

try {
    final File preferences = new File("~/Library/Preferences/.GlobalPreferences.plist");
    final NSDictionary root = (NSDictionary) PropertyListParser.parse(preferences);
    final String scrollbars = root.get("AppleShowScrollBars").toString();

    System.out.println(scrollbars);
} catch (Exception e) {
    // Handle errors
    System.out.println();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM