Home > Java, Mac administration, Mac OS X, Scripting > Updated script to activate the Java web plug-ins setting in 10.7

Updated script to activate the Java web plug-ins setting in 10.7

A few months back, I posted a way to activate the Java web plug-ins from the command-line and also posted an accompanying script using PlistBuddy. At the time, I was only targeting the default user template as I was mostly doing test installs of 10.7 where I was creating new users. However, recently I’ve begun testing upgrades on machines with existing users and those users weren’t getting the new settings.

To fix this, I went digging in the latest build of DeployStudio. DeployStudio had recently included the ability to suppress the iCloud and gesture demos in ds_finalize, so I took a look to see if I could adapt what the DeployStudio developers had done for my own purposes. Sure enough, the answer to my problem was included as a for loop inside DeployStudio’s ds_finalize.sh script. (For those interested and who have DeployStudio handy, this script is in stored in /Applications/Utilities/DeployStudio Admin.app/Contents/Frameworks/DSCore.framework/Versions/A/Resources/Tools/Common/ds_finalize.sh.) The new script is also now using defaults instead of PlistBuddy (hat tip to Chris Hotte for posting how to use defaults to set the correct plist values.)

See below the jump for what the new and improved Java-enabling script looks like.


#!/bin/sh

# Set the the "Enable applet plug-in and Web Start Applications" setting for Java in your Mac's default user template and for all existing users.
# Code adapted from DeployStudio's rc130 ds_finalize script, from the section where DeployStudio is disabling the iCloud and gestures demos

# Get the system's UUID to set ByHost prefs
MAC_UUID=$(system_profiler SPHardwareDataType | awk -F" " '/UUID/{print $3}')

# Checks first to see if the Mac is running 10.7. If so, the script
# checks the system default user template for the presence of
# the Library/Preferences and Library/Preferences/ByHost directories.
# If the directories are not found, they are created and then the
# "Enable applet plug-in and Web Start Applications" setting for Java
# setting is enabled.

if [ `sw_vers -productVersion | awk -F. '{ print $2 }'` -ge 7 ]
then
for USER_TEMPLATE in "/System/Library/User Template"/*
do
if [ ! -d "${USER_TEMPLATE}"/Library/Preferences ]
then
mkdir -p "${USER_TEMPLATE}"/Library/Preferences
fi
if [ ! -d "${USER_TEMPLATE}"/Library/Preferences/ByHost ]
then
mkdir -p "${USER_TEMPLATE}"/Library/Preferences/ByHost
fi
if [ -d "${USER_TEMPLATE}"/Library/Preferences/ByHost ]
then
defaults write "${USER_TEMPLATE}"/Library/Preferences/ByHost/com.apple.java.JavaPreferences.${MAC_UUID} '{ GeneralByTask = { Any = { PrefsVersion = 2; WebComponentsEnabled = true;};};}'
fi
done
fi

# Checks first to see if the Mac is running 10.7. If so, the script
# checks the existing user folders in /Users for the presence of
# the Library/Preferences and Library/Preferences/ByHost directories.
# If the directories are not found, they are created and then the
# "Enable applet plug-in and Web Start Applications" setting for Java
# setting is enabled.

if [ `sw_vers -productVersion | awk -F. '{ print $2 }'` -ge 7 ]
then
for USER_HOME in /Users/*
do
USER_UID=`basename "${USER_HOME}"`
if [ ! "${USER_UID}" = "Shared" ]
then
if [ ! -d "${USER_HOME}"/Library/Preferences ]
then
mkdir -p "${USER_HOME}"/Library/Preferences
chown "${USER_UID}" "${USER_HOME}"/Library
chown "${USER_UID}" "${USER_HOME}"/Library/Preferences
fi
if [ ! -d "${USER_HOME}"/Library/Preferences/ByHost ]
then
mkdir -p "${USER_HOME}"/Library/Preferences/ByHost
chown "${USER_UID}" "${USER_HOME}"/Library
chown "${USER_UID}" "${USER_HOME}"/Library/Preferences
chown "${USER_UID}" "${USER_HOME}"/Library/Preferences/ByHost
fi
if [ -d "${USER_HOME}"/Library/Preferences/ByHost ]
then
defaults write "${USER_HOME}"/Library/Preferences/ByHost/com.apple.java.JavaPreferences.${MAC_UUID} '{ GeneralByTask = { Any = { PrefsVersion = 2; WebComponentsEnabled = true;};};}'
chown "${USER_UID}" "${USER_HOME}"/Library/Preferences/ByHost/com.apple.java.JavaPreferences.${MAC_UUID}.*
fi
fi
done
fi

UPDATE – 2-18-2012:

As of the 10.8 DP 1 build, it appears that the defaults command referenced above does not work in 10.8. I’ve updated my code to use PlistBuddy to correctly set these values. The script now also checks to see if you’re running 10.7 or 10.8 before running.

I’ve posted the updated script to my GitHub repo here:

https://github.com/rtrouton/rtrouton_scripts/tree/master/rtrouton_scripts/enable_java_web_plugins

Since there’s no problem with running it in 10.7, my 10.7 first boot script is still using defaults to set these values. That script is posted here on my GitHub repo:

https://github.com/rtrouton/rtrouton_scripts/tree/master/rtrouton_scripts/first_boot/10.7

  1. November 18, 2011 at 7:16 am

    Nice one. Is it necessary to set the values per user, or can you set them with defaults write -currentHost? We have a lot of portable home directories that won’t get the prefs on first login, as they already have a network home.

    • November 18, 2011 at 11:34 am

      Per,

      This is a per-user setting which lives in ~/Library/Preferences/ByHost, and I haven’t found a way to set it globally (hopefully you’ll have better luck.)

      If you wanted to set it automatically when someone logged in, you could set up a login hook or use a systems management tool to run something like this at login:


      # Enables the "Enable applet plug-in and Web Start Applications" setting for Java.

      /usr/bin/defaults write "$loggedInUserHome"/Library/Preferences/ByHost/com.apple.java.JavaPreferences.${MAC_UUID} '{ GeneralByTask = { Any = { PrefsVersion = 2; WebComponentsEnabled = true;};};}'

      #Correcting Permissions for /Users/loggedInUser/Library/Preferences/ByHost/com.apple.java.JavaPreferences.UUID.plist and .plist.lockfile

      /usr/sbin/chown "$loggedInUser" "$loggedInUserHome"/Library/Preferences/ByHost/com.apple.java.JavaPreferences.${MAC_UUID}.*

      You could also set up a LaunchAgent and script on the machines that ran something like this:


      #!/bin/sh

      # Get the system's UUID to set ByHost prefs

      MAC_UUID=$(system_profiler SPHardwareDataType | awk -F" " '/UUID/{print $3}')

      # Enables the "Enable applet plug-in and Web Start Applications" setting for Java.

      /usr/bin/defaults write /Users/$USER/Library/Preferences/ByHost/com.apple.java.JavaPreferences.${MAC_UUID} '{ GeneralByTask = { Any = { PrefsVersion = 2; WebComponentsEnabled = true;};};}'

      The LaunchAgent would be running the script with the logged-in user’s permissions, so you shouldn’t need to fix the permissions on the .plist and .plist.lockfile.

  2. November 18, 2011 at 11:57 am

    Yeah, I’ll pick it up and play with it. Thanks!

  1. No trackbacks yet.

Leave a comment