REALBasic’s Built-In Clipboard Classes
Table of Contents
Getting Data from the Clipboard
Its pretty simple to create a class to handle the contents of the clipboard. To start, lets create a class to contain the contents of the clipboard. For the example, I’m going to use a class named “CBContent”.
CBContent will need two properties; text as string, and pict as picture.
CbContent will need one method. For the sake of this example, I am going to call it “currentize”. This method will make the two properties equal the contents of the clipboard.
dim mycb as new Clipboard
if mycb.PictureAvailable then // Is there a picture on the clipboard
pict = mycp.Picture // There is, so set the pict property to the image.
elseif mycb.TextAvailable then // If not, is there text?
text = mycb.Text // There is, so set the text property to the text.
end if
A common question as this point is “If there is not a picture, why do I need to check for text?”. The answer is simple; The clipboard can contain something different then a picture or text. For example, in Apple’s Keynote, graphs may be copied to the clipboard.
The last step in creating our clipboard classes is a CB class, which will also hold the date and time that the clipboard is from. For the sake of this example, I am calling it “CB”.
CB will need 1 method. I’m calling it “currentize”.
CB will need 2 properties. contents as CBContents and time as date.
In the currentize method:
time = new date // Make the date object current
contents = new CBContents // Make contents a new CBContents; reset
contents.currentize // Currentize / Update the contents
Now you can use your CB class to know the contents of the clipboard, and when the clipboard was in that state.
Although I would recommend the class, you can also pull the clipboard data from a push button and just place it into an editfield... Here is the code you would place into a pushbutton:
dim mycb as new Clipboard
if mycb.TextAvailable then
editfield1.text = mycb.text
end if
If your putting the same code in a timer, use this to avoid flicker:
dim mycb as new Clipboard
if mycb.TextAvailable then
if mycb.text <> editfield1.text then
editfield1.text = mycb.text
end if
end if
Just to restate, I highly recommend using classes.