How to one invoke a keyboard shortcut from within an AppleScript

applescriptkeyboard

I need to invoke a keyboard shortcut from within an AppleScript code, later to be used as a startup item (i.e. the alternative to having to press something at every log in).

Best Answer

I get the impression that what you're trying to do is a copy command (i.e., ⌘C)—am I right?

Here's an example from one of my scripts:

tell application "System Events"
    tell application "Microsoft Entourage" to activate
    tell menu "Edit" of menu bar item "Edit" of menu bar 1 of process "Microsoft Entourage"
        click menu item "Select All"
        click menu item "Copy"
    end tell
    key code 123
end tell

That is, I script the menu command instead of scripting shortcuts.

You might also take a look and see if you have /Library/Scripts/UI Element Scripts/Key Down-Up.applescript, which should give you alternate ideas of how to emulate keyboard actions.

And of course, there are a lot of examples in /Library/Scripts/ and ~/Library/Scripts/, many of which are worth a look in general.