Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send a key code to an application without activating it first?

Tags:

applescript

I don't think you can send a keystroke to an inactive application, but you can hide an app immediately after activating it and executing code for it. This does however, cause the app to briefly flash before it hides.

tell application "System Events"
    tell application "X" to activate
    key code 49
    set visible of process "X" to false
end tell

Sending a keystroke can basically be seen as using a keyboard, but the only difference is that the keys that need to be pressed are already predefined. The rest of the process revolving around this doesn't change. This means that the application itself still needs to be opened and activated before you can actually send keystrokes to it.

Depending on the application however, it might be possible to use certain Applescript functions in the application's API to send different inputs to the application without having to activate it first. Take the Messages API for instance:

tell application "Messages"
      set theBuddy to buddy "[email protected]" of service "iMessage"
      send "Hi there" to theBuddy
end tell