11

I am working on an Applescript to paste what was last copied to any current field. It is to be used with VoiceOver and the key code way (only way I know how) does not work all of the time.

    tell application "System Events" to key code 9 using command down

    say "paste"

3 Answers 3

22

I use keystroke:

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

I'm not aware of an error you should get, so you'll have to share.

2
  • 1
    Also, when using UI scripting to copy and past the clipboard, I put in delays to help keep the script from tripping over itself. delay .2
    – jweaks
    Aug 9, 2015 at 2:58
  • Note that this won't work if you are holding down other modifiers while this occurs. Any keys you are pressing are added to the modifiers used; e.g., if holding the Option key, then this will do "cmd-opt-v" which likely won't work. Nov 26, 2017 at 4:39
5

There is a direct way to access the clipboard via the the clipboard keyword:

tell application "System Events" to keystroke (the clipboard as text)

Reference:

1
tell application "myApp" to activate

tell application "System Events" to tell application process "myApp"
    click menu item "Paste" of menu "Edit" of menu bar item "Edit" of menu bar 1
end tell

Obviously, replace myApp with the name of your app.

Advantages:

  • Only targets a specific app (safer!)
  • Will work even if the user set a custom keyboard shortcut for Paste
  • Text is pasted instantly (unlike using keystroke).
0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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