# How to script Copy Paste Clipboard right mouse click

**URL:** <https://forum.inductiveautomation.com/t/how-to-script-copy-paste-clipboard-right-mouse-click/10277>\
**Category:** Ignition\
**Created:** [July 25, 2015, 2:44pm UTC](https://forum.inductiveautomation.com/t/how-to-script-copy-paste-clipboard-right-mouse-click/10277 "2015-07-25T14:44:03Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![framebuilder](https://avatars.discourse-cdn.com/v4/letter/f/96bed5/32.png) [@framebuilder](https://forum.inductiveautomation.com/u/framebuilder)\
**Post date:** [July 25, 2015, 2:44pm UTC](https://forum.inductiveautomation.com/t/how-to-script-copy-paste-clipboard-right-mouse-click/10277/1 "2015-07-25T14:44:03Z")

</div>

What is the best way to allow users to copy and paste to and from Ignition client text fields? The keyboard shortcuts Ctrl+C and Ctrl+V work fine but some people like using a mouse 😒 .

I found this code from Carl in the forums but it has a select-all effect after the script executes on mouseReleased and creates some issues with overwriting.

What do you guys use?

[code]def doCut(event):  
from java.awt.event import ActionEvent  
action = event.source.actionMap.get(“cut”)  
action.actionPerformed(ActionEvent(event.source, ActionEvent.ACTION\_PERFORMED, action.getValue(action.ACTION\_COMMAND\_KEY)))

def doCopy(event):  
from java.awt.event import ActionEvent  
action = event.source.actionMap.get(“copy”)  
action.actionPerformed(ActionEvent(event.source, ActionEvent.ACTION\_PERFORMED, action.getValue(action.ACTION\_COMMAND\_KEY)))

def doPaste(event):  
from java.awt.event import ActionEvent  
action = event.source.actionMap.get(“paste”)  
action.actionPerformed(ActionEvent(event.source, ActionEvent.ACTION\_PERFORMED, action.getValue(action.ACTION\_COMMAND\_KEY)))

menu = fpmi.gui.createPopupMenu([“Cut”,“Copy”,“Paste”], [doCut, doCopy,doPaste])

menu.show(event)  
[/code]

---

<div class="post-metadata">

**Author:** ![JGJohnson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jgjohnson/32/29582_2.png) [@JGJohnson](https://forum.inductiveautomation.com/u/JGJohnson)\
**Post date:** [July 27, 2015, 12:57pm UTC](https://forum.inductiveautomation.com/t/how-to-script-copy-paste-clipboard-right-mouse-click/10277/2 "2015-07-27T12:57:31Z")

</div>

I don’t know that it’s the best way but we have several components that can be double-clicked to copy their values to the clipboard. To let the user know that the double-click was successful I briefly change the cursor to the “busy” cursor and then back again. That script is on a timer on each page that has double-click copy enabled.

This is the script in the change cursor timer.

```auto
event.source.running = 0
c_code = event.source.parent.cursorCode
event.source.parent.cursorCode = 3

def delay(event=event,c_code=c_code):
	import time
	import system
	time.sleep(.5)
	
	def finished(event=event):
		event.source.parent.cursorCode = c_code
	
	system.util.invokeLater(finished)

system.util.invokeAsynchronous(delay)
```

---

<div class="post-metadata">

**Author:** ![JGJohnson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jgjohnson/32/29582_2.png) [@JGJohnson](https://forum.inductiveautomation.com/u/JGJohnson)\
**Post date:** [July 27, 2015, 1:08pm UTC](https://forum.inductiveautomation.com/t/how-to-script-copy-paste-clipboard-right-mouse-click/10277/3 "2015-07-27T13:08:29Z")

</div>

The script that we use for the actual copy to clipboard is:

```auto
from java.awt import Toolkit
from java.awt.datatransfer import StringSelection
	
clipboard = Toolkit.getDefaultToolkit().getSystemClipboard()
clipboard.setContents(StringSelection(text_to_copy), None)

```

The script is inside a function that takes the text of the component (text\_to\_copy) as a parameter.

---

<div class="post-metadata">

**Author:** ![framebuilder](https://avatars.discourse-cdn.com/v4/letter/f/96bed5/32.png) [@framebuilder](https://forum.inductiveautomation.com/u/framebuilder)\
**Post date:** [July 27, 2015, 1:20pm UTC](https://forum.inductiveautomation.com/t/how-to-script-copy-paste-clipboard-right-mouse-click/10277/4 "2015-07-27T13:20:26Z")

</div>

That’s a cool idea with the double-click copy. For our particular Project, our users will be copying data from an application outside of Ignition and pasting it into the Ignition Project.

Do you do any pasting into Ignition with your projects after the double-click copy?

---

<div class="post-metadata">

**Author:** ![JGJohnson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jgjohnson/32/29582_2.png) [@JGJohnson](https://forum.inductiveautomation.com/u/JGJohnson)\
**Post date:** [July 27, 2015, 2:51pm UTC](https://forum.inductiveautomation.com/t/how-to-script-copy-paste-clipboard-right-mouse-click/10277/5 "2015-07-27T14:51:55Z")

</div>

There is only one place in the project that data can be pasted in. For that I am using a TextArea to accept text in a specific csv format that gets parsed and put into a table so the user can verify the import and edit any data as needed before the data is processed.

Can you export the data from your other application into files and then use file functions to read the data in and parse it?

I’m not understanding what issues you are experiencing when people paste data into your text fields.

---

<div class="post-metadata">

**Author:** ![framebuilder](https://avatars.discourse-cdn.com/v4/letter/f/96bed5/32.png) [@framebuilder](https://forum.inductiveautomation.com/u/framebuilder)\
**Post date:** [July 27, 2015, 11:07pm UTC](https://forum.inductiveautomation.com/t/how-to-script-copy-paste-clipboard-right-mouse-click/10277/6 "2015-07-27T23:07:43Z")

</div>

Basically, our Ignition Project requires users to type “Comments” into a Text Field and another into a Text Area.

Most of the time, they’ll grab these random strings of comments from an email (Microsoft Outlook) or from a non-Ignition HTML web page and want to paste them into the Ignition Text Field and/or Text Area instead of re-typing them.

Ctrl+C, Ctrl+V works fine but they insist on using right-mouse clicks. The problem is that you cannot right-click and paste into Ignition projects like you can with all other Microsoft-based applications. Actually, you can…but you need to build the functionality into your Project manually with Java. And I’m not a great Java developer.

My first code snippet above from Carl works with Text Fields (even though it has a weird select-all effect which we can live with). However, we just found out that it does NOT work with Text Areas…ugh. I understand Java scripting is super powerful but sometimes it’s frustrating when I need what seems to be a simple solution on the surface.

---

<div class="post-metadata">

**Author:** ![JGJohnson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jgjohnson/32/29582_2.png) [@JGJohnson](https://forum.inductiveautomation.com/u/JGJohnson)\
**Post date:** [July 28, 2015, 1:08pm UTC](https://forum.inductiveautomation.com/t/how-to-script-copy-paste-clipboard-right-mouse-click/10277/7 "2015-07-28T13:08:17Z")

</div>

I am by no means a competent Java developer but I was able to get the following script to work with text areas.

```auto
from java.awt import Toolkit
from java.awt.datatransfer import DataFlavor
   
clipboard = Toolkit.getDefaultToolkit().getSystemClipboard()
contents = clipboard.getContents(None)
if contents is not None and contents.isDataFlavorSupported(DataFlavor.stringFlavor):
	text = contents.getTransferData(DataFlavor.stringFlavor) 
	event.source.parent.getComponent('Text Area').text = text

```

You could probably expand on this and check on a right click event to see if there is string data on the clipboard and if not then disable the Paste menu item.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [July 28, 2015, 7:02pm UTC](https://forum.inductiveautomation.com/t/how-to-script-copy-paste-clipboard-right-mouse-click/10277/8 "2015-07-28T19:02:12Z")

</div>

[quote=“JGJohnson”]I am by no means a competent Java developer …[/quote]I’m not so sure about that 🙂  
Anyways, tickled my “that’s interesting!” neurons and got me to extend your suggestion as this shared script:[code]def textPopup(event):  
if event.popupTrigger:  
menunames = []  
menufuncs = []  
if event.source.selectionStart != event.source.selectionEnd:  
menunames.append(“cut”)  
def cuttext(ev = event):  
ev.source.cut()  
menufuncs.append(cuttext)  
menunames.append(“copy”)  
def copytext(ev = event):  
ev.source.copy()  
menufuncs.append(copytext)

```
from java.awt import Toolkit
from java.awt.datatransfer import DataFlavor

clipboard = Toolkit.getDefaultToolkit().getSystemClipboard()
contents = clipboard.getContents(None)
if contents is not None and contents.isDataFlavorSupported(DataFlavor.stringFlavor):
  menunames.append("paste")
  def pastetext(ev = event):
    ev.source.paste()
  menufuncs.append(pastetext)
if len(menunames)>0:
  menu = system.gui.createPopupMenu(menunames, menufuncs)
  menu.show(event)[/code]... and then put the following in each component's mousePressed() and mouseReleased() events:[code]shared.cliphelp.textPopup(event)[/code]Applying the built-in cut(), copy(), and paste() methods like this might be enough for you.
```

---

<div class="post-metadata">

**Author:** ![framebuilder](https://avatars.discourse-cdn.com/v4/letter/f/96bed5/32.png) [@framebuilder](https://forum.inductiveautomation.com/u/framebuilder)\
**Post date:** [August 18, 2015, 3:59pm UTC](https://forum.inductiveautomation.com/t/how-to-script-copy-paste-clipboard-right-mouse-click/10277/9 "2015-08-18T15:59:27Z")

</div>

Thanks for posting that shared script. I haven’t used shared scripts before so I learned how it’s very powerful to only have to maintain it in one location! Very clean and scalable.

Unfortunately, I can’t get it to work with a Text Area. It works perfectly with Text Fields. The error is:

```auto
AttributeError: 'com.inductiveautomation.factorypmi.application.com' object has no attribute 'selectionStart'
```

I’ve been trying to learn about the event object and why selectionStart doesn’t exist for Text Areas but I’m not having any luck.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [August 18, 2015, 5:03pm UTC](https://forum.inductiveautomation.com/t/how-to-script-copy-paste-clipboard-right-mouse-click/10277/10 "2015-08-18T17:03:30Z")

</div>

Hmmm. Ok, I poked around inside the text area component (using [shared.inspect.introspect()](https://inductiveautomation.com/forum/viewtopic.php?f=70&t=13566)) and noted that the JTextArea that has the necessary methods is nested in the viewport of a JScrollPane. So, you need another shared script:[code]def textAreaPopup(event):  
if event.popupTrigger:  
menunames = []  
menufuncs = []  
if event.source.viewport.view.selectionStart != event.source.viewport.view.selectionEnd:  
menunames.append(“cut”)  
def cuttext(ev = event):  
ev.source.viewport.view.cut()  
menufuncs.append(cuttext)  
menunames.append(“copy”)  
def copytext(ev = event):  
ev.source.viewport.view.copy()  
menufuncs.append(copytext)

```
from java.awt import Toolkit
from java.awt.datatransfer import DataFlavor

clipboard = Toolkit.getDefaultToolkit().getSystemClipboard()
contents = clipboard.getContents(None)
if contents is not None and contents.isDataFlavorSupported(DataFlavor.stringFlavor):
  menunames.append("paste")
  def pastetext(ev = event):
    ev.source.viewport.view.paste()
  menufuncs.append(pastetext)
if len(menunames)>0:
  menu = system.gui.createPopupMenu(menunames, menufuncs)
  menu.show(event)[/code]and change the mousePressed() and mouseReleased() code to[code]shared.cliphelp.textAreaPopup(event)[/code]Note: [b]untested![/b]
```

---

<div class="post-metadata">

**Author:** ![framebuilder](https://avatars.discourse-cdn.com/v4/letter/f/96bed5/32.png) [@framebuilder](https://forum.inductiveautomation.com/u/framebuilder)\
**Post date:** [August 18, 2015, 11:07pm UTC](https://forum.inductiveautomation.com/t/how-to-script-copy-paste-clipboard-right-mouse-click/10277/11 "2015-08-18T23:07:12Z")

</div>

Phil,  
That worked perfectly! I created both shared scripts and tried them on two different projects and all was great. Thank you so much. 😃

I also checked out your introspect() function [inductiveautomation.com/forum/v … 70&t=13566](https://inductiveautomation.com/forum/viewtopic.php?f=70&t=13566) and will try to use this in the future to find methods.

I’ve got so much to learn with Python but having this forum is a great resource.

---

<div class="post-metadata">

**Author:** ![shane.jenke](https://avatars.discourse-cdn.com/v4/letter/s/58956e/32.png) [@shane.jenke](https://forum.inductiveautomation.com/u/shane.jenke)\
**Post date:** [May 30, 2022, 10:51pm UTC](https://forum.inductiveautomation.com/t/how-to-script-copy-paste-clipboard-right-mouse-click/10277/12 "2022-05-30T22:51:26Z")

</div>

Hi,

Thanks for this script,

I was able to get this script to copy to the clipboard when I ran it from the script console,  
But when I tried it on a perspective component event script, it didn’t work.

Should this script work from a perspective session (currently using Microsoft Edge as the browser)?

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [May 30, 2022, 11:56pm UTC](https://forum.inductiveautomation.com/t/how-to-script-copy-paste-clipboard-right-mouse-click/10277/13 "2022-05-30T23:56:40Z")

</div>

> [@shane.jenke](#):
>
> when I tried it on a perspective component event script, it didn’t work.

The original discussion long predates the existence of Perspective, so it can only be Vision. Perspective isn't Java Swing like Vision, so anything you find that invokes Swing methods and components certainly won't apply.

Perspective doesn't expose clipboard operations yet, IIRC, so I wouldn't expect to find any variation on this that works.

---

<div class="post-metadata">

**Author:** ![shane.jenke](https://avatars.discourse-cdn.com/v4/letter/s/58956e/32.png) [@shane.jenke](https://forum.inductiveautomation.com/u/shane.jenke)\
**Post date:** [May 31, 2022, 12:21am UTC](https://forum.inductiveautomation.com/t/how-to-script-copy-paste-clipboard-right-mouse-click/10277/14 "2022-05-31T00:21:17Z")

</div>

Ah ok, that’s unfortunate,

Thanks for the quick response!
