Home > Bash scripting, Java, Mac administration, Mac OS X > Automatically activate the Java web plug-ins setting on login

Automatically activate the Java web plug-ins setting on login

One of the features of Apple’s Java for OS X Lion 2012-003 update is that this Java update for Mac OS X 10.7.x turns off the automatic execution of Java applets through web browsers.

The Java update does allow for the automatic execution of Java applets to be re-enabled through the browser. However, if it’s been a while since a Java applet was launched, then automatic execution of Java applets is once again automatically disabled.

This was going to be an issue at my workplace, as we have at least one application that relies on Java applets running through the browser. Fortunately, I already had a fix for this issue, I just needed to script it and run that script automatically with the user’s permissions.

Update (4-13-2012): It turned out that I also needed to add the GeneralByTask:Any:WebComponentsLastUsed key to the plist and set it. I’ve now updated the script below and my GitHub repo with the added key settings.

Update (4-18-2012): Joel Bruner in the comments pointed out that the real value associated with the GeneralByTask:Any:WebComponentsLastUsed key needs to be a dynamic date value rather than a static value that will expire. I’m incorporating his date calculating method into the script.

#!/bin/sh

# DYNAMICALLY SET THE UUID FOR THE BYHOST FILE NAMING
if [[ `ioreg -rd1 -c IOPlatformExpertDevice | grep -i "UUID" | cut -c27-50` == "00000000-0000-1000-8000-" ]]; then
MAC_UUID=`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
MAC_UUID=`ioreg -rd1 -c IOPlatformExpertDevice | grep -i "UUID" | cut -c27-62`
fi

# Set the the "Enable applet plug-in and Web Start Applications" setting in
# the Java Preferences for the current user.

/usr/libexec/PlistBuddy -c "Delete :GeneralByTask:Any:WebComponentsEnabled" /Users/$USER/Library/Preferences/ByHost/com.apple.java.JavaPreferences.${MAC_UUID}.plist
/usr/libexec/PlistBuddy -c "Add :GeneralByTask:Any:WebComponentsEnabled bool true" /Users/$USER/Library/Preferences/ByHost/com.apple.java.JavaPreferences.${MAC_UUID}.plist
/usr/libexec/PlistBuddy -c "Delete :GeneralByTask:Any:WebComponentsLastUsed" /Users/$USER/Library/Preferences/ByHost/com.apple.java.JavaPreferences.${MAC_UUID}.plist
/usr/libexec/PlistBuddy -c "Add :GeneralByTask:Any:WebComponentsLastUsed real $(( $(date "+%s") - 978307200 ))" /Users/$USER/Library/Preferences/ByHost/com.apple.java.JavaPreferences.${MAC_UUID}.plist


To make this work, I’ve written a script and launch agent combination. The script will enable the Enable applet plug-in and Web Start Applications setting in the Java Preferences application.The LaunchAgent runs the script on login to any user account with the logging-in user’s privileges and permissions.

You can find it here on my GitHub repo.

  1. Patrick Fergus
    April 13, 2012 at 1:47 am

    Thanks for the script!

    Is this for convenience and consistency sake, or is there something preventing regular users from enabling the inactive Java plugin in the browser or upon launch of a Java Web Start application?

    • April 13, 2012 at 2:00 am

      It’s to save calls to the help desk. There’s nothing that prevents the user from enabling the inactive plug-in, the process is just not intuitive.

  2. n8tron
    April 13, 2012 at 4:37 pm

    so the option to enable web start apps appears to be removed from the java prefs gui now, correct?

  3. n8tron
    April 13, 2012 at 4:39 pm

    so the option to enable web start apps from the java prefs gui seems to be gone now, right? enabling programmatically is the only way?

    • April 13, 2012 at 6:02 pm

      The checkbox is still there in the GUI. However, if Java applets aren’t used in a browser for a while, it unchecks itself.

  4. April 18, 2012 at 11:07 pm

    @N8tron: I thought I was going crazy when the check box wasn’t there – but in 10.6 it’s not there at all, only in 10.7. The 10.6 Update 8 for Snow Leopard does not do the same deactivation after ~30 days of inactivity that the 10.7 Update 2012-003 does

    @rtrouton: Nice UUID scripting, I didn’t know how to obtain that and had been relying on launching the Java Prefs to create the pref, or doing wild card searches for an existing one… For the WebComponentsLastUsed rather than using a static number which will eventually expire, you can give it the current time, in this case, the number of seconds since January 1, 2001 (this is Apple’s epoch apparently) so the single quotes are necessary in plistbuddy, just put this in it’s place: $(( $(date “+%s”) – 978307200 ))

    /usr/libexec/PlistBuddy -c “Add :GeneralByTask:Any:WebComponentsLastUsed real $(( $(date “+%s”) – 978307200 ))”

    Style-wise I’m a $() fan over “ ;)

    Thanks,
    Joel

    • April 19, 2012 at 12:03 am

      Thanks, Joel. I’d misinterpreted what the real value was for, but when I re-checked the values in my own com.apple.java.JavaPreferences.${MAC_UUID}.plist, it looks like dynamically updating using the current date is a good idea. I’ll update my script with your PlistBuddy command.

  5. Christian
    June 14, 2012 at 1:58 pm

    One of the features of Appleā€™s Java for OS X Snow Leopard update 9 is that this Java update for Mac OS X 10.6.x turns off the automatic execution of Java applets through web browsers, like in Mac OS X 10.7.x.
    Do you think that your fix will be ok for Mac OS X 10.6.x too ?

    Thanks in advance for answers

    Christian

    • June 14, 2012 at 5:41 pm

      Christian,

      I’ve tested my script this morning on a 10.6.8 Mac and it looks like Apple is using the same method to disable Java on 10.6.x as they used on 10.7.x. This script re-enabled Java on 10.6.8 without a problem and didn’t require any modifications to run on 10.6.x.

      • Christian
        June 18, 2012 at 10:03 am

        Many thanks for your work !!!

        Christian

  6. Lannie Schafroth
    September 28, 2012 at 4:46 pm

    I ran this as a script from my Casper remote and the script ran and created the files but the check box was NOT enabled. This was on a 10.7.4 client. Does not appear to work.

  7. Lannie Schafroth
    September 28, 2012 at 4:47 pm

    It appears the user variable is the cause when ran from my Casper Remote:
    Script result: Delete: Entry, “:GeneralByTask:Any:WebComponentsEnabled”, Does Not Exist
    File Doesn’t Exist, Will Create: /Users/root/Library/Preferences/ByHost/com.apple.java.JavaPreferences.146C7D46-C7D8-5E43-97B8-1A54B6701EA6.plist
    File Doesn’t Exist, Will Create: /Users/root/Library/Preferences/ByHost/com.apple.java.JavaPreferences.146C7D46-C7D8-5E43-97B8-1A54B6701EA6.plist
    Delete: Entry, “:GeneralByTask:Any:WebComponentsLastUsed”, Does Not Exist
    File Doesn’t Exist, Will Create: /Users/root/Library/Preferences/ByHost/com.apple.java.JavaPreferences.146C7D46-C7D8-5E43-97B8-1A54B6701EA6.plist
    File Doesn’t Exist, Will Create: /Users/root/Library/Preferences/ByHost/com.apple.java.JavaPreferences.146C7D46-C7D8-5E43-97B8-1A54B6701EA6.plist

    It runs it as root and not the real current user. I will test with another script that works and see what needs changed in the user variable.

    • September 28, 2012 at 4:53 pm

      Lannie,

      I recommend running this script in combination with a LaunchAgent. That way, it launches and runs as the currently-logged in user. For an example, see the script and launchdaemon available at the link to my GitHub repo.

  8. Lannie Schafroth
    September 28, 2012 at 4:54 pm

    Here is the script updated to work with Casper Remote:

    #!/bin/sh

    username=$3
    currentuser=`/usr/bin/defaults read /Library/Preferences/com.apple.loginwindow lastUserName`

    /bin/echo “currentuser is $currentuser”

    # DYNAMICALLY SET THE UUID FOR THE BYHOST FILE NAMING
    if [[ `ioreg -rd1 -c IOPlatformExpertDevice | grep -i "UUID" | cut -c27-50` == "00000000-0000-1000-8000-" ]]; then
    MAC_UUID=`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
    MAC_UUID=`ioreg -rd1 -c IOPlatformExpertDevice | grep -i “UUID” | cut -c27-62`
    fi

    # Set the the “Enable applet plug-in and Web Start Applications” setting in
    # the Java Preferences for the current user.

    /usr/libexec/PlistBuddy -c “Delete :GeneralByTask:Any:WebComponentsEnabled” /Users/$currentuser/Library/Preferences/ByHost/com.apple.java.JavaPreferences.${MAC_UUID}.plist
    /usr/libexec/PlistBuddy -c “Add :GeneralByTask:Any:WebComponentsEnabled bool true” /Users/$currentuser/Library/Preferences/ByHost/com.apple.java.JavaPreferences.${MAC_UUID}.plist
    /usr/libexec/PlistBuddy -c “Delete :GeneralByTask:Any:WebComponentsLastUsed” /Users/$currentuser/Library/Preferences/ByHost/com.apple.java.JavaPreferences.${MAC_UUID}.plist
    /usr/libexec/PlistBuddy -c “Add :GeneralByTask:Any:WebComponentsLastUsed real $(( $(date “+%s”) – 978307200 ))” /Users/$currentuser/Library/Preferences/ByHost/com.apple.java.JavaPreferences.${MAC_UUID}.plist

  9. majedian21
    January 16, 2013 at 5:46 pm

    This script works well in Casper except if the user is a network account. The fix I found was to do ~$currentuser/Library/Preferences…. instead of /Users/$currentuser. This will work for everyone, even if they’re not local users.

  10. Christian
    February 5, 2013 at 8:43 am

    Is it working also for java 7 or only java 6 ?

    • February 5, 2013 at 7:57 pm

      This script is for Apple’s Java 6.

  11. cembo
    February 22, 2013 at 8:33 pm

    Thanks for the workflow.
    For Mountain Lion use the below script before, then apply the launchd item. I haven’t test it with Lion OS but it should work.

    #!/bin/bash

    #If, after installing Java for OS X 2013-001 and the latest version of Java 7 from Oracle.
    #you want to disable Java 7 and re-enable the Apple-provided Java SE 6 plug-in

    rm -R /Library/Internet\ Plug-Ins/disabled

    mkdir -p /Library/Internet\ Plug-Ins/disabled

    mv /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin /Library/Internet\ Plug-Ins/disabled

    ln -sf /System/Library/Java/Support/Deploy.bundle/Contents/Resources/JavaPlugin2_NPAPI.plugin /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin

    ln -sf /System/Library/Frameworks/JavaVM.framework/Commands/javaws /usr/bin/javaws

    exit 0

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 151 other followers

%d bloggers like this: