Paste text with AppleScript without simulating cmd-v with System Events

applescriptcopy/paste

I would like to be able to use AppleScript to paste the clipboard's text into the foreground application. Every solution I can find online uses tell app "System Events" to keystroke "v" using command down. However, I am running this script with a keyboard shortcut, so I may already be holding down keys when this command fires, which makes the paste fail because more keys are pressed than just cmd and v. I know that I can use delay to give myself time to release the keys, but that feels like a hack (and ideally the paste would be instantaneous).

Is there a way to use AppleScript to paste without simulating the shortcut cmd-v? Thanks.

Edit: Also, if there is a way to use AppleScript to copy selected text without simulating cmd-c, that would be much appreciated as well.

Best Answer

I managed to find the following solution.

tell application "System Events" to tell process (name of current application)
    tell menu bar item "Edit" of menu bar 1
        click menu item "Paste" of menu 1
    end tell
end tell

delay 0.1

The delay is there to give the system time to copy before using the clipboard.