Previous Thread
Next Thread
Print Thread
Hop To
Creating a really simple menubar app #524680 06/01/10 05:22 PM
Joined: Sep 2003
Posts: 4,921
padmavyuha Offline OP
Trans Tomboy™
OP Offline
Trans Tomboy™
Joined: Sep 2003
Posts: 4,921
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!


If it's brokenless, don't suffix it...
Re: Creating a really simple menubar app [Re: padmavyuha] #524787 06/02/10 01:14 PM
Joined: Apr 2002
Posts: 17,709
MacBozo Offline
Nut Dood
Offline
Nut Dood
Joined: Apr 2002
Posts: 17,709
I wonder if this thread will shed a little light:

Menulet


[Linked Image from dl.dropbox.com]
Re: Creating a really simple menubar app [Re: MacBozo] #524918 06/03/10 02:16 PM
Joined: Sep 2003
Posts: 4,921
padmavyuha Offline OP
Trans Tomboy™
OP Offline
Trans Tomboy™
Joined: Sep 2003
Posts: 4,921
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


If it's brokenless, don't suffix it...
Re: Creating a really simple menubar app [Re: padmavyuha] #524929 06/03/10 03:25 PM
Joined: Sep 2003
Posts: 4,921
padmavyuha Offline OP
Trans Tomboy™
OP Offline
Trans Tomboy™
Joined: Sep 2003
Posts: 4,921
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


If it's brokenless, don't suffix it...
Re: Creating a really simple menubar app [Re: padmavyuha] #524930 06/03/10 03:31 PM
Joined: Apr 2002
Posts: 17,709
MacBozo Offline
Nut Dood
Offline
Nut Dood
Joined: Apr 2002
Posts: 17,709
Heh! Your posts are wider than the style sheet. The ad space on the right is now obscuring your posts.


[Linked Image from dl.dropbox.com]
Re: Creating a really simple menubar app [Re: MacBozo] #524931 06/03/10 03:33 PM
Joined: Sep 2003
Posts: 4,921
padmavyuha Offline OP
Trans Tomboy™
OP Offline
Trans Tomboy™
Joined: Sep 2003
Posts: 4,921
Trying to fix the formatting, but AppleScript Editor puts in tabs that this site displays very wide!


If it's brokenless, don't suffix it...
Re: Creating a really simple menubar app [Re: padmavyuha] #524934 06/03/10 03:54 PM
Joined: Sep 2003
Posts: 4,921
padmavyuha Offline OP
Trans Tomboy™
OP Offline
Trans Tomboy™
Joined: Sep 2003
Posts: 4,921
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:



If it's brokenless, don't suffix it...

Moderated by  Acumowchek, MacGizmo, Reboot