Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

10.3: Restore the Key Caps application via AppleScript System
From time to time, I need to take a peek at the Keyboard Viewer, but I despise having the Input Menu in my menu bar since it serves no other purpose for me and just takes up space. And for whatever reason, the Input Menu is the only keyboard extra to make heavy use of color, which I find distracting. Anyway, there's an application called "KeyboardViewerServer" located at /System -> Library -> Components -> KeyboardViewer.component -> Contents -> SharedSupport. Running this app opens the Keyboard Viewer pallete. However, if this app is already running (it is not quit when the pallete is closed) then "opening it has no effect."

So I wrote a short AppleScript that checks to see if this process exists, quits it if it does, and then launches KeyboardViewerServer. It's worked perfectly for me. I saved the script as an application, called it "Keyboard Caps" and put it in my Utilities folder. Now I can finally get rid of the input menu. Here's the script.
tell application "System Events"
  if exists process "KeyboardViewerServer" then
    try
      quit application "KeyboardViewerServer"
    end try
  end if
end tell

tell application "Finder"
  -- NOTE: Enter the following two lines as ONE LINE without spaces!
  open item "System:Library:Components:KeyboardViewer.component:
  Contents:SharedSupport:KeyboardViewerServer" of the startup disk
end tell
Now "Key Caps" is back in its original location.

[robg adds: The first couple of times I tried to compile this, it failed. I have no idea why, as after simply adding and removing a space from the script, it compiled and ran just fine...]
    •    
  • Currently 2.75 / 5
  You rated: 5 / 5 (4 votes cast)
 
[12,364 views]  

10.3: Restore the Key Caps application via AppleScript | 13 comments | Create New Account
Click here to return to the '10.3: Restore the Key Caps application via AppleScript' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.3: Restore the Key Caps application via AppleScript
Authored by: Jay on Feb 26, '04 01:01:25PM

I couldn't get this work in Panther at first. When I tried to compile, a dialog box came up that said "where is KeyboardViewerServer?" It wanted me to browse for it or select it from a list of all applications, and it wasn't in that list.

I browsed for it, and it was in the path listed. Once I chose it, it worked. Is there a typo in the path in the script? I didn't notice one, but can't explain why it didn't work at first.



[ Reply to This | # ]
10.3: Restore the Key Caps application via AppleScript
Authored by: BulbVivid on Feb 26, '04 02:28:05PM

Thanks!



[ Reply to This | # ]
10.3: Restore the Key Caps application via AppleScript
Authored by: regulus on Feb 26, '04 02:50:12PM

That's awesome and worked just as explained.

As you mentioned, closing the pallett doesn't quit the keyboardviewerserver process (as evidenced by using the top command in the terminal). Is there a better way of quitting the server (after I close the pallett) other than killing it in the terminal? I guess I can make another applescript with just the first part of your script to quit it, but that seems a bit unelegant to have to run a second script. Are there any other suggestions? Can one applescript be made which launches the keyboardviewerserver as explained, but waits around in the background looking for when I close the keyboardviewer pallett, and then quits the server when I close the pallett, and then quits the applescript? I hate to leave processes running for something that I will use infrequently.



[ Reply to This | # ]
10.3: Restore the Key Caps application via AppleScript
Authored by: jakacmar on Feb 26, '04 04:52:53PM
Here's an addition to the script which should quit the KeyboardViewerServer Process when the window is closed. For this to work, the app must be saved as "stay open". One caveat, for some reason when the app quits itself (which it doesn't do very well) it must first be activated (very annoying but it's done automatically and I can't figure out a fix right now), also, for the actual "Quitting" to take place, you need to click on another app (any other app, even the Desktop). Once you do this, the icon will be removed form the Dock etc. Give it a try and you'll see what I mean. I know this is ugly script but I just threw it together real quick and don't have time to tinker with it now. Had I been able to figure this out in the first place I would've just included it in the original hint submission. So here goes, just paste this extra code at the very end of the script (after the last end tell) and save it as an app with "stay open" checked.

on idle
	repeat
		tell application "System Events"
			tell process "KeyboardViewerServer"
				if exists window "Keyboard Viewer" then
					set isOpen to true
				else
					set isOpen to false
				end if
			end tell
			if not isOpen then
				try
					quit application "KeyboardViewerServer"
				end try
				exit repeat
			else
				do shell script "sleep 1"
			end if
		end tell
	end repeat
	if not isOpen then
		tell me to activate
		tell application "System Events"
			tell process me
				keystroke "q" using command down
			end tell
		end tell
	end if
end idle


[ Reply to This | # ]
Also works with Character Palette
Authored by: SimonDorfman.com on Feb 26, '04 05:17:55PM
Thanks for the script. I modified it to work with the Character Palette which I use quite often. (normally you would launch it here: Finder Menu -> Edit -> Special Characters...)

tell application "System Events"
	if exists process "CharPaletteServer" then
		try
			quit application "CharPaletteServer"
		end try
	end if
end tell

tell application "Finder"
	open item "System:Library:Components:CharacterPalette.component:Contents:SharedSupport:CharPaletteServer.app" of the startup disk
end tell
or click here to open this script in Script Editor. (More info about how to make links like this here.)

[ Reply to This | # ]
10.3: Restore the Key Caps application via AppleScript
Authored by: autoxr on Feb 26, '04 06:26:22PM
Here's an enhancement of the original application to handle the window closing as well as better handling of the app already running (I found that it took two launches to get the keyboard viewer app back running after closing the window). Create an applescript and paste the following code into that. Save the script as an Application and check the "Stay Open" checkbox. Then other than an annoying progress cursor every time it polls for the window being open, it'll behave just as if it was the KeyCaps application itself. Closing the window will quit both apps and quitting the script application will quit the keyboard viewer app. Paste a nice pretty icon onto the script application and I bet that you'd fool people into thinking that it was the old KeyCaps updated for panther :-)

on run
	tell application "System Events"
		if exists process "KeyboardViewerServer" then
			try
				quit application "KeyboardViewerServer"
				repeat until exists process "KeyboardViewerServer" is false
					delay 1
				end repeat
			end try
		end if
	end tell
	
	tell application "Finder"
		open item "System:Library:Components:KeyboardViewer.component:Contents:SharedSupport:KeyboardViewerServer.app" of the startup disk
	end tell
	
	tell application "System Events"
		repeat until exists process "KeyboardViewerServer"
			delay 1
		end repeat
	end tell
	
	set frontmost to false
end run

on idle
	tell application "System Events"
		if exists process "KeyboardViewerServer" then
			set windList to every window of application process "KeyboardViewerServer"
			if (count of windList) is 0 then
				quit me
			end if
		end if
	end tell
	return 1
end idle

on quit
	tell application "System Events"
		if exists process "KeyboardViewerServer" then
			try
				quit application "KeyboardViewerServer"
			end try
		end if
	end tell
	continue quit
end quit


[ Reply to This | # ]
10.3: Restore the Key Caps application via AppleScript
Authored by: workerB on Feb 26, '04 06:59:16PM

Trying autoxr's script I keep getting the same error/notice:

"can't set frontmost to false"

What's up?



[ Reply to This | # ]
10.3: Restore the Key Caps application via AppleScript
Authored by: Eanes03WFU on Jul 24, '04 02:45:41PM

I get the same error message, and know virtually nothing about AppleScript so I've been unable to edit the script... a little help?



[ Reply to This | # ]
10.3: Restore the Key Caps application via AppleScript
Authored by: Azark on Feb 26, '04 06:31:43PM

Or you could just take the Key Caps app that was in Jaguar...
That's one of the first thing I did after installing Panther.

---
PB Ghz 1024MB Combo 10.3.2
G4 400 AGP 768MB 10.3.2



[ Reply to This | # ]
10.3: Restore the Key Caps application via AppleScript
Authored by: vniow on Feb 27, '04 03:36:06PM

Yeah, you can get the app here, its a small file:

http://forums.macrumors.com/attachment.php?s=&postid=696610



[ Reply to This | # ]
Bad fixes to the script
Authored by: nyarlathotep on Mar 02, '04 01:36:56AM

You guys should not be using these "stay open" applescripts, as they are likely to suck up too much CPU. Instead, I'd suggest having it cue a variant of itself to be run 5min from now with the at program. If the window is still open, it can ignore it, if the window is closed, it can kill the process.



[ Reply to This | # ]
10.3: Restore the Key Caps application via AppleScript
Authored by: dennisbest on Jan 06, '05 12:28:17PM

Here's what worked for me without having to leave a script open. I just close the window when I'm done with the keyboard viewer - I don't really care if it's still running and taking no resources. This puppy checks to see if it is open and visible... if not, it tries to quit and reopen it. And then it just tries it all over again just in case. (For some reason repeat routines weren't working... and the quit errors only seemed to occur once... so I just gave up elegance for functionality.) It won't win an award for coding but it has worked for me 100% of the time. This script does not need to stay open. Just place it in your scripts menu or wherever you like.

try
tell application "System Events"
if exists process "KeyboardViewerServer" then
set windList to every window of application process "KeyboardViewerServer"
if (count of windList) is 0 then
tell application "KeyboardViewerServer"
quit
end tell
end if
else
tell application "Finder"
open item "System:Library:Components:KeyboardViewer.component:Contents:SharedSupport:KeyboardViewerServer.app" of the startup disk
end tell
end if
end tell
on error
tell application "System Events"
if exists process "KeyboardViewerServer" then
set windList to every window of application process "KeyboardViewerServer"
if (count of windList) is 0 then
tell application "KeyboardViewerServer"
quit
end tell
end if
else
tell application "Finder"
open item "System:Library:Components:KeyboardViewer.component:Contents:SharedSupport:KeyboardViewerServer.app" of the startup disk
end tell
end if
end tell
end try



[ Reply to This | # ]
Restore the Key Caps application via AppleScript - updated for 10.4
Authored by: magnamous on Jul 06, '05 03:23:04PM
It seems that having the applescript tell System Events to quit the Keyboard Viewer is no longer necessary in 10.4. I was able to run the following script and close the Keyboard Viewer window several times over and over with no problem:


tell application "Finder"
	-- NOTE: Enter the following two lines as ONE LINE without spaces!
	open item "System:Library:Components:KeyboardViewer.component:Contents:SharedSupport:KeyboardViewerServer" of the startup disk
end tell


[ Reply to This | # ]