13.3. Using the clipboard

You can use the clipboard to transfer data between your programs by using some Standard Additions commands that work with the clipboard. To access the contents of the clipboard in your program, write the following:

the clipboard

Here's what the clipboard contains after I copy this section's title from my Word file to the clipboard:

"Using the Clipboard"

Here's what the clipboard contains if I copy an image from Safari to the clipboard:

«data
PICT00000000000000950073001102FF0C00FFFE00000048000000480000000000000095007300000000001E
    ...
»

You can also copy something from your AppleScript program to the clipboard by using the set the clipboard to command, like so:

set the clipboard to "This is going onto the clipboard."

You can even tell some applications to paste whatever's on the clipboard at the current selection point in the active document, like so:

tell application "Microsoft Word"
    activate
    paste
end tell

This works because Word has a paste command. It's an easy way for you to get some data from AppleScript into your document. You can also do it like this:

tell application "Microsoft Word"
    activate
    set selection to the clipboard
end tell

You can have Word copy the current selection as a string onto the clipboard like so:

tell application "Microsoft Word"
    set the clipboard to the selection as string
end tell

If you don't specify the as string part, you get a reference to an object in Word instead. Also, in general, many applications put styled text onto ...

Get Beginning AppleScript® now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.