Gui script to hit return

This is basic but I can’t get it ti work

I am running Yosemite and have Privacy>Accessibility>Script Editor enabled

I just can’t get the OK button clicked!

I have tried different commands and put the code in script editor and finder tell boxes using System Events

All I want to do is

tell application “System Events”
display dialog “hallo”

(* click button "OK" of window 1
keystroke return
click (keystroke return)

end tell

Could someone just point me in the right direction please

Hi there,

Does ‘giving up after…’ not do what you’re looking for?

tell application "System Events"
	display dialog "hallo" giving up after 2
	(* click button "OK" of window 1
    keystroke return
    click (keystroke return)
end tell

Hi Tecknic

I know that works but I am going to use the GUI for some thing else with apps with other dialogs that I need clicked

This is just a test, but if I can’t GUI script this I haven’t a chance on the other stuff

Hi there,

This works with InDesign:

tell application "Adobe InDesign CC 2015" to activate

tell application "System Events"
	tell application process "Adobe InDesign CC 2015"
		keystroke return
	end tell
end tell


Thanks. But that works for you why does not this work?

tell application "Script Editor" to activate

tell application "System Events"
	tell application process "Script Editor"
		display dialog "hallo"
		keystroke return
	end tell
end tell

Good question :confused:

If you’re going to be using it with other apps shouldn’t you be targeting those as I did with InDesign?

Thats what I am doing with script editor. I have also tried with the Finder but nothing. I’m confused

If I run your script in Applescript Editor the dialog appears.
If I then use Script Debugger to execute the System Events part, that hits the return key, the dialog generated by Applescript Editor is cancelled and it disappears.

This is a bit of a guess but I’m wondering if you can’t target the AppleScript Editor like that.
I’m sure someone will correct me if I’m wrong :wink:

It cannot work because display dialog works modally,
that means the execution of the next line is suspended
until display dialog receives the button pressed or gave up timeout event

So you can’t script a button pressed with GUI script in a display dialog?

One of the things I am trying to do is action dialog boxes when importing into Calendar from an ics file. So i need to GUI script that. So something like this would work.

tell system events
tell calendarfilehandler
keystroke return
end tell
end tell


. although this still works. Apologies for posting it again. :wink:

on showError(msg)
	set b to {"¢", ""}
	repeat 6 times
		do shell script "osascript -e 'say \"Error\" using \"Zarvox\"' -e 'tell application \"System Events\" to keystroke return' > /dev/null 2>&1 & "
		display dialog msg buttons b default button "" with title "OH NO! WE'RE ALL GOING TO DIE!" cancel button "¢" with icon caution
		delay 0.1
		set b to reverse of b
	end repeat
	say "Ah ha ha" using "Hysterical"
	error number -128
end showError

	set fire to my trousers
on error msg
end try

That’s indeed the funniest script I’ve ever run

Perhaps you could post the actual script you are working with.

It is not clear why you need to “click” a button on a display dialog that you have created.

Seems like better choices are:
(1) display notification
(2) display dialog with desired default button, using “giving up after xxx”

What the script will do is open a .ics file in an email. When an ics is actioned the CalendarFile Helper creates a dialog titled Add Event.

Simply want a script that will click the “OK” button but I cant get anything so far to work

AppleScript: 2.2.1
Browser: Safari 523.15
Operating System: Mac OS X (10.10)