Lion: temporarily change a keyboard shortcut via Applescript

In Lion, I’d like to use Applescript to temporarily change a keyboard shortcut (as if I did so via System Preferences->Keyboard->Keyboard Shortcuts), do some processing, and then change the keyboard shortcut back to its original value.

I already know how to read ~/Library/Preferences/com.apple.symbolichotkeys.plist in order to find out the current values of all my keyboard shortcuts, but I haven’t been able to find a way to programmatically change these shortcut settings during the course of a running Applescript program.

In other words, within a single running Applescript program, I want to do this:

  1. query the current value of a particular function’s keyboard shortcut and store it in a variable
  2. set that function’s keyboard shortcut to a new value
  3. do some processiong
  4. reset that function’s keyboard shortcut to its originally stored value

I know how to perform steps 1 and 3. It’s steps 2 and 4 that I’m trying to figure out.

If this is easier via some sort of Cocoa procedure under ASOC, I’m glad to do it that way.

Thanks in advance for any suggestions or pointers to docs.

I don’t understand the need for such a feature.
In your script, trigger the menu item thru GUIScripting which would be required to issue a shortcut.



--=====
(*
my selectMenu("Pages",5, 12)
==== Uses GUIscripting ====
*)
on selectmenu(theApp, mt, mi)
	tell application theApp to activate
tell application "System Events" to tell process theApp to tell menu bar 1 to ¬
			tell menu bar item mt to tell menu 1 to click menu item mi
end selectmenu

--=====
(*
my selectSubMenu("Pages",6, 4, 26)
==== Uses GUIscripting ====
*)
on selectSubMenu(theApp, mt, mi, ms)
	
	tell application theApp to activate
		tell application "System Events" to tell process theApp to tell menu bar 1 to ¬
			tell menu bar item mt to tell menu 1 to tell menu item mi to tell menu 1 to click menu item ms
end selectSubMenu

--=====

Yvan KOENIG (VALLAURIS, France) dimanche 11 décembre 2011 21:35:09

Thank you very much.

However, I think I do need to set a hotkey via a program. Here’s why I believe that I need to do this:

In Lion, I want to control Mission Control via a program. However, it is difficult to do that. Some of the Cocoa calls that worked under Snow Leopard to control Spaces have been removed or changed in Lion.

Under Lion, there is an API that can be utilized to programmatically switch to any space except the first (main) space. The only method that I have been able to find for switching to that first space in Lion is to assign a hotkey to the function for switching to the first space, and then programmatically send a system event which simulates that hotkey press.

Given this limitation and the fact that I want to write a program that can switch to any space, I have to pre-define a hotkey to switch to the first space. However, I want my program to be generic and not to require any presetting of hotkeys, so this is what I want to do when programmatically switching to the first space:

  1. Determine if there already is a hotkey set up for switching to the first space. If so, just send a system event which simulates that hotkey press (I know how to do this already).
  2. If there is no such hotkey, traverse the entire list of already defined hotkeys and figure out a hotkey sequence that currently isn’t in use (I know how to do this already).
  3. Programmatically set that hotkey to switch to the first space (I do not know how to do this).
  4. Send a system event which simulates that hotkey press (I know how to do this already). This should switch me to the first space.
  5. Programmatically unset that hotkey from switching to the first space (I do not know how to do this).

Again, for spaces 2 through N, this isn’t necessary, because there’s an API for switching to those spaces. But for the first space, that API doesn’t work, and a simulated hotkey event seems to be the only way to cause a programmatic switch to that first space.

This is why I am looking for the functionality I described above.

Given what I saw, when a shortcut is dedicated to an app, it’s defined by an entry in the app’s preferences file.
Maybe it’s a track to study.

Yvan KOENIG (VALLAURIS, France) lundi 12 décembre 2011 10:56:50

Thank you.

In this case, the hotkey mappings are defined in ~/Library/Preferences/com.apple.symbolichotkeys.plist. That file is in binary format. I’ve tried to change its contents directly (i.e., not via System Preferences->Keyboard->Keyboard Shortcuts), and so far, that doesn’t seem to cause the changes to go into effect during the course of the Applescript that is running.

In other words, unless I’m doing something wrong, it seems like I can’t programmatically change that file’s contents during the course of the running of my Applescript program and cause a system event that simulates the keypress that I changed to be recognized.

It looks like I have to change the keypress-to-function mapping at a lower level (i.e., not simply via a change to the System Preferences->Keyboard->Keyboard Shortcuts file), but I don’t know how to do this, or whether this is even possible.