Script to Enable "Use F1, F2, etc. keys as standard Function keys on external keyboards"

jonathan_rudge
New Contributor III

I have created a self service item for some software called iTerm2 including a DMG that pushes out a configured preference file which includes F key configuration and profiles.

As part of the requirement for this I want to add a script to automatically turn on the above preference in keyboard settings for the user.

Does anyone have anything already created to cover this that doesn't require a restart of the system?

Thanks

Jon

14 REPLIES 14

etippett
Contributor II

I've used this in the past (change path to reflect the name of the user for which you want to change the setting)

defaults write /Users/username/Library/Preferences/.GlobalPreferences com.apple.keyboard.fnState 1
chown username /Users/username/Library/Preferences/.GlobalPreferences

With the caching of preferences with cfprefsd, you will probably need to kill that process in order for this to take effect. Worse, it may actually overwrite your changes. My workflow changes these settings on first boot, so that's not an issue for me.

Eric

bentoms
Release Candidate Programs Tester

@etippett Defaults is part of the preferences system, so shouldn't need to kill cfprefsd

jonathan_rudge
New Contributor III

What if you wanted this as a global policy? As its in self service I technically wouldn't know the username of the person.....

etippett
Contributor II

Ah, that's right, @bentoms . Thanks for calling out my error.

@jonathan.rudge Is the account your users use to login to the OS the same as what they are using for Self Service (directory-based)? If so, just use the built-in $3 variable for user in your script.
[https://www.jamf.com/jamf-nation/articles/146/script-parameters](link URL)

Or, you can get the username of the currently logged in user with this code: ls -l /dev/console | awk '{print $3}'
Store that in a variable, and use that variable in the other commands.

Good luck!
Eric

jonathan_rudge
New Contributor III

@etippett we have a mixed site of local mobile accounts and directory accounts would this cause an issue?

etippett
Contributor II

Mobile accounts are directory accounts. Are the username and password they use to login to the OS the same as what they use to login to Self Service?

jonathan_rudge
New Contributor III

Sorry I didn't explain myself very well!

We have some users where a mobile account has been created when logging onto the Mac that has sync'd with AD (first name.surname)

New (today world) are created by adding a local account for the user and then they set the account. (this could be an abbreviation of their first name, sometimes with or without the surname i.e. Jon or JonR

etippett
Contributor II

You still haven't said how they login to Self Service (perhaps you don't require them to), so it's uncertain whether $3 will work for you. Therefore it sounds like you're best off using the command I provided above to get the currently logged in user.

jonathan_rudge
New Contributor III

Sorry, they don't logon to self service, we push out the application to the device for them to use without the additional need to sign in.

etippett
Contributor II

In that case you'll need to use ls -l /dev/console | awk '{print $3}' to get the current user.

jonathan_rudge
New Contributor III

Thank you btw!

Anonymous
Not applicable

For our end of year testing, I need to have the Use all F1, F2, etc keys as standard function keys checked for all user accounts(current and future add accounts) on a group of computers. I tried a basic script but it doesn't work because it doesn't run as the logged in user. I see that you are saying to add the variable. The thing is I do not know how/where to add the variable. I'm still able to write only very basic scripts.

casey306
New Contributor

smccullar - Any luck? This is exactly what I need to accomplish (apply the F1 as standard function preference to all users... unknown users)!
Thanks!

jmig
New Contributor II

@smccullar did you get this to work?