Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Noah212

macrumors newbie
Original poster
Nov 16, 2010
5
0
I was wondering if it was possible to execute keyboard shortcuts by using terminal. Thanks.
 
Let me clarify... Is it possible to run a keyboard shortcut, not by actually pressing the keys, but by entering a certain command into terminal (which would complete the task that the keyboard shortcut would have completed)?

For example... Let's say I wanted to be able to shut down my computer via a terminal command. Given that ^⌥⌘ + eject will shut down the computer, would it be possible to somehow enter this command into terminal however, not with the keyboard shortcut, but by typing something (My question is what is this something?)?

Let me know if you are still having trouble understanding my question. Thanks.
 
For example... Let's say I wanted to be able to shut down my computer via a terminal command. Given that ^⌥⌘ + eject will shut down the computer, would it be possible to somehow enter this command into terminal however, not with the keyboard shortcut, but by typing something (My question is what is this something?)?

To shutdown from Terminal, enter

Code:
sudo shutdown -h now

To reboot from Terminal, enter

Code:
sudo shutdown -r now
 
Offhand the only thing that comes to mind is using the osascript command to run some AppleScript code, which can execute key events. This page has some examples of using AppleScript to issue key events.

Thanks, I'll look into that.

To shutdown from Terminal, enter

Code:
sudo shutdown -h now

To reboot from Terminal, enter

Code:
sudo shutdown -r now

I appreciate your contribution to the discussion however, I'm looking for a terminal command which doesn't prompt a password.
 
I appreciate your contribution to the discussion however, I'm looking for a terminal command which doesn't prompt a password.

You can include your password in the command:

Code:
echo 'yourpasswordhere' | sudo -S shutdown -r now
 
In general, you should be looking for ways to achieve the same result as hitting a particular keyboard shortcut, not to actually make the computer think you hit that shortcut. You'll be more likely to find the correct solution (like with the shutdown command from a couple posts ago).

jW
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.