Clearing Windows' Clipboard While Using Sikuli Or Jython - clipboard

I'm trying to clear the Windows' clipboard using Sikuli or Jython.
I'm currently using the clipboard to grab user entered text from a textbox. I use the clipboard to check if the user entered text before clicking a button. I want to check if the textbox was left empty before the button was clicked.
The problem that I am having is that once the clipboard is used, I am unable to clear it. So the value in the clipboard is returned to my script every time the button is clicked.
I need to be able to clear the clipboard after grabbing the user entered text from it, or find a way to read the textbox without using the clipboard.
Help.
Thanks in advance,
Marwan

Using Jython, you can do as follows to set the contents of the clipboard to an empty string, which would be something like clearing it.
from java.awt.datatransfer import StringSelection
from java.awt.datatransfer import Clipboard
from java.awt import Toolkit
toolkit = Toolkit.getDefaultToolkit()
clipboard = toolkit.getSystemClipboard()
clipboard.setContents(StringSelection(""), None)
And as follows to read the contents of the clipboard as a string:
from java.awt.datatransfer import DataFlavor
contents = clipboard.getContents(None)
print contents.getTransferData(DataFlavor.stringFlavor)

Related

How to set own clipboard data style and get other program's clipboard style?

For example, google docs can remebering the color, style, size and so on. Also, EXCEL can copy and paste data on diffierent charts. However, when you try to paste these into a txt, it failed and it seemed that there is nothing on the clipboard.
It shows that we can create our own style on clipboard. I wondering how it works and how could I set my own clipboard style and how to paste it(How to add a response event). Is there any API to achieve this?
Besides that, >>>>how could we get other program's data on the clipboard<<<<? I woundering this becuase I want to make a software that can help me remembering some special data forms(Sometimes I may forget and just press down Ctrl+C so I lose my data, and I have to get them again)
I have already serch the internet but there is no useful data.
Thank you.

Which clipboard Autokey stores the data with "clipboard.get_selection()"

Autokey function, text = clipboard.get_selection(), which clipboard is the selected data is stored?
I am using Linux Mint 19.1 Cinnamon with Python 3.6. I am searching for the clipboard storage of Autokey's (Python 3) function, text = clipboard.get_selection().
I assume that it is stored in one of the X clipboard's three clipboards, but none of the Gnome compatible clipboard viewers display it.
I need to know how to refer to the particular clipboard in an Autokey, (or Python), script, to view the data before and after it is being altered by a script.
Paste this code in an Autokey3 keyboard macro. (script) and use any key definition to activate it.
In Linux (or any X11 app), the clipboard owner process must be running to # be able to paste the clipboard contents?
Select any text.
import time
import subprocess
import sys
t_xt = ""
keyboard.send_keys("<ctrl>+a") # select all
time.sleep(.3) # delay of .3 of a second
t_xt = clipboard.get_selection # place selection in clipboard
time.sleep(.3)
keyboard.send_keys(t_xt) # retrieves the t_xt I want to know where this is stored and how I can refer it in an Autokey script?
keyboard.send_keys("<ctrl>+a") # This pastes the contents of a different clipboard.
sys.exit()
According to this documentation, it uses the X mouse selection clipboard.

Copy Selected Text with VBA and the Adpbe PDF Reader Control

I am trying to copy selected text directly out of a PDF viewer I have made with the Adobe PDF Reader Control in Visual Basic 2010.
I can utilize highlight text with a macro in Word using something this:
Private Sub CommandButton1_Click()
Dim Sel As Selection
Set Sel = Application.Selection
If Sel.Type <> wdSelectionIP Then
MsgBox Sel.Text
End If
End Sub
I am having trouble figuring out whether this bit of code can be used to perform actions with the highlighted text with Adobe PDF Reader. If not, does anyone know how I would go about doing that?
I don't think that the Adobe PDF viewer supports VBA.
However, I would suggest an indirect approach, using the clipboard.
By sending a control+c key, the content of the selection will be transferred to the clipboard, and the clipboard could be processed using "native" VBA.
Sending a control+c is done by using SendKeys("^C"), see This link
And the Clipboard can be manipulated using This exampe

AppleScript to take text and turn it into pasteable HTML

We work with bugzilla. Whenever you need to query a ticket you just need to know the bugid (integer) and you simply prepend this to it.
http://<bugzilla_server>/bugzilla/show_bug.cgi?id=<bug_id>
Suppose I have a bug link which looks like this 777. If I select and copy this it is preserved on the pasteboard so when I paste this into mail it will correctly preserve the link and it's attributes.
What I am looking for is to simple type '777' select it and run an applescript on it and replace it with a link like the one above. Can anyone help me out??
The following AppleScript will take the contents of the clipboard and replace it with the URL prepended:
set the clipboard to "http://bugzilla_server/bugzilla/show_bug.cgi?id=" & (the clipboard)
You can compile that to an AppleScript scpt and make it available in a Scripts folder or compile it to a launchable app:
osacompile -e 'set the clipboard to "http://bugzilla_server/bugzilla/show_bug.cgi?id=" & (the clipboard)' -o replacebug.scpt # or -o replacebug.app
If your primary use case for this is in composing mail in Mail.app, this may not be the most user-friendly approach, though. If you are using Snow Leopard (10.6), a simpler solution is to take advantage of the new Text Substitution feature. Open the System Preferences -> Language & Text preference panel, select the Text tab, and click + to add a new substitution, perhaps:
Replace With
(b) http://bugzilla_server/bugzilla/show_bug.cgi?id=
Then, in Mail.app, start a New Message and, with the cursor clicked within the text body, do a Control click of the mouse to bring up the contextual menu. From it, select Substitutions -> Text Replacement. From now on, as you are typing in the text body of the email when you type:
(b)777
the (b) will automatically change to the URL text you saved:
http://bugzilla_server/bugzilla/show_bug.cgi?id=777
This will also work in other Cocoa text-enabled applications like Safari.
EDIT:
When talking about composing URL links in email, there are at least three different formats of email, each with a different solution. Since you don't say which kind you are using, I'll cover all three:
Plain text format - There's no way to "hide" the URL in the composed email although some email readers might present a clickable link for a plain-text URL.
HTML-formatted email - Apple's Mail.app does not support composing email in this format although it will display it. Using some other mail writer client or your own program, it's easy enough to compose a link using a standard HTML anchor <a href=...> tag.
Rich Text Format email - AFAIK, this is the only way to compose a URL link with Mail.app. Unfortunately, there does not appear to be an easy way to directly create an RTF hyperlink using AppleScript commands. Based on a suggestion here, this is a way to do it by creating a modifiable RTF template via the clipboard.
In TextEdit.app, create a new Document window.
Insert the text you want to appear in the email, i.e. 777.
Select the text (⌘A) then add a link (⌘K). Enter the full URL also with 777 into the "Link destination" field; click OK.
Modify the text format as desired with Format menu commands.
Save the file (⇧⌘S) as temp.rtf with File Format -> Rich Text Format.
Close the document window.
Open a document window (⌘O) selecting file temp.rtf and selecting Ignore rich text commands.
Insert the following before the first line in the file:
#!/bin/sh
sed -e "s/777/$(pbpaste -Prefer txt)/g" <<EOF | pbcopy -Prefer rtf
Append EOF as a separate line at the end of the file.
It should now look something like this:
#!/bin/sh
sed -e "s/777/$(pbpaste -Prefer txt)/g" <<EOF | pbcopy -Prefer rtf
{\rtf1\ansi\ansicpg1252\cocoartf1038\cocoasubrtf250
{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
\margl1440\margr1440\vieww9000\viewh8400\viewkind0
\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\ql\qnatural\pardirnatural
{\field{\*\fldinst{HYPERLINK "http://bugzilla_server/bugzilla/show_bug.cgi?id=777"}}{\fldrslt
\f0\fs24 \cf0 777}}}
EOF
Save this as a Plain Text file and execute directly as a shell script or call it via the AppleScript do shell script command.
This kind of solution will work with most other applications that support Rich Text format.
Not sure exactly the function you're looking for, but this will take a number from your clipboard and process it into a link and put the link on the clipboard as a standard href URL that will work in plain or rich text, like:
Bug number 777 link
Change <bugzilla_server> to your working URL.
set bug_number to the clipboard
set the_text to "Bug number " & bug_number & " link"
set the clipboard to the_text

Intercept Copy and Paste with AppleScript

Is it possible to intercept global copy and paste in OS X with an AppleScript? Or would I have to have some intermediary step that would trigger the AppleScript to read the clipboard?
They way I do it in Applescript is with the following command:
get the clipboard
You could of course then do stuff like setting it to a variable and displaying it:
set xyz to the clipboard
display alert (xyz as text)
Hope that helps!
Elliott

Resources