I am trying to automate some keypresses every few minutes. The exact timing is still undetermined. But basically I want to capture a live chat dialogue. I'm sure I could do this more elegantly. I don't know much if anything about html or javascript or php etc.. I know a little bit of Bash and a little bit of AppleScript.

osascript -e 'tell app "Safari" to activate'
osascript -e 'set Script to "keystroke \"a\" using command down"'
osascript -e do Script

This would keep going and activate TextEdit and paste into there. But I haven't gotten past this point. This results in: syntax error: Can’t set script to "keystroke \"a\" using command down". Access not allowed. (-10003)

Changing Script to myScript and I get: keystroke "a" using command down.

But both methods still result in: execution error: The variable do is not defined. (-2753)

The fact that it calls "do" a variable makes my head spin.

I thought about using the expect program. I know I can run expect and say something like send \n to get a new line. However, that's about all I know. And I cannot find much documentation on how to send modifier keys like Cmd-A. It doesn't really matter whether expect or osascript as long as the end result works.

I would like to somehow get away from the select all step because it selects the entire webpage to be copied instead of just the chat box. Any help would be appreciated very much.

  • 2
    I don't think this is technically off-topic here, but you'll likely get better responses on OSX tools on Ask Different. :-) – Chris Down May 3 '16 at 22:23
  • Why don't you run the command directly, instead of setting it as a variable and calling it? – DisplayName May 3 '16 at 22:24
  • As the other commenter mentioned, you'll probably get better responses on Ask Different, the SE site for Apple stuff. But one specific tip: try the "Automator" tool that comes with OS X. It allows you to record events into a script. – RJHunter May 6 '16 at 5:05

Try this instead:

osascript -e 'tell Application "Safari" to activate'
osascript -e 'tell application "System Events"' -e 'keystroke "a" using command down' -e 'end tell'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.