Home > Java, Mac administration, Mac OS X, Scripting > Automatically enable the Java web plug-ins setting in Safari 6.0.3 and later

Automatically enable the Java web plug-ins setting in Safari 6.0.3 and later

One of the features of Apple’s Safari 6.0.3 update is that it turns off the automatic execution of Java applets through Safari, even if the Java browser plug-in is otherwise enabled.

Safari 6.0.3 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 a couple of applications that rely on Java applets running through the browser. Fortunately, I already had a fix for this issue; it just needed to be updated with some additional commands.

To make this work, I’ve written a script and launch agent combination. The script will perform a couple of tasks:

1. Set the com.apple.WebKit.JavaPlugInLastUsedTimestamp plist key in ~/Library/Preferences/.GlobalPreferences.plist

2. Enable the Enable applet plug-in and Web Start Applications setting in the Java Preferences application.


#!/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

# Enable Java browser plug-ins in Safari 6.0.3 and later
# for the current user by setting the com.apple.WebKit.JavaPlugInLastUsedTimestamp
# key in ~/Library/Preferences/.GlobalPreferences.plist

/usr/libexec/PlistBuddy -c "Delete :com.apple.WebKit.JavaPlugInLastUsedTimestamp" $HOME/Library/Preferences/.GlobalPreferences.plist
/usr/libexec/PlistBuddy -c "Add :com.apple.WebKit.JavaPlugInLastUsedTimestamp real $(( $(date "+%s") - 978307200 ))" $HOME/Library/Preferences/.GlobalPreferences.plist
/usr/bin/plutil -convert xml1 $HOME/Library/Preferences/.GlobalPreferences.plist

# 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" $HOME/Library/Preferences/ByHost/com.apple.java.JavaPreferences.${MAC_UUID}.plist
/usr/libexec/PlistBuddy -c "Add :GeneralByTask:Any:WebComponentsEnabled bool true" $HOME/Library/Preferences/ByHost/com.apple.java.JavaPreferences.${MAC_UUID}.plist
/usr/libexec/PlistBuddy -c "Delete :GeneralByTask:Any:WebComponentsLastUsed" $HOME/Library/Preferences/ByHost/com.apple.java.JavaPreferences.${MAC_UUID}.plist
/usr/libexec/PlistBuddy -c "Add :GeneralByTask:Any:WebComponentsLastUsed real $(( $(date "+%s") - 978307200 ))" $HOME/Library/Preferences/ByHost/com.apple.java.JavaPreferences.${MAC_UUID}.plist
/usr/bin/plutil -convert xml1 $HOME/Library/Preferences/ByHost/com.apple.java.JavaPreferences.${MAC_UUID}.plist

# Forces preferences to be re-read

/usr/bin/killall cfprefsd

The LaunchAgent runs the script on login to any user account with the logging-in user’s privileges and permissions.

You can find the updated script here on my GitHub repo:

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

  1. jonmoore123
    March 16, 2013 at 2:08 am

    Outstanding. Thank you very much.

  2. Jeffery Nelson
    March 16, 2013 at 7:00 am

    Great effort here, but it didn’t seem to work for me. I’m trying to launch a jnlp from Safari. Followed the readme. Any suggestions? Thanks either way.

    • March 16, 2013 at 9:52 pm

      Jeffery,

      Your issue may be related to the second note on this Apple KBase article:

      http://support.apple.com/kb/HT5672

      “CoreTypes

      Available for: OS X Lion v10.7 to v10.7.5, OS X Lion Server v10.7 to v10.7.5, OS X Mountain Lion v10.8 to v10.8.2

      Impact: Visiting a maliciously crafted website could allow a Java Web Start application to be launched automatically even if the Java plug-in is disabled

      Description: Java Web Start applications would run even if the Java plug-in was disabled. This issue was addressed by removing JNLP files from the CoreTypes safe file type list, so the Web Start application will not be run unless the user opens it in the Downloads directory.

      CVE-ID

      CVE-2013-0967”

  3. Jeffery Nelson
    March 16, 2013 at 10:42 pm

    Thank you for the response. I take it a “user” cannot add this file type to the “safe file” list? Keep up the good coding.

    Best regards,
    j

  4. Ian
    March 17, 2013 at 12:07 am

    Thanks for the heads up! I hadn’t even heard about the Java change until you mentioned it!! What tipped your hand, any particular resource I can additionally throw into my feeds?

  5. March 18, 2013 at 5:57 pm

    Thanks Rich – it seems to me the way to get the timer to reset to the current date is to click on the “Inactive plug-in” text, which seems to trigger loading the plugin. The text will also show if the com.apple.WebKit.JavaPlugInLastUsedTimestamp key isn’t present at all.

    I suspect this is used to apply the same expiry system to the Java 7 web plugin, whereas the previous mechanism targeted Apple’s Java. On a machine with no Apple Java but with only Oracle Java installed, the old com.apple.java.JavaPreferences ByHost domain isn’t modified when I first load the plugin.

  6. AB
    March 21, 2013 at 1:11 am

    It worked like a charm! Thank you!

  7. Kyle
    March 29, 2013 at 11:44 am

    Does it work for Java 7 also? or just for Apple’s Java 6.

    Thanks!

    • March 29, 2013 at 12:54 pm

      Kyle,

      This applies to both Java 7 and Java 6.

  8. Kyle
    March 29, 2013 at 2:33 pm

    Thanks rtrouton. Then I may be doing something wrong, because I have Java 7 and it does not work on my Safari 6.0.3.

  9. April 3, 2013 at 8:58 am

    Question: how is this key (“com.apple.WebKit.JavaPlugInLastUsedTimestamp”) updated?

    As I understand it, once the 30 days are up, you will no longer be able to launch Java applets in a web browser. Is this key updated when you launch a Java applet or do you have to enable it manually (or with this script).

    A small thing I do not understand: your script produces a time stamp in the form of “386671648” but when I update the time stamp from Oracles webpage (http://www.java.com/en/download/testjava.jsp) it becomes “386606982.494483”. It doesn’t appear to impact how it works, but it feels a bit weird.

    Thank you for the script, though!

  10. April 12, 2013 at 1:40 pm

    Reblogged this on Java Reblog.

  11. dave trouble
    October 4, 2013 at 2:35 pm

    why do you delete the key and readd it? can’t you just set it?

  12. ramuntcho
    December 4, 2014 at 10:48 am

    Is it still useful with Yosemite and Java 8 ?

  1. No trackbacks yet.

Leave a comment