PDA

View Full Version : Applescript to toggle F1-F11 keys as function keys




obrien234
Nov 8, 2007, 03:53 PM
Hi,

So I've got a Powerbook G4, and as Apple keeps adding features like Expose and Spaces, I have to hold the function key to use them.

At different times I like to be able to just hit F8, F9, F10,and have it not turn on my backlight keys (Photoshop, Video Editing, etc). While other times I like to have my volume control without using the Fn Key (just browsing the web and listening to music)

So now my question:

I wrote a GUI Script that toggles the Fn Key through System Pref in Keyboard & Mouse, But when I run it, it is disruptive and opens System Preferences.

Does anyone know how write the script so it doesn't have to go through the GUI (or just doesn't pop up System Preferences window)?

I'll post the code I have in an hour or so. Currently I'm on a computer at work. Thanks.



obrien234
Nov 8, 2007, 05:07 PM
Here's the current code I have.



tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.keyboard"
end tell


tell application "System Events"
if UI elements enabled then
tell tab group 1 of window "Keyboard & Mouse" of process "System Preferences"
click checkbox "Use all F1, F2, etc. keys as standard function keys"
end tell
else
tell application "System Preferences"
activate
set current pane ¬
to pane "com.apple.preference.universalaccess"
display dialog ¬
"UI element scripting is not enabled. Check \"Enable access for assistive devices\""
end tell
end if
end tell

tell application "System Preferences"
quit
end tell

weinrdog
Nov 8, 2007, 10:34 PM
You could use the terminal command 'defaults'.

defaults read "Apple Global Domain" com.apple.keyboard.fnState

will show the current state of 0 or 1.

defaults write ... should update it.

Post back if/when you get it...

thirdwaver
Feb 10, 2008, 05:46 PM
You could use the terminal command 'defaults'.

defaults read "Apple Global Domain" com.apple.keyboard.fnState

will show the current state of 0 or 1.

defaults write ... should update it.

Post back if/when you get it...

I have a need for this also. Mine is driven by World of Warcraft. When I'm playing the game, I want those "special" keys on the top row of the new Apple keyboard to be regular function keys. When I'm not playing I want them to control iTunes.

I can use the commands you gave to read and write (and my reads after the writes show the changes) but the gui is not changing (system prefs is not open when I execute the commands) and more importantly, the functionality is not changing as I expected it to. If I go to the gui and change the box state, it is reflected in the next read I do in terminal so I know this is the right key/pair. Any idea what might be causing it not to update from the terminal side?

Do I need to stop/start a process or issue a follow up command to get the system to "reload" the new setting?

mcbeej
Apr 17, 2008, 10:21 AM
Ooh, this is something I want as well. I get the same behaviour as you regarding the terminal defaults setting - it updates the value, but gives no change in behaviour.

Any one else got any better ways to do this..?

rezecib
Feb 22, 2011, 11:57 AM
From what I can gather, setting the bit in defaults is like changing an address in someone's address book; if they're actively dealing with that address, they're not looking it up every time (unless they have terrible memory). Basically, the OS only checks the bit on startup (and writes it when you check the box). So…*that won't work.

However, if you remove the "activate" statements from the first applescript, you get System Preferences to launch in the background and do it more quickly; the only visual cue that it's occurred is a bounce of system preferences on the dock.


tell application "System Preferences"
set current pane to pane "com.apple.preference.keyboard"
end tell


tell application "System Events"
if UI elements enabled then
tell tab group 1 of window "Keyboard" of process "System Preferences"
click checkbox "Use all F1, F2, etc. keys as standard function keys"
end tell
else
tell application "System Preferences"
set current pane ¬
to pane "com.apple.preference.universalaccess"
display dialog ¬
"UI element scripting is not enabled. Check \"Enable access for assistive devices\""
end tell
end if
end tell

tell application "System Preferences"
quit
end tell

droppedd
Apr 28, 2011, 01:01 PM
I've bundled up a similar script that also uses Growl notifications to GitHub; see the
project page on GitHub (https://github.com/ysamlan/toggle-function-keys) or download the app (https://github.com/downloads/ysamlan/toggle-function-keys/Toggle%20Function%20Keys.zip).

G3t/All
Jul 14, 2012, 02:28 AM
I noticed that growl is basically going to be replaced by notification center in Mountain Lion and I needed this script to work for my photoshop/illustrator shortcuts. Here's my version. I couldn't get the script to stop hanging in launch bar (how i launch it), so I saved it as an app instead. Hope this helps someone, as I've registered just to share this. ;) Remember, I had to save it as an app instead of a script to get it to work for me. Would appreciate any feedback/suggestions. This also displays a dialog box that you click to acknowledge the function keys or the media keys are on (I've also attached the script saved as an app if your really lazy):

tell application "System Preferences"
set current pane to pane "com.apple.preference.keyboard"
end tell

tell application "System Events"
-- If we don't have UI Elements enabled, then nothing is really going to work.
if UI elements enabled then
tell application process "System Preferences"
get properties

click radio button "Keyboard" of tab group 1 of window "Keyboard"
click checkbox "Use all F1, F2, etc. keys as standard function keys" of tab group 1 of window "Keyboard"
set messageToShow to "Function keys set to "
if (value of checkbox "Use all F1, F2, etc. keys as standard function keys" of tab group 1 of window "Keyboard") as boolean then
set messageToShow to messageToShow & "standard function keys"
else
set messageToShow to messageToShow & "media/hardware controls"
end if

tell application "Finder"
display dialog messageToShow
end tell

end tell
tell application "System Preferences" to quit
else
-- GUI scripting not enabled. Display an alert
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.universalaccess"
display dialog "UI element scripting is not enabled. Please activate \"Enable access for assistive devices\""
end tell
end if
end tell

mcgrath3
Feb 4, 2013, 10:34 PM
I have been trying to figure out how to access each of the Assigned FKeys in the Keyboard Shortcuts Pane using Applescript. I am having a heck of a time trying to deal with the side by side ? tables or ? outlines. I could really use some help with how to call them in AS. Accessing a checkbox in a table is what I am stuck on.

I have tried a hundred different combinations including:

click checkbox 1 of outline 1 of tab group 1 of window "Keyboard"

click checkbox 1 of row 1 of table 1 of scroll area 1 of group "Display" of table "Display shortcuts"

tell checkbox "Show Launchpad" of scroll area "Launchpad and Dock shortcuts" of tab group 1 of window 1

if (value of checkbox "Show Launchpad" of tab group 2 of window 1 as boolean then
click checkbox "Show Launchpad" of tab group 2 of window 1
end tell

etc.

I am using this code to toggle the "Use all F1...." of the keyboard pane which works great but now I need to also uncheck some of the Function Key Shortcuts boxes in the Shortcuts Pane. The problem is they are embedded in Tables or Outlines and I don't know how to access them. Any help is greatly appreciated:

tell application "System Preferences"
set current pane to pane "com.apple.preference.keyboard"
get the name of every anchor of pane id "com.apple.preference.keyboard"
-->returns: {"keyboardTab", "shortcutsTab", "keyboardTab_ModifierKeys"}
reveal anchor "keyboardTab" of pane id "com.apple.preference.keyboard"
end tell

tell application "System Events"
if UI elements enabled then
tell application process "System Preferences"
get properties
click radio button "Keyboard" of tab group 1 of window "Keyboard"
if (value of checkbox "Use all F1, F2, etc. keys as standard function keys" of tab group 1 of window "Keyboard") as boolean then
-- If already on then do nothing
else
click checkbox "Use all F1, F2, etc. keys as standard function keys" of tab group 1 of window "Keyboard"
end if
end tell
tell application "System Preferences" to quit
else
tell application "System Preferences"
activate
set current pane ¬
to pane "com.apple.preference.universalaccess"
display dialog ¬
"Please Check \"Enable access for assistive devices\""
end tell
end if
end tell

kryten2
Feb 5, 2013, 06:22 AM
UI Browser vs. Apple's UIElementInspector
Apple has long offered a free utility—variously called UI Element Inspector, Accessibility Inspector, and UIElementInspector—as part of the Developer Tools installation package. Apple released the latest version on June 3, 2010 as the UIElementInspector 1.4 sample code project, which you can build and install on your computer. The new version of UIElementInspector has been completely rewritten as a Cocoa application, but its functionality appears to be unchanged since version 1.2.


Info : http://pfiddlesoft.com/uibrowser/vs_ui_element_inspector.html

mcgrath3
Feb 6, 2013, 04:25 PM
Thanks I am using that tool and it is helping a lot to figure out the right labels for these.

kiodane
Apr 6, 2013, 01:54 PM
Using ControllerMate 4 (http://www.orderedbytes.com/controllermate/), I created a page for enabling mouse configuration for StarCraft 2 that swaps back when I tab out to Safari or Finder (anything not SC2). Using the above scripts as a starting place, I added AppleScript blocks that specifically enable or disable the Function Key behavior, as well as set up a fallback trigger.

My modified scripts uses a shell script to test the current value of the F-key row state, decides if it should swap the value and if it should display a message if changed. I'm displaying my message via Notification Center with the help of this trick: https://github.com/alloy/terminal-notifier All my Macs are 10.8, so it doesn't check for version number or that terminal-notifier.app is properly installed into ~/Applications/.

406551

406552

My Scripts

Function Swapper.applescript


tell application "System Preferences"
set current pane to pane "com.apple.preference.keyboard"
end tell

tell application "System Events"
if UI elements enabled then
tell tab group 1 of window "Keyboard" of process "System Preferences"
click checkbox "Use all F1, F2, etc. keys as standard function keys"

if (do shell script "defaults read -g com.apple.keyboard.fnState") = "1" then
set fnStateRead to "Special Keys - Volume, iTunes..."
else
set fnStateRead to "Function Keys - F1, F2, F3..."
end if

do shell script "~/Applications/terminal-notifier.app/Contents/MacOS/terminal-notifier -message \"Function Swapper, an AS & Shell script\" -title \"" & fnStateRead & "\" -activate com.orderedbytes.ControllerMate4"

end tell
else
tell application "System Preferences"
set current pane ¬
to pane "com.apple.preference.universalaccess"
display dialog ¬
"UI element scripting is not enabled. Check \"Enable access for assistive devices\""
end tell
end if
end tell

tell application "System Preferences"
quit
end tell


Function Function.applescript


tell application "System Preferences"
set current pane to pane "com.apple.preference.keyboard"
end tell

tell application "System Events"
if UI elements enabled then
tell tab group 1 of window "Keyboard" of process "System Preferences"

if (do shell script "defaults read -g com.apple.keyboard.fnState") = "0" then

click checkbox "Use all F1, F2, etc. keys as standard function keys"

set fnStateRead to "Function Keys - F1, F2, F3..."

do shell script "~/Applications/terminal-notifier.app/Contents/MacOS/terminal-notifier -message \"Function Swapper, an AS & Shell script\" -title \"" & fnStateRead & "\" -activate com.orderedbytes.ControllerMate4"

end if
end tell
else
tell application "System Preferences"
set current pane ¬
to pane "com.apple.preference.universalaccess"
display dialog ¬
"UI element scripting is not enabled. Check \"Enable access for assistive devices\""
end tell
end if
end tell

tell application "System Preferences"
quit
end tell



Function Special.applescript


tell application "System Preferences"
set current pane to pane "com.apple.preference.keyboard"
end tell

tell application "System Events"
if UI elements enabled then
tell tab group 1 of window "Keyboard" of process "System Preferences"

if (do shell script "defaults read -g com.apple.keyboard.fnState") = "0" then
else

click checkbox "Use all F1, F2, etc. keys as standard function keys"

set fnStateRead to "Special Keys - Volume, iTunes..."

do shell script "~/Applications/terminal-notifier.app/Contents/MacOS/terminal-notifier -message \"Function Swapper, an AS & Shell script\" -title \"" & fnStateRead & "\" -activate com.orderedbytes.ControllerMate4"

end if
end tell
else
tell application "System Preferences"
set current pane ¬
to pane "com.apple.preference.universalaccess"
display dialog ¬
"UI element scripting is not enabled. Check \"Enable access for assistive devices\""
end tell
end if
end tell

tell application "System Preferences"
quit
end tell


My ControllerMate 4 configuration

406550