Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can one invoke a keyboard shortcut from within an AppleScript?

Tags:

applescript

I need to invoke a keyboard shortcut from within an AppleScript code, e.g. Cmd+Ctrl+Opt+E.

like image 421
Barton Avatar asked Sep 11 '10 07:09

Barton


People also ask

Which key activates keyboard shortcuts?

In others, pressing the Alt or F10 keys shows available keyboard shortcuts. If a letter is underlined in a menu, press the Altkey and the underlined key together instead of choosing that menu item.

What is the shortcut key used to invoke the Run menu?

First things first, the most efficient way to call up the Run command dialog box is to use this keyboard shortcut combination: Windows key + R.

How do I enable shortcuts in archicad?

To do so, go to Options> Work Environment> Keyboard shortcuts. Then search and choose the command you want to assign a shortcut to and type in your keyboard the preferred shortcut.


2 Answers

Sure it works. System events can perform keystrokes. However, keystrokes are always sent to the frontmost application so to perform a shortcut for an application you must tell that app to activate first and then perform the shortcut. For example, I can open a new tab in Safari using command-t. That applescript would look like this...

tell application "Safari" to activate tell application "System Events"     keystroke "t" using command down end tell 

Now suppose you have a global keyboard shortcut. Global meaning it works from any application. Then you don't even need to activate an application first, just perform the keystroke. To press the keys you requested do this...

tell application "System Events"     keystroke "e" using {command down, option down, control down} end tell 
like image 50
regulus6633 Avatar answered Sep 28 '22 04:09

regulus6633


You can invoke the keystroke, or if GUI Scripting is on, you can select a menu item from a menu.

Here's a great link explaining this in detail.

http://hints.macworld.com/article.php?story=20060921045743404

like image 34
Alex Zavatone Avatar answered Sep 28 '22 05:09

Alex Zavatone