Use caps-lock to lock fn key or other way to lock fn key?
To address the last point in your post, "Can I toggle the option in the Keyboard preference pane with a script?", here is the AppleScript code that will toggle the state of the "Use all F1, F2, etc. keys as standard function keys" checkbox on the Keyboard tab of the Keyboard System Preferences.

tell application "System Preferences"
    activate
    set the current pane to pane id "com.apple.preference.keyboard"
    reveal anchor "keyboardTab" of pane id "com.apple.preference.keyboard"
end tell

tell application "System Events" to tell process "System Preferences"
    click checkbox 1 of tab group 1 of window 1
end tell

quit application "System Preferences"


Note:  This code was tested under OS X 10.8.5. It may need to be adjusted for later versions of OS X.

You could use this code in an AppleScript application that could be placed in the Dock or in an Automator Service Workflow that could be assigned a Keyboard Shortcut, etc. If you need help with that, just ask.