This is UltimateRPA Documentation
Clipboard

Working with the Windows clipboard

The robot can communicate with the Windows clipboard by simulating the keyboard shortcut (CTRL + C) over the marked text via the method send_key or copy text directly to the clipboard using the method set_clipboard_text. The method send_key simulates how the user copies text to the clipboard using CTRL + C keyboard shortcut. To access the clipboard content use the method clipboard_text. Within the robot script to paste the text on a GUI element from the clipboard use the method send_key to simulate the keyboard shortcut CTRL + V.

The clipboard can then act as an intermediary if the text in the GUI element is not stored in any of its internal variables (Name, Value, Control Type, ...). This is particularly useful if you need to verify that the robot has communicated successfully with the application and the values have been entered in the element correctly.

Pasting text via the clipboard is also recommended in applications or GUI elements with autocomplete (e.g., various search engines), which could occasionally confuse the robot and result in poor text transcription.

As both the user and certain applications have access to the clipboard, it is a good idea to try to minimize its usage time (the time between putting a value in the clipboard and retrieving a value from the clipboard). This prevents the unwanted overwriting of a value stored in the clipboard by a user or another application.

Determining a value in a GUI element via the clipboard

We demonstrate the advantages of using the clipboard using the application RpaFormsTest. To search for GUI elements in RpaFormsTest, use Inspector tool and use the function exec_app to run RpaFormsTest. This example demonstrates how to fetch and test the text from a GUI element using clipboard.

RpaFormsTest.png
Figure 1 RpaFormsTest application
import urpa
def main():
app = urpa.exec_app("TutorialApps/RpaFormsTest.exe")

The path to an app "TutorialApps/RpaFormsTest.exe" is relative to the script file directory. The above code assumes the script is placed in the main directory of UltimateRPA tools. If your script is saved elsewhere, remember to change the path.

Using the method find_first_right_to, find the edit box to the right of the text object "ID". In the edit box, enter the word "test" using the method send_text.

Clipboard_editbox_highlighted.png
Figure 2 Highlighted editbox ID
Clipboard_text_sent.png
Figure 3 Text written by the robot to the editbox ID
import urpa
def main():
app = urpa.exec_app("TutorialApps/RpaFormsTest.exe")
id_box = app.find_first_right_to(cf.text().name("ID"), cf.custom())
id_box.send_text("test")

As you are unable to determine the value entered in the object from the internal values value and name of the GUI element, this is checked by copying the content to the clipboard. First, simulate the keyboard shortcut (CTRL + A) using the method send_key to select the cell content and then simulate the keyboard shortcut (CTRL + C) using the same method.

Clipboard_text_selected.png
Figure 4 Text selected by robot
import urpa
def main():
app = urpa.exec_app("TutorialApps/RpaFormsTest.exe")
id_box = app.find_first_right_to(cf.text().name("ID"), cf.custom())
id_box.send_text("test")
id_box.send_key("CTRL+a")
id_box.send_key("CTRL+c")

In the script, access the text in the clipboard via the function clipboard_text, which returns the text from the clipboard. Store this in a variable and print it to the console.

import urpa
def main():
app = urpa.exec_app("TutorialApps/RpaFormsTest.exe")
id_box = app.find_first_right_to(cf.text().name("ID"), cf.custom())
id_box.send_text("test")
id_box.send_key("CTRL+a")
id_box.send_key("CTRL+c")
print(text)

Run the script at the end by pressing the keyboard shortcut (ALT + F9) in PyScripter.

The robot can then only work with the text in the clipboard. If different content (an image or file) is stored in the clipboard, invoking the function clipboard_text returns an empty string and the robot makes the log entry [ERROR] Event: #, Clipboard: text not available. However, this error does not stop the robot from running.

Pasting a value into a GUI element with the clipboard

The robot, much like a user, enters values by simulating the keyboard shortcut (CTRL + V), via the method send_key. We demonstrate this usage on work with RpaFormsTest. First, run PyScripter and prepare a new script.

import urpa
def main():
app = urpa.exec_app("TutorialApps/RpaFormsTest.exe")

Using the command find_first_right_to, find the edit box to the right of the text object "ID".

import urpa
def main():
app = urpa.exec_app("TutorialApps/RpaFormsTest.exe")
id_box = app.find_first_right_to(cf.text().name("ID"), cf.custom())

To paste text into the selected field with the clipboard, first copy the text required on the clipboard. Do this by invoking the function set_clipboard_text, which takes as its argument the string, which is copied to the clipboard.

import urpa
def main():
app = urpa.exec_app("TutorialApps/RpaFormsTest.exe")
id_box = app.find_first_right_to(cf.text().name("ID"), cf.custom())

To paste text from the clipboard to the edit box, invoke the method send_key to simulate the keyboard shortcut (CTRL + V).

import urpa
def main():
app = urpa.exec_app("TutorialApps/RpaFormsTest.exe")
id_box = app.find_first_right_to(cf.text().name("ID"), cf.custom())
id_box.send_key("CTRL+v")

Run the script in PyScripter with the keyboard shortcut (ALT + F9). After pasting, it is advisable to check that you have inserted the text correctly.