Activate Java web plug-ins setting in 10.7 from the command-line
As part of getting ready to support 10.7 in my shop, I ran into a problem with Java plug-ins. The issue was that Java applications were not launching in Safari. Instead, I’d get a message saying “Inactive Plug-In”. Here’s how you can fix this in the GUI:
1. Go to /Applications/Utilities.
2. Double-click on the Java Preferences icon.
3. In the General tab, place a check in the box labeled Enable applet plug-in and Web Start Applications.
However, this setting is set on a per-user basis. I couldn’t check the box in my local admin account and expect it to apply to all future users of a particular Mac. However, there is a way to set this setting from the command-line. See below the jump for details.
You’ll need to use PlistBuddy to set this, and you’ll need to have the UUID of the machine, since this setting is stored in the user’s Library/Preferences/ByHost directory. The script below will correctly set this setting in your Mac’s default user template:
#!/bin/sh
# Get the system's UUID to set ByHost prefs
MAC_UUID=$(system_profiler SPHardwareDataType | awk -F" " '/UUID/{print $3}')
# Creates the ByHost directory in the system's user template
# if it doesn't already exist.
mkdir /System/Library/User\ Template/English.lproj/Library/Preferences/ByHost/
# Activates the "Enable applet plug-in and Web Start Applications" setting in Java Preferences
/usr/libexec/PlistBuddy -c "Add :GeneralByTask:Any:WebComponentsEnabled string" /System/Library/User\ Template/English.lproj/Library/Preferences/ByHost/com.apple.java.JavaPreferences.$MAC_UUID.plist
/usr/libexec/PlistBuddy -c "Set :GeneralByTask:Any:WebComponentsEnabled true" /System/Library/User\ Template/English.lproj/Library/Preferences/ByHost/com.apple.java.JavaPreferences.$MAC_UUID.plist
Hat tip to Justin Rummel and Peter Bukowinski for helping me figure out how to get the UUID and use PlistBuddy to set the correct setting in the Java preferences plist file.
Update – 9-1-2011: I managed to tighten up the script somewhat, so that the PlistBuddy command is now all one line. The updated script is below.
#!/bin/sh
# Get the system's UUID to set ByHost prefs
MAC_UUID=$(system_profiler SPHardwareDataType | awk -F" " '/UUID/{print $3}')
# Creates the ByHost directory in the system's user template
# if it doesn't already exist.
mkdir /System/Library/User\ Template/English.lproj/Library/Preferences/ByHost/
# Activates the "Enable applet plug-in and Web Start Applications" setting in Java Preferences
/usr/libexec/PlistBuddy -c "Add :GeneralByTask:Any:WebComponentsEnabled bool true" /System/Library/User\ Template/English.lproj/Library/Preferences/ByHost/com.apple.java.JavaPreferences.$MAC_UUID.plist
Glad to help!
Don’t you want a boolean (not a string)? I did the same process of deciphering where this setting was and came up with the following:
$PlistBuddy -c ‘Add GeneralByTask dict’ “${userPrefsDirectory}/com.apple.java.JavaPreferences.$UUID.plist”
$PlistBuddy -c ‘Add GeneralByTask:Any dict’ “${userPrefsDirectory}/com.apple.java.JavaPreferences.$UUID.plist”
$PlistBuddy -c ‘Add GeneralByTask:Any:WebComponentsEnabled bool true’ “${userPrefsDirectory}/com.apple.java.JavaPreferences.$UUID.plist”
The plists we create differ a little:
pfergus$ defaults read /Users/pfergus/Desktop/rtrouton.plist
{
GeneralByTask = {
Any = {
WebComponentsEnabled = true;
};
};
}
pfergus$ defaults read /Users/pfergus/Desktop/pfergus.plist
{
GeneralByTask = {
Any = {
WebComponentsEnabled = 1;
};
};
}
But if they both work–great!
– Patrick
Patrick,
Both methods do work, but I’ll test setting boolean instead of string in my own script. The first PlistBuddy command was just to get the key added, with the next one actually setting the value.
Just a note that by default there is no ByHosts folder in User Template prefs, so the command will log that it’s creating the file, but will actually fail to write anything. Adding a
mkdir /System/Library/User\ Template/English.lproj/Library/Preferences/ByHost
before the PlistBuddy command should do the trick though.
Robin,
True, thanks for the catch! In the first boot script I have this command in, the ByHost directory is created for another command so I hadn’t thought to include creating it for this standalone script. I’ll update the post to include that and I’ve updated the script in my GitHub repo to include creating the ByHost directory:
http://tinyurl.com/3pyh3kz
No idea why plist buddy is being used… you can pretty much just pump in defaults read output right into defaults write input. IE:
defaults write ~/Library/Preferences/ByHost/com.apple.java.JavaPreferences.${HWUUID} ‘{ GeneralByTask = { Any = { PrefsVersion = 2; WebComponentsEnabled = true;};};}’
I can also confirm the numeric 1 in place of where ‘true’ is stated above does not work. The word ‘true’, interpreted as a bool does.
Thanks, Chris. I’ve been reworking this script over the past couple of days and I’m trying out your defaults command as part of the overhaul.
After some plugging away, I’ve refined it a bit – this works for all current and future users.
#!/bin/sh
# set_java_prefs.sh
# DYNAMICALLY SET THE LEOPARD UUID FOR THE BYHOST FILE NAMING
if [[ `ioreg -rd1 -c IOPlatformExpertDevice | grep -i “UUID” | cut -c27-50` == “00000000-0000-1000-8000-” ]]; then
LEOUUID=`ioreg -rd1 -c IOPlatformExpertDevice | grep -i “UUID” | cut -c51-62 | awk {‘print tolower()’}`
elif [[ `ioreg -rd1 -c IOPlatformExpertDevice | grep -i “UUID” | cut -c27-50` != “00000000-0000-1000-8000-” ]]; then
LEOUUID=`ioreg -rd1 -c IOPlatformExpertDevice | grep -i “UUID” | cut -c27-62`
fi
mkdir /System/Library/User\ Template/English.lproj/Library/Preferences/ByHost/
touch /System/Library/User\ Template/English.lproj/Library/Preferences/ByHost/com.apple.java.JavaPreferences.${LEOUUID}.plist
defaults write /System/Library/User\ Template/English.lproj/Library/Preferences/ByHost/com.apple.java.JavaPreferences.${LEOUUID} ‘{ GeneralByTask = { Any = { PrefsVersion = 2; WebComponentsEnabled = true;};};}’
for user in /Users/*; do
user=`basename $user`
if [ “$user” != Shared ]
then
defaults write /Users/${user}/Library/Preferences/ByHost/com.apple.java.JavaPreferences.${LEOUUID} ‘{ GeneralByTask = { Any = { PrefsVersion = 2; WebComponentsEnabled = true;};};}’
fi
done
echo “Java Preference successfully set.”
exit 0
Please note – the quote marks around the Preference strings need to be SINGLE quotes – not quotes or backticks. I wasted an hour trying to figure this out.
#!/bin/sh
# set_java_prefs.sh
# For 10.6 and 10.7, deactivates java-web-plugins for all current and future users. Based on:
# https://derflounder.wordpress.com/2011/08/31/activate-java-web-plug-ins-setting-in-10-7-from-the-command-line/
# DYNAMICALLY SET THE LEOPARD UUID FOR THE BYHOST FILE NAMING
if [[ `ioreg -rd1 -c IOPlatformExpertDevice | grep -i “UUID” | cut -c27-50` == “00000000-0000-1000-8000-” ]]; then
LEOUUID=`ioreg -rd1 -c IOPlatformExpertDevice | grep -i “UUID” | cut -c51-62 | awk {‘print tolower()’}`
elif [[ `ioreg -rd1 -c IOPlatformExpertDevice | grep -i “UUID” | cut -c27-50` != “00000000-0000-1000-8000-” ]]; then
LEOUUID=`ioreg -rd1 -c IOPlatformExpertDevice | grep -i “UUID” | cut -c27-62`
fi
mkdir /System/Library/User\ Template/English.lproj/Library/Preferences/ByHost/
touch /System/Library/User\ Template/English.lproj/Library/Preferences/ByHost/com.apple.java.JavaPreferences.${LEOUUID}.plist
defaults write /System/Library/User\ Template/English.lproj/Library/Preferences/ByHost/com.apple.java.JavaPreferences.${LEOUUID} ‘{ GeneralByTask = { Any = { PrefsVersion = 2; WebComponentsEnabled = false;};};}’
for user in /Users/*; do
user=`basename $user`
if [ “$user” != Shared ]
then
defaults write /Users/${user}/Library/Preferences/ByHost/com.apple.java.JavaPreferences.${LEOUUID} ‘{ GeneralByTask = { Any = { PrefsVersion = 2; WebComponentsEnabled = false;};};}’
fi
done
echo “Java Preference successfully set.”
exit 0
Here’s a one-liner I came u with when I encountered this and wanted to change the setting using ARD:
/usr/libexec/PlistBuddy -c “Set :GeneralByTask:Any:WebComponentsEnabled true” ~/Library/Preferences/ByHost/com.apple.java.JavaPreferences.`ioreg -rd1 -c IOPlatformExpertDevice | awk ‘/UUID/ {print $3}’ | sed ‘s/”//g’`.plist
(http://pastie.org/3343390)
WARNING: When straight quotes are pasted into replies at this site, they are displayed as curly quotes which can cause shell scripts to fail. Easiest way to straighten quotes is to use TextWrangler http://www.barebones.com/products/textwrangler/ (this editor is also aware of shell syntax when it sees #!/bin/sh and can color-highlight arguments, etc.
10.7.4 is more persikity. It requires all fields to be properly formatted. Ints have to be ints. Bools have to be bools. Reals have to be reals…
PlistBuddy=$”/usr/libexec/PlistBuddy”
HWUUID=$(system_profiler SPHardwareDataType|awk ‘/UUID/ { print $3 }’)
rm ~/Library/Preferences/ByHost/com.apple.java.JavaPreferences.$UUID.plist
$PlistBuddy -c ‘Add GeneralByTask dict’ ~/Library/Preferences/ByHost/com.apple.java.JavaPreferences.$HWUUID.plist
$PlistBuddy -c ‘Add GeneralByTask:Any dict’ ~/Library/Preferences/ByHost/com.apple.java.JavaPreferences.$HWUUID.plist
$PlistBuddy -c ‘Add GeneralByTask:Any:WebComponentsEnabled bool true’ ~/Library/Preferences/ByHost/com.apple.java.JavaPreferences.$HWUUID.plist
$PlistBuddy -c ‘Add GeneralByTask:Any:PrefsVersion integer 2’ ~/Library/Preferences/ByHost/com.apple.java.JavaPreferences.$HWUUID.plist
$PlistBuddy -c ‘Add GeneralByTask:Any:WebComponentsLastUsed real 360172160.000000’ ~/Library/Preferences/ByHost/com.apple.java.JavaPreferences.$HWUUID.plist
A question. I never have seen the plist you mention above in my (~/Library/Preferences) folder: “com.apple.java.JavaPreferences.$HWUUID.plist”. It simply isn’t there. But it does need to be created in order for the setting to be ‘always enabled’?
With the current security policies in ML, does the setting work in that OS as well?
Thanks for the info!
Regards,
Mark
I found the preferneces file! there is a ‘ByHost’ subdirectory under (~/Library/Preferences)!
Still wondering if using PlistBuddy is the same as using the defaults command to write the preferences file.
There are also settings in the (~/Library/Preferences/com.apple.Safari.plist) for WebKitJavaEnabled. Does that plist file also need modification or just the ones under the ‘ByHost’ subdirectory?