How To Resolve "cannot open system clipboard" error

Asked by Rafiq Mohammed

Hi,
I am using the following code extensively to copy the contents of clipboard into a variable:

uid = Env.getClipboard().strip()
and
row = int (Env.getClipboard())

In the earlier version of Sikuli, the following error used to occasionally occur:

[error] java.lang.IllegalStateException ( java.lang.IllegalStateException: cannot open system clipboard )
[error] --- Traceback --- error source first line: module ( function ) statement 176: main ( open_Excel ) uid = Env.getClipboard().strip()
[error] --- Traceback --- end --------------

When this error occurs, I used to just re-run the script and the script would run fine.

But now, after upgrading to Sikuli 1.0.1, the frequency of this error has increased and re-running the script is not helping. Could you please let me me know if there is any other efficient way to get this done.

This is my operating environment:
IDE: Sikli 1.0.1
OS: Windows 7 (64-bit)
Java 7 (Earlier I was using Java 6. This error was not frequent then.)

Thanks,
Rafiq

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

might be, that the internal implementation in Java 7 has changed.

For now, I do not know any workaround for that.
I take it as a bug. need to check deeper.

Might be a timing problem (as far as I remember: you use ctrl-c to get it on the clipboard).
But sorry, for now, I cannot really help you.

In the perspective, I would recommend, to use the module xlrd anyway, to access excel sheets. Or convert them to csv files and access those.

Revision history for this message
Rafiq Mohammed (rcholayil) said :
#2

Hi RaiMAn,
Could you please elaborate on how using csv file would solve this problem. Wouldn't I still need to use cntrl+c to access the values in the csv file??

Thanks,
Rafiq

Revision history for this message
rob (reg82) said :
#3

i think he means try something like -
for line in open('path-to-your-file.csv'):
  print line

i was getting the same error using Env.getClipboard() to read html using view source. so i started saving the source as text files and then using open. it works well enough.

Revision history for this message
RaiMan (raimund-hocke) said :
#4

You are accessing the XLS "visually" over the GUI using copy and paste.

A CSV file is the sheet (usually comma separated), that contains the rows and the cell values.

This can be accessed in a script directly, so you just have your row contents directly in strings to work with.

The same can be done with module xlrd (see faq 2208) and even no need to convert to CSV, since it allows, to directly access cells in a sheet.

Revision history for this message
Rafiq Mohammed (rcholayil) said :
#5

Thanks rob and RaiMan for your replies.
xlrd unfortunately does not support the version of Excel that I am using.

From the sample that rob provided, I understand that we can read a csv file. Is there any way we can update the csv file as well??

Thanks,
Rafiq

Revision history for this message
Best RaiMan (raimund-hocke) said :
#6

-- xlrd
if it is a newer Excel or a problem with .xlsx, just save your sheet in an older layout as .xls.

-- csv
a csv file is a simple textfile. of course you can write to a (csv) file.
But you cannot write back to the same file that you are reading from.

Revision history for this message
RaiMan (raimund-hocke) said :
#7

There is even a high sophisticated Python module CSV, that hides all the specific formatting, reading and writing.
https://docs.python.org/2/library/csv.html#module-csv

Revision history for this message
Rafiq Mohammed (rcholayil) said :
#8

Thanks RaiMan, that solved my question.

Revision history for this message
Andrew Gr (andrew-gr) said :
#9

Hi,

I have almost the same case: I extensively use the following code while processing Excel:
    type(Key.INSERT, Key.CTRL)
    cellValue = Env.getClipboard().strip()

And at some point (randomly, sometimes after 5 rows, sometimes after 20 rows) I am getting the error:
    Env.getClipboard: clipboard not available: cannot open system clipboard

I have even added such hook in order to repeat the access to clipboard:
    if (cellValue == "") :
        print ("HOOK 1: empty")
        type(Key.ESC)
        type(Key.INSERT, Key.CTRL)
        cellValue = Env.getClipboard().strip()

Probably it helps but at some point on second attempt to access clipboard it stops working as well.

In case there is no possibility to access Excel file programmatically, is it possible to somehow solve the error, maybe by implementing some workaround?

I have searched extensively this forum and internet for this error, but beside the following topic didn't find anything helpful:
    "Using the native getClipboard method in eclipse"
    https://answers.launchpad.net/sikuli/+question/170984

Environment:
IDE: Sikli 1.1.1 (and also tried with 1.1.2)
OS: Windows 7 (64-bit)
Java 8 x64 b131

Thank you in advance!

Revision history for this message
Andrew Gr (andrew-gr) said :
#10

Not sure how, but after reboot it was able to process all 230 rows without throwing the error...
No code changes were made...

Revision history for this message
RaiMan (raimund-hocke) said :
#11

Thanks for the pointer to the old clipboard question.

I will look into the code and check, wether it can be optimised in this sense.