Editing the Clipboard
This lesson shows you how to use the clipboard by appending data to it, allowing the user to build up a piece of text and then paste it.
The UI for this example consists of a field containing some text, a button allowing the user to append the selected text to the clipboard and a label field informing the user how many characters are currently on the clipboard.
You can download the stack associated with this lesson from this url: https://tinyurl.com/y7cx4o8x
This handler is based on the clipboardData property. The clipboardData contains whatever is currently in the clipboard: plain text, styled text, image data, or files, stored in an array whose keys are the data types that the clipboard can hold. The appendSelectionToClipboard handler takes the selected text and adds it to the end of the text on the clipboard, letting you accumulate text, instead of wiping out what was already on the clipboard.
The first thing this handler does is check whether any text is currently selected. If the selectedText function returns empty, either there is no selection, or something other than text is selected. In either case, there's nothing to append to the clipboard, so the handler uses the return control structure to skip the rest of the handler and return an error message. The handler that called the appendSelectionToClipboard command can check the result function to see whether there was an error:
put the selectedText into tText if tText is empty then return "Error: No text" end if
If there is a text selection, the handler appends it to the end of the clipboard contents. It does this by setting the "text" element of the clipboardData array. The expression the clipboardData["text"] refers to the plain text on the clipboard; the handler sets this property to the current clipboard text and the selected text, separated by a return.
set the clipboardData["text"] to the clipboardData["text"] & return & tText
If you use the appendSelectionToClipboard handler repeatedly, the text accumulates, one entry per line. When you choose Edit -> Paste, all the accumulated text is pasted.
Finally the handler displays the number of characters stored on the clipboard.
put the length of the clipboardData["text"] && "characters in clipboard" into field "clipboard length"
This handler is placed on the script of the "Append Selected Text" button
on mouseUp appendToClipboard if the result is not empty then answer the result end if end mouseUp
This handler is placed on the Card script
on appendSelectionToClipboard local tText put the selectedText into tText if tText is empty then return "Error: No text" end if set the clipboardData["text"] to the clipboardData["text"] & return & tText put the length of the clipboardData["text"] && "characters in clipboard" into field "clipboard length" end appendSelectionToClipboard
To make this example handler more flexible, we might give it a parameter to hold the text to add to the clipboard. Instead of appending the selection, the handler would add whatever text was provided as the parameter, and fall back on the selection only if no text is supplied. Here's what the modified handler looks like:
on appendToClipboard pText if pText is empty then ## use the selected text put the selectedText into pText end if if pText is empty then return "Error: No text" end if set the clipboardData["text"] to the clipboardData["text"] & return & pText put the length of the clipboardData["text"] && "characters in clipboard" into field "clipboard length" end appendToClipboard
You use this modified handler with a statement like one of these:
appendToClipboard "Hello world" ## adds "Hello world"
appendToClipboard field "Name" ## adds the field contents
appendToClipboard ## adds whatever is selected
Another way to modify the original handler is to change other parts of the clipboardData array to allow clipping styled text. Because this handler specifies the clipboardData["text"], it's only capable of handling plain text with no styles. If the clipboard already contains styled text, setting the "text" element of the clipboardData causes the styles to be lost.
However, the clipboardData property also contains styled text. The expression the clipboardData["HTML"] evaluates to the styled text on the clipboard, in the same format as the htmlText property. We can use this element of the clipboardData array to set the clipboard to the selected text, while including any style information:
set the clipboardData["HTML"] to the clipboardData["HTML"] & "<p></p>" & the htmlText of the selectedChunk
The expression the htmlText of the selectedChunk evaluates to the selected text, along with its style information, in HTML format, so this variation includes the style information that goes with the selected text. The "<p></p>" is the htmlText equivalent of the return used in the original handler.