Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 8, 2007, 03:53 PM   #1
obrien234
macrumors regular
 
Join Date: Apr 2005
Location: Brighton, MI
Applescript to toggle F1-F11 keys as function keys

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 is offline   0 Reply With Quote
Old Nov 8, 2007, 05:07 PM   #2
obrien234
Thread Starter
macrumors regular
 
Join Date: Apr 2005
Location: Brighton, MI
GUI Script

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
obrien234 is offline   0 Reply With Quote
Old Nov 8, 2007, 10:34 PM   #3
weinrdog
macrumors newbie
 
Join Date: Oct 2007
Location: o-HI-o
Send a message via Yahoo to weinrdog
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...
weinrdog is offline   0 Reply With Quote
Old Feb 10, 2008, 05:46 PM   #4
thirdwaver
macrumors member
 
Join Date: Jun 2003
Quote:
Originally Posted by weinrdog View Post
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?
__________________
"Light a fire for a man and he'll be warm for a day. Light a man on fire and he'll be warm for the rest of his life."
thirdwaver is offline   0 Reply With Quote
Old Apr 17, 2008, 10:21 AM   #5
mcbeej
macrumors member
 
Join Date: Jun 2007
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..?
mcbeej is offline   0 Reply With Quote
Old Feb 22, 2011, 11:57 AM   #6
rezecib
macrumors newbie
 
Join Date: Dec 2009
Updated Applescript

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.

Code:
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
rezecib is offline   0 Reply With Quote
Old Apr 28, 2011, 01:01 PM   #7
droppedd
macrumors newbie
 
Join Date: Mar 2008
I've bundled up a similar script that also uses Growl notifications to GitHub; see the
project page on GitHub or download the app.
droppedd is offline   0 Reply With Quote
Old Jul 14, 2012, 02:28 AM   #8
G3t/All
macrumors newbie
 
Join Date: Jul 2012
App to toggle without using Growl

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):

Code:
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
Attached Files
File Type: zip Function Key Toggle.app.zip (60.6 KB, 80 views)

Last edited by OllyW; Jul 14, 2012 at 09:58 AM. Reason: Please use code tags
G3t/All is offline   0 Reply With Quote
Old Feb 4, 2013, 10:34 PM   #9
mcgrath3
macrumors newbie
 
Join Date: May 2009
Applescript to toggle Keyboard Shortcuts

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:

Code:
click checkbox 1 of outline 1 of tab group 1 of window "Keyboard"
Code:
click checkbox 1 of row 1 of table 1 of scroll area 1 of group "Display" of table "Display shortcuts"
Code:
tell checkbox "Show Launchpad" of scroll area "Launchpad and Dock shortcuts" of tab group 1 of window 1
Code:
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:

Code:
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

Last edited by mcgrath3; Feb 4, 2013 at 10:47 PM.
mcgrath3 is offline   0 Reply With Quote
Old Feb 5, 2013, 06:22 AM   #10
kryten2
macrumors 6502a
 
Join Date: Mar 2012
Location: Belgium
Quote:
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_...inspector.html
__________________
Space Corps Directive 34124
kryten2 is offline   0 Reply With Quote
Old Feb 6, 2013, 04:25 PM   #11
mcgrath3
macrumors newbie
 
Join Date: May 2009
Applescript to toggle F1-F11 keys

Thanks I am using that tool and it is helping a lot to figure out the right labels for these.
mcgrath3 is offline   0 Reply With Quote
Old Apr 6, 2013, 01:54 PM   #12
kiodane
macrumors newbie
 
Join Date: Oct 2009
Added ControllerMate into the action

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/.

Click image for larger version

Name:	Screen Shot 2013-04-06 at 1.47.38 PM.png
Views:	20
Size:	187.7 KB
ID:	406551

Click image for larger version

Name:	Screen Shot 2013-04-06 at 1.47.44 PM.png
Views:	17
Size:	175.7 KB
ID:	406552

My Scripts

Function Swapper.applescript

Code:
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

Code:
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

Code:
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

Click image for larger version

Name:	Screen Shot 2013-04-06 at 1.17.29 PM.png
Views:	66
Size:	495.1 KB
ID:	406550
kiodane is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
function keys are messed up whoossaa Mac Basics and Help 1 Dec 26, 2013 04:46 PM
Function Keys Problem hjerpsted MacBook Air 0 Nov 20, 2013 11:54 AM
Can I have cmd + volume keys (f11, f12) changing iTunes volume? madsylar OS X 5 Oct 12, 2013 11:03 AM
Having certain Function keys be OS X and others not? ConfusedBatman Mac Peripherals 0 Oct 7, 2013 01:19 PM

Forum Jump

All times are GMT -5. The time now is 09:22 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC