Creating a really simple menubar app

Posted by: padmavyuha

Creating a really simple menubar app - 06/01/10 10:22 AM

I've got a very simple project in mind: it's a menubar app that shows an icon in the menubar in one of two different states, depending on which way a system prefs switch is set, and gives you the option to choose a key combo to trigger that switch.

Where would you start if you were building something like this for OSX? I'm pretty much still walking in the door of writing apps for OSX, so the simpler the better. I'm sure I could use Xcode to do this, but every time I launch it I get agoraphobia - so many options... agh!
Posted by: MacBozo

Re: Creating a really simple menubar app - 06/02/10 06:14 AM

I wonder if this thread will shed a little light:

Menulet
Posted by: padmavyuha

Re: Creating a really simple menubar app - 06/03/10 07:16 AM

I've had to rethink this - the preference I'm working with (the one in the Keyboard prefpane that tells OSX whether to call up Help or dim your screen when you hit F1 as opposed to Fn+F1 on some mac keyboards) is in .GlobalPreferences.plist but is held in ram once loaded. So there's no point in me editing the .plist file, what I need to do instead is find out what the OSX variable is that holds this value, so I can switch it.

Meanwhile, I've put together an AppleScript that uses the GUI to do the work, and pops up a brief message about the switch state afterwards. This works fine, except if I trigger it using iKeys, I get an annoying Run/Cancel dialogue first - not sure why that is.

Anyway, in case anyone's interested, here's the script:

Code:
(* script to switch on/off "Use all F1, F2, etc. keys as standard function keys" *)

set resultString to ""

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" of process "System Preferences"
			if value of checkbox 1 is 0 then
				click checkbox 1
				set resultString to "F1, F2, etc. set as function keys"
			else
				click checkbox 1
				set resultString to "F1, F2 etc. set as media controllers"
			end if
		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

delay 0.5

tell application "System Preferences"
	quit
end tell

if resultString is not "" then display dialog resultString buttons {""} giving up after 2.5
Posted by: padmavyuha

Re: Creating a really simple menubar app - 06/03/10 08:25 AM

Of course, this was silly - a better approach is to have the script ask me which state I want, and then change it if necessary (it still has to open the prefpane to check this, but at least you know what you're ending up with when you start).

Code:
(* script to switch on/off "Use all F1, F2, etc. keys as standard function keys" *)

set resultString to ""

set dialogString to "What do you want your function keys to be (without the Fn key)?"

display dialog dialogString buttons {"F1, F2...", "Media controls", "Cancel"} default button 3 with icon note

set choiceFlag to button returned of result
if choiceFlag = "Cancel" then return ""

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" of process "System Preferences"
			if choiceFlag is "F1, F2..." then
				if value of checkbox 1 is 0 then click checkbox 1
				set resultString to "F1, F2, etc. set as function keys"
			else
				if value of checkbox 1 is 1 then click checkbox 1
				set resultString to "F1, F2 etc. set as media controllers"
			end if
		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

delay 0.5

tell application "System Preferences"
	quit
end tell

if resultString is not "" then display dialog resultString buttons {""} with icon note giving up after 2
Posted by: MacBozo

Re: Creating a really simple menubar app - 06/03/10 08:31 AM

Heh! Your posts are wider than the style sheet. The ad space on the right is now obscuring your posts.
Posted by: padmavyuha

Re: Creating a really simple menubar app - 06/03/10 08:33 AM

Trying to fix the formatting, but AppleScript Editor puts in tabs that this site displays very wide!
Posted by: padmavyuha

Re: Creating a really simple menubar app - 06/03/10 08:54 AM

As a bizarre aside, if you run applescripts using iKey (or FastScripts etc.) to launch it via a key combo, you have to avoid any key combos that have a ctrl in them - otherwise you're stuck with the applescript startup dialog first, regardless of settings: