I'm very new to AppleScript and I'm trying to do this. I have an Safari process running on my machine. I wish I could go to the running safari and stimulate an enter key event.

Is it possible to do so?

The code I'm trying is:

on is_running(appName)
    tell application "System Events" to (name of processes) contains appName
end is_running

set safRunning to is_running("Safari")
if safRunning then
    run script "tell application \"Safari\" 
         key code 36
    end tell"
    return "Running"
    return "Not running"
end if

Inside the run script the key code 36 (for stimulating enter) is giving me error:

error "Expected end of line but found identifier." number -2741

Any guesses where I'm making mistake?


The "System Events" must to the key pressing.

for example:

tell application "Safari"
    tell application "System Events" to key code 36 #return
end tell

I don't know, what the key pressing is for, so now has no effect.

A working example, where the download list is shown and then by pressing "space" the list is cleared:

tell application "Safari"
    tell application "System Events" to keystroke "l" using {command down, option down} # cmd-opt-l
    delay 1
    tell application "System Events" to key code 49 #space
end tell

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .