Using applescript to press keys

Hey guys i know this is simple but ive kinda forgotten :stuck_out_tongue:

how would i make a applescript that can press keys in the specfied app or is it possible to tell it to press a menu item

This activates TextEdit and presses the z key:


tell application "TextEdit" to activate
tell application "System Events" to keystroke "z"

Here’s how you’d launch commands (in this case, select all):

tell application "System Events" to keystroke "a" using command down

so what do i do if i want to do cmd+shift+a i tried this with no luck

tell application “MultiAlarm” to activate
tell application “System Events” to keystroke “a” using command down and shift down

You’re close. Try:

tell application "MultiAlarm" to activate
        tell application "System Events" to keystroke "a" using command down & shift down

This syntax should also work


tell application "System Events" to keystroke "a" using {command down, shift down}

tell application "System Events" to keystroke "q" using {command down, shift down}

how would i make the script press the log out button?

cheers

tell application "System Events" to keystroke return

That should do the trick, I think

Chris

You don’t really need to use keystrokes to invoke a logout, dgorjup31 - since System Events’ Power Suite allows you to do this directly. (It’s generally a good idea, wherever possible, to use GUI scripting only as a last resort.)

It’s probably also worth making sure the logout dialog has appeared before attempting to confirm the action. So here’s another way to do the job:

tell application "System Events"
	log out
	tell button "Log Out" of window 1 of process "loginwindow"
		repeat until exists
			delay 0.1
		end repeat
		click
	end tell
end tell