Copy text into clipboard

In linux, anyone knows how to write data into clipboard memory ( like Ctrl + C)

I am try to read the text in a scrollField, and copy to clipboard ,
Instead of selecting text Ctrl + C, I like to make it a button.

Since you’re mentioning scrollField I think you’re already using Qt? If so, this code might be a good example for you.

clipboard = QtWidgets.QApplication.clipboard()
assert clipboard, "Must have running QApplication instance"
# Set to Clipboard
clipboard.setText(text)

If instead you’d like to actually put a file in your clipboard so you can paste it in your system’s explorer or a browser, etc. then this example code will help you. Which boils down to:

clipboard = QtWidgets.QApplication.clipboard()
assert clipboard, "Must have running QApplication instance"

# Build mime data for clipboard
data = QtCore.QMimeData()
url = QtCore.QUrl.fromLocalFile(path)
data.setUrls([url])

# Set to Clipboard
clipboard.setMimeData(data)

This should both work across different platforms, including linux.

thanks for the reply, I will give a try :slight_smile:

To correct myself, scrollField of course is not a Qt command but a Maya UI element from maya.cmds.

Anyway, in Maya you should still be able to access the Qt functions from PySide2. You can import those with:

from PySide2 import QtWidgets

It’s good to be aware that over the years Maya has had different versions of PySide and will likely upgrade with the next version to PySide6. So you might need to ensure you do the right imports in the right maya versions or use a library like Qt.py or qtpy or alike which tries to abstract away the different versions into a single library. But I believe Maya 2020-2024 at least were all using PySide2.

NB:

Unlike the cmdScrollFieldReporter command,
the ScrollFiel command does not have flags for selecting text and copying it to the clipboard.
The ScrollFiel command has flags for some simple manipulations with text content and its formatting.
For example, the command has flags to return all text or a selected part of it.

# work with maya 2019-24
import maya.cmds as cmds
import maya.mel as mel
import PySide2

# Get Maya window name:
maya_window_name = mel.eval('$tmpVar = $gMainWindow;')
my_window_name = 'TEST_WINDOWS'
# We check that windows and prefs with the same name do not exist:
if cmds.window(my_window_name, exists = True):
    cmds.deleteUI(my_window_name)
if cmds.windowPref(my_window_name, exists = True):
    cmds.windowPref(my_window_name, enableAll = False, remove = True)
# Create windows with Scrol Field on Form Layout:
my_window = cmds.window(my_window_name,
                        title = 'Test window',
                        resizeToFitChildren = True,
                        parent = maya_window_name)
my_form_layout_name = 'TEST_FORM_LAYOUT'
my_form_layout = cmds.formLayout(my_form_layout_name,
                                 numberOfDivisions = 100,
                                 parent = my_window)
my_scrol_field_name = 'TEST_SCROL_FIELD'
my_scrol_field = cmds.scrollField(my_scrol_field_name,
                                  parent = my_form_layout)
cmds.formLayout(my_form_layout,
                edit = True,
                attachForm = [
                              (my_scrol_field, 'left', 5),
                              (my_scrol_field, 'right', 5),
                              (my_scrol_field, 'top', 5),
                              (my_scrol_field, 'bottom', 5)
                             ])
# visible my_window:
# cmds.window(my_window_name, edit = True, visible = True) or:
cmds.showWindow(my_window)

# Manipulations with text data:

# Create text in field:
my_example_text_01 = 'Example text 01\nExample text 02\nExample text 03\nExample text 04\n'
cmds.scrollField(my_scrol_field_name,
                 edit = True,
                 text = my_example_text_01)
# Get all text data from Scroll Field:
all_text_from_scrol_field = cmds.scrollField(my_scrol_field_name,
                                             query = True,
                                             text = True)
# Result: 'Example text 01\nExample text 02\nExample text 03\nExample text 04\n'

# Get selected text data from Scroll Field:
#  For example selected line 3 in field.
selected_text_from_scrol_field = cmds.scrollField(my_scrol_field_name,
                                                  query = True,
                                                  selection = True)
# Result: 'Example text 03'

# Deselect text and set insertion position,
# 1 - based value for start of the field position (0 - end of the field)
cmds.scrollField(my_scrol_field_name,
                 edit = True,
                 insertionPosition = 0)
# Insert text into the field at the current position
# (or position set with flag -insertionPosition):
my_example_text_02 = 'Insert Text 01\n'
cmds.scrollField(my_scrol_field_name,
                 edit = True,
                 insertionPosition = 17,
                 insertText = my_example_text_02)

# Clipboard with PySide:

# Get clipboard:
clipboard = PySide2.QtWidgets.QApplication.clipboard()
# Clear clipboard:
clipboard.clear(mode = clipboard.Clipboard)
# Set clipboard data:
clipboard.setText(all_text_from_scrol_field)
# Get clipboard data:
clipboard_data = clipboard.text()
# Clear clipboard:
clipboard.clear(mode = clipboard.Clipboard)
# Write clipboard data to file:
my_output_parh = r'C:\Temp\my_log.txt'
with open(my_output_parh, 'wb') as wf:
    wf.write(clipboard_data.encode('utf-8'))

command (Python) scrollField

1 Like

Thanks for the help :slight_smile: