Re: Accessing clipboard through software built on Python

2018-10-28 Thread Tim Daneliuk
On 10/27/2018 08:17 AM, Musatov wrote:
> I am wondering if Python could be used to write a program that allows:
> 
> 1. Highlight some text
> 2. Ctl+HOTKEY1 stores the string of text somewhere as COPIEDTEXT1
> 3. Highlight another string of text
> 4. Ctl+HOTKEY1 stores another string of text somewhere as COPIEDTEXT2
> 
> THEN
> 
> 5. Ctl+HOTKEY2 pastes COPIEDTEXT1
> 6. Ctl+HOTKEY2 pastes COPIEDTEXT2
> 
> I found "pyperclip" and "Tkinter" but I don't know where to start.
> 
> Thanks,
> 
> Musatov
> 


I am able to do this with clipboard (pip install clipboard).


However, the highlighted text must be copied explicitly:

Highlight
Ctl-C

In Python:

TEXT1 = clipboard.paste()

Highlight again

TEXT2 = clipboard.paste()

X actually has several clipboard buffers and it can be tricky to get this 
going.  I don't recall,
but either clipboard or pyperclip have a way to get to them all IIRC ...

HTH,
-Tim

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Accessing clipboard through software built on Python

2018-10-28 Thread Brian Oney via Python-list
You don't have to start from scratch. You don't to do anything other than learn 
to use anamnesis. I use anamnesis as my clipboard manager. I you can easily 
tell to get which ever one you want (i.e.  the thousandth item).

# Inform yourself
https://sourceforge.net/projects/anamnesis/
# Install it
cd ~/bin/src/
wget -O anamnesis.tar.gz  
https://sourceforge.net/projects/anamnesis/files/latest/download
tar xzf anamnesis.tar.gz
ln -sf ~/bin/src/anamnesis-1.0.4/source/anamnesis.py ~/bin/anamnesis

# Be happy


It hasn't changed in years and that's just fine. I just works. I can access it 
through the command line when I am in a remote ssh session. Or I have it as a 
autostarted daemon. It uses sqlite to store all your copied (and selected, if 
configured) text, which can be slow on a spinning disk. If this turns you off, 
replace your OS hard drive with an ssd - life's too short.

HTH

On October 28, 2018 5:14:54 PM GMT+01:00, Tim Daneliuk  
wrote:
>On 10/27/2018 08:17 AM, Musatov wrote:
>> I am wondering if Python could be used to write a program that
>allows:
>> 
>> 1. Highlight some text
>> 2. Ctl+HOTKEY1 stores the string of text somewhere as COPIEDTEXT1
>> 3. Highlight another string of text
>> 4. Ctl+HOTKEY1 stores another string of text somewhere as COPIEDTEXT2
>> 
>> THEN
>> 
>> 5. Ctl+HOTKEY2 pastes COPIEDTEXT1
>> 6. Ctl+HOTKEY2 pastes COPIEDTEXT2
>> 
>> I found "pyperclip" and "Tkinter" but I don't know where to start.
>> 
>> Thanks,
>> 
>> Musatov
>> 
>
>
>I am able to do this with clipboard (pip install clipboard).
>
>
>However, the highlighted text must be copied explicitly:
>
>Highlight
>Ctl-C
>
>In Python:
>
>TEXT1 = clipboard.paste()
>
>Highlight again
>
>TEXT2 = clipboard.paste()
>
>X actually has several clipboard buffers and it can be tricky to get
>this going.  I don't recall,
>but either clipboard or pyperclip have a way to get to them all IIRC
>...
>
>HTH,
>-Tim
>
>-- 
>https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


regular expression problem

2018-10-28 Thread Karsten Hilbert
Dear list members,

I cannot figure out why my regular expression does not work as I expect it to:

#---
#!/usr/bin/python

from __future__ import print_function
import re as regex

rx_works = '\$<[^<:]+?::.*?::\d*?>\$|\$<[^<:]+?::.*?::\d+-\d+>\$'
# it fails if switched around:
rx_fails = '\$<[^<:]+?::.*?::\d+-\d+>\$|\$<[^<:]+?::.*?::\d*?>\$'
line = 'junk  $$  junk  $$  
junk'

print ('')
print ('line:', line)
print ('expected: $$')
print ('expected: $$')

print ('')
placeholders_in_line = regex.findall(rx_works, line, regex.IGNORECASE)
print('found (works):')
for ph in placeholders_in_line:
print (ph)

print ('')
placeholders_in_line = regex.findall(rx_fails, line, regex.IGNORECASE)
print('found (fails):')
for ph in placeholders_in_line:
print (ph)

#---

I am sure I simply don't see the problem ?

Thanks,
Karsten
-- 
GPG  40BE 5B0E C98E 1713 AFA6  5BC0 3BEA AC80 7D4F C89B
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Accessing clipboard through software built on Python

2018-10-28 Thread Akkana Peck
Tim Daneliuk writes:
> However, the highlighted text must be copied explicitly:
> 
> Highlight
> Ctl-C
[ ... ]
> X actually has several clipboard buffers and it can be tricky to get this 
> going.  I don't recall,
> but either clipboard or pyperclip have a way to get to them all IIRC ...

To get the highlighted text in X without needing a copy, you want the
PRIMARY X selection rather than CLIPBOARD.

I think pyperclip can get it (it uses an external program like xsel,
and xsel can get any of the three X selections), or you can get the
selection using a GUI library like GTK, Qt or Tk. Some examples:
https://github.com/akkana/scripts/blob/master/pyclip

...Akkana
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Accessing clipboard through software built on Python

2018-10-28 Thread Tim Daneliuk
On 10/28/2018 02:08 PM, Akkana Peck wrote:
> Tim Daneliuk writes:
>> However, the highlighted text must be copied explicitly:
>>
>> Highlight
>> Ctl-C
> [ ... ]
>> X actually has several clipboard buffers and it can be tricky to get this 
>> going.  I don't recall,
>> but either clipboard or pyperclip have a way to get to them all IIRC ...
> 
> To get the highlighted text in X without needing a copy, you want the
> PRIMARY X selection rather than CLIPBOARD.
> 
> I think pyperclip can get it (it uses an external program like xsel,
> and xsel can get any of the three X selections), or you can get the
> selection using a GUI library like GTK, Qt or Tk. Some examples:
> https://github.com/akkana/scripts/blob/master/pyclip
> 
> ...Akkana
> 


Yes, that sounds right in the distant recesses of my memory :)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: regular expression problem

2018-10-28 Thread MRAB

On 2018-10-28 18:51, Karsten Hilbert wrote:

Dear list members,

I cannot figure out why my regular expression does not work as I expect it to:

#---
#!/usr/bin/python

from __future__ import print_function
import re as regex

rx_works = '\$<[^<:]+?::.*?::\d*?>\$|\$<[^<:]+?::.*?::\d+-\d+>\$'
# it fails if switched around:
rx_fails = '\$<[^<:]+?::.*?::\d+-\d+>\$|\$<[^<:]+?::.*?::\d*?>\$'
line = 'junk  $$  junk  $$  
junk'

print ('')
print ('line:', line)
print ('expected: $$')
print ('expected: $$')

print ('')
placeholders_in_line = regex.findall(rx_works, line, regex.IGNORECASE)
print('found (works):')
for ph in placeholders_in_line:
print (ph)

print ('')
placeholders_in_line = regex.findall(rx_fails, line, regex.IGNORECASE)
print('found (fails):')
for ph in placeholders_in_line:
print (ph)

#---

I am sure I simply don't see the problem ?

Here are some of the steps while matching the second regex. (View this 
in a monospaced font.)



1:
junk  $$  junk  $$  junk
  ^

\$<[^<:]+?::.*?::\d+-\d+>\$|\$<[^<:]+?::.*?::\d*?>\$
^


2:
junk  $$  junk  $$  junk
 ^

\$<[^<:]+?::.*?::\d+-\d+>\$|\$<[^<:]+?::.*?::\d*?>\$
^


3:
The .*? matches as few characters as possible, initially none.

junk  $$  junk  $$  junk
  ^
^
\$<[^<:]+?::.*?::\d+-\d+>\$|\$<[^<:]+?::.*?::\d*?>\$
   ^


4:
junk  $$  junk  $$  junk
 ^

\$<[^<:]+?::.*?::\d+-\d+>\$|\$<[^<:]+?::.*?::\d*?>\$
^

At this point it can't match, so it backtracks.


5:
The .*? matches more characters, including the ":".

After more matching it's like the following.

junk  $$  junk  $$  junk
^

\$<[^<:]+?::.*?::\d+-\d+>\$|\$<[^<:]+?::.*?::\d*?>\$
   ^


6:
junk  $$  junk  $$  junk
  ^

\$<[^<:]+?::.*?::\d+-\d+>\$|\$<[^<:]+?::.*?::\d*?>\$
 ^

Again it can't match, so it backtracks.


7:
The .*? matches more characters, including the ":".

After more matching it's like the following.

junk  $$  junk  $$  junk
   ^

\$<[^<:]+?::.*?::\d+-\d+>\$|\$<[^<:]+?::.*?::\d*?>\$
   ^

8:
junk  $$  junk  $$  junk
  ^

\$<[^<:]+?::.*?::\d+-\d+>\$|\$<[^<:]+?::.*?::\d*?>\$
   ^

Success!

The first choice has matched this:

$$  junk  $$
--
https://mail.python.org/mailman/listinfo/python-list


Re: Accessing clipboard through software built on Python

2018-10-28 Thread Tim Daneliuk
On 10/28/2018 02:08 PM, Akkana Peck wrote:
> Tim Daneliuk writes:
>> However, the highlighted text must be copied explicitly:
>>
>> Highlight
>> Ctl-C
> [ ... ]
>> X actually has several clipboard buffers and it can be tricky to get this 
>> going.  I don't recall,
>> but either clipboard or pyperclip have a way to get to them all IIRC ...
> 
> To get the highlighted text in X without needing a copy, you want the
> PRIMARY X selection rather than CLIPBOARD.
> 
> I think pyperclip can get it (it uses an external program like xsel,
> and xsel can get any of the three X selections), or you can get the
> selection using a GUI library like GTK, Qt or Tk. Some examples:
> https://github.com/akkana/scripts/blob/master/pyclip
> 
> ...Akkana
> 


Yes, that sounds right in the distant recesses of my memory :)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: regular expression problem

2018-10-28 Thread Karsten Hilbert
Now that MRAB has shown me the follies of my ways I would
like to learn how to properly write the regular expression I
need.

This part:

> rx_works = '\$<[^<:]+?::.*?::\d*?>\$|\$<[^<:]+?::.*?::\d+-\d+>\$'
> # it fails if switched around:
> rx_fails = '\$<[^<:]+?::.*?::\d+-\d+>\$|\$<[^<:]+?::.*?::\d*?>\$'

suggests that I already have a solution. However, in reality this line:

> line = 'junk  $$  junk  $$  
> junk'

can be either way round (match_A, then match_B or the vice
versa) which, in turn, will switch the rx_works/rx_fails.

Let my try to explain the expression I am actually after
(assuming .compile with re.VERBOSE):

rx_works = '
\$< # start of match is literal '$<' 
anywhere inside string
[^<:]+?::   # followed by at least one "character", except 
'<' or ':', until the next '::'  (this is the placeholder "name")
.*?::   # followed by any number of any "character", 
until the next '::'(this is the 
placeholder "options")
\d*?# followed by any number of digits  

(the max length of placeholder output)
>\$ # followed by '>$'
|   # -- OR (in *either* order) --
\$< # start of match is literal '$<' 
anywhere inside string
[^<:]+?::   # followed by at least one "character", except 
'<' or ':', until the next '::'  (this is the placeholder "name")
.*?::   # followed by any number of any "character", 
until the next '::'(this is the 
placeholder "options")
# now the difference:
\d+-\d+ # followed by one-or-many digits, a '-', and 
one-or-many digits (this is the *range* 
from with placeholder output)
>\$'# followed by '>$'

I want this to work for

any number of matches

in any order of max-lenght or output-range

inside one string.

Now, why the [^<:]+? dance ?

Because three levels of placeholders

$<...::...::>$
$<<...::...::>>$
$<<<...::...::>>>$

need to be nestable inside each other ;-)

Anyone able to help ?

This seems beyond my current grasp of regular expressions.

Thanks,
Karsten
-- 
GPG  40BE 5B0E C98E 1713 AFA6  5BC0 3BEA AC80 7D4F C89B
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: regular expression problem

2018-10-28 Thread Karsten Hilbert
On Sun, Oct 28, 2018 at 09:43:27PM +0100, Karsten Hilbert wrote:

> Let my try to explain the expression I am actually after
> (assuming .compile with re.VERBOSE):
> 
> rx_works = '
>   \$< # start of match is literal '$<' 
> anywhere inside string
>   [^<:]+?::   # followed by at least one "character", except 
> '<' or ':', until the next '::'  (this is the placeholder "name")
>   .*?::   # followed by any number of any "character", 
> until the next '::'(this is the 
> placeholder "options")
>   \d*?# followed by any number of digits  
>   
>   (the max length of placeholder output)
>   >\$ # followed by '>$'
>   |   # -- OR (in *either* order) --
>   \$< # start of match is literal '$<' 
> anywhere inside string
>   [^<:]+?::   # followed by at least one "character", except 
> '<' or ':', until the next '::'  (this is the placeholder "name")
>   .*?::   # followed by any number of any "character", 
> until the next '::'(this is the 
> placeholder "options")
>   # now the difference:
>   \d+-\d+ # followed by one-or-many digits, a '-', and 
> one-or-many digits (this is the 
> *range* from with placeholder output)
>   >\$'# followed by '>$'

Another try:

- lines can contain several placeholders

- placeholders start and end with '$'

- placeholders are parsed in three passes

- the pass in which a placeholder is parsed is denoted by the number of '<' and 
'>' next to the '$':

$<...>$ / $<<...>>$ / $<<<...>>>$

- placeholders for different parsing passes must be nestable:

$<<<...$<...>$...>>>$

(lower=earlier parsing passes will be inside)

- the internal structure is "name::options::range"

$$

- name will *not* contain '$' '<' '>' ':'

- range can be either a length or a "from-until"

- a length will be a positive integer (no bounds checking)

- "from-until" is: a positive integer, a '-', and a positive integer (no sanity 
checking)

- options needs to be able to contain nearly anything, except '::'


Is that sufficiently defined and helpful to design the regular expression ?

Karsten
-- 
GPG  40BE 5B0E C98E 1713 AFA6  5BC0 3BEA AC80 7D4F C89B
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: regular expression problem

2018-10-28 Thread Karsten Hilbert
On Sun, Oct 28, 2018 at 10:04:39PM +0100, Karsten Hilbert wrote:

> - options needs to be able to contain nearly anything, except '::'

This seems to contradict the "nesting" requirement, but the
nesting restriction "earlier parsing passes go inside" makes
it possible.

Karsten
-- 
GPG  40BE 5B0E C98E 1713 AFA6  5BC0 3BEA AC80 7D4F C89B
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Accessing clipboard through software built on Python

2018-10-28 Thread Peter via Python-list



On 28/10/2018 12:17 AM, Musatov wrote:

I am wondering if Python could be used to write a program that allows:

1. Highlight some text
2. Ctl+HOTKEY1 stores the string of text somewhere as COPIEDTEXT1
3. Highlight another string of text
4. Ctl+HOTKEY1 stores another string of text somewhere as COPIEDTEXT2

THEN

5. Ctl+HOTKEY2 pastes COPIEDTEXT1
6. Ctl+HOTKEY2 pastes COPIEDTEXT2

I found "pyperclip" and "Tkinter" but I don't know where to start.

Thanks,

Musatov

You might find the GUI automation tool Skiuli or the newer SikuliX 
programs useful. They can be used to script a GUI and perform operations.


http://www.sikuli.org/

If you're on Windows then the COM interface can be useful (if the 
program you want to copy from supports it).


Peter


--
https://mail.python.org/mailman/listinfo/python-list


Re: regular expression problem

2018-10-28 Thread Brian Oney via Python-list
On Sun, 2018-10-28 at 22:04 +0100, Karsten Hilbert wrote:
> [^<:]

Would a simple regex work?

I mean:

~$ python
Python 2.7.13 (default, Sep 26 2018, 18:42:22) 
[GCC 6.3.0 20170516] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> t = '$$'
>>> re.findall('[^<>:$]+', t)
['name', 'options', 'range']

You can then interpret what you have extracted afterwards.
Maybe if you want to have the single ones grouped you could consider:

>>> t = t*2
>>> t
''
>>> re.findall('\$<+([^:]+)::([^:]+)::([^:]+)>+\$', t)
[('name', 'options', 'range'), ('name', 'options', 'range')]

HTH
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: regular expression problem

2018-10-28 Thread MRAB

On 2018-10-28 21:04, Karsten Hilbert wrote:

On Sun, Oct 28, 2018 at 09:43:27PM +0100, Karsten Hilbert wrote:


Let my try to explain the expression I am actually after
(assuming .compile with re.VERBOSE):

rx_works = '
\$<  # start of match is literal '$<' anywhere 
inside string
[^<:]+?::# followed by at least one "character", except '<' or ':', 
until the next '::' (this is the placeholder "name")
.*?::   # followed by any number of any "character", until the 
next '::'  (this is the placeholder "options")
\d*?# followed by any number of digits  

(the max length of placeholder output)
>\$  # followed by '>$'
|   # -- OR (in *either* order) --
\$<  # start of match is literal '$<' anywhere 
inside string
[^<:]+?::# followed by at least one "character", except '<' or ':', 
until the next '::' (this is the placeholder "name")
.*?::   # followed by any number of any "character", until the 
next '::'  (this is the placeholder "options")
# now the difference:
\d+-\d+ # followed by one-or-many digits, a '-', and 
one-or-many digits (this is the *range* 
from with placeholder output)
>\$' # followed by '>$'


Another try:

- lines can contain several placeholders

- placeholders start and end with '$'

- placeholders are parsed in three passes

- the pass in which a placeholder is parsed is denoted by the number of '<' and 
'>' next to the '$':

$<...>$ / $<<...>>$ / $<<<...>>>$

- placeholders for different parsing passes must be nestable:

$<<<...$<...>$...>>>$

(lower=earlier parsing passes will be inside)

- the internal structure is "name::options::range"

$$

- name will *not* contain '$' '<' '>' ':'

- range can be either a length or a "from-until"

- a length will be a positive integer (no bounds checking)

- "from-until" is: a positive integer, a '-', and a positive integer (no sanity 
checking)

- options needs to be able to contain nearly anything, except '::'


Is that sufficiently defined and helpful to design the regular expression ?


How can they be nested inside one another?
Is the string scanned, placeholders filled in for that level, and then 
the string scanned again for the next level? (That would mean that the 
fill value itself will be scanned in the next pass.)


You could try matching the top level, for each match then match the next 
level, and for each of those matches then match for the final level.


Trying to do it all in one regex is usually a bad idea. Keep it simple! 
(Do you even need to use a regex?)

--
https://mail.python.org/mailman/listinfo/python-list


Re: regular expression problem

2018-10-28 Thread Thomas Jollans
On 28/10/2018 22:04, Karsten Hilbert wrote:
> - options needs to be able to contain nearly anything, except '::'
> 
> Is that sufficiently defined and helpful to design the regular expression ?

so options isn't '.*', but more like '(:?[^:]+)*' (Figuring out what
additional restriction this imposes is left an an exercise for the reader)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: regular expression problem

2018-10-28 Thread Thomas Jollans
On 28/10/2018 22:04, Karsten Hilbert wrote:
> - options needs to be able to contain nearly anything, except '::'

Including > and $ ?
-- 
https://mail.python.org/mailman/listinfo/python-list


zenity substitution

2018-10-28 Thread listo factor via Python-list

Hi all,
I'm new to Python, but not to programming.

As a teaching exercise, I am converting a bunch of bash shell
scripts to Python, so that they can be run on all three OS-es
(Linux, Windows, MacOS).

The scripts in questions make extensive use of Linux "zenity"
dialogs.

Is there an equivalent facility in Python 3? If so, what is it?

TIA
--
https://mail.python.org/mailman/listinfo/python-list


Re: Accessing clipboard through software built on Python

2018-10-28 Thread Thomas Jollans
On 27/10/2018 20:50, Musatov wrote:
> On Saturday, October 27, 2018 at 11:12:35 AM UTC-5, Marko Rauhamaa wrote:
>> Michael Torrie :
>>> As far as I know it's not possible for an application to directly yank
>>> highlighted text from another application.
>>
>> That's an age-old pattern in X11. I don't know if Wayland supports it.
>>
>> Application 1 holds a selection (usually highlighted) and Application 2
>> wants to copy the selection. No clipboard is needed. Application 2
>> simply asks for the selection. The request is relayed to Application 1,
>> which generates the response:
>>
>> https://en.wikipedia.org/wiki/X_Window_selection#Selections>
>>
>>
>> Marko
> 
> I work from a web database of users and I continually have to copy email 
> address and user ID to two separate fields on a Salesforce.com page.
> 
> I go to the webpage, highlight email address then copy.
> Then go to Salesforce page, and paste.
> Then go back to the webpage, then copy the User ID.
> Then go back to Salesforce page, and paste.
> 
> I think it would be much more efficient to:
> On webpage, copy emailaddress and user ID.
> Then go to Salesforce and paste email address and user ID.
> 

As this is all on the web, it's probably easier to work in the browser
rather than at the display manager level, i.e.: build a browser
extension or something instead. You'll almost certainly have easier
access to selected text (not in Python, though).

Really though you might want to take a step back and take the web page
out the the equation completely. Salesforce must have an API, right? And
the email addresses you're copying are presented in some regular format
you might be able to parse (using beautifulsoup or something)? Or maybe
the data source has an API, even? Or a database you can access?

Surely, you could write a program that somehow gets (all) the data you
want to copy from whatever server you get them from, gives you an
opportunity to do whatever checks you have to do by hand, and then feeds
the data into the database they go in in the end – or gives the data to
you in a format that makes that last step easy. Right?

All I'm saying is: if you're going to try to automate a job, you might
as well do it with conviction ^_^

If you insist on getting the selected text with something like
autohotkey, my hunch is that the simplest way to do this would be to
inject Ctrl+C, wait a moment, and then retrieve the clipboard content.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: zenity substitution

2018-10-28 Thread Bob Gailer
On Oct 28, 2018 10:17 PM, "listo factor via Python-list" <
python-list@python.org> wrote:
>
> Hi all,
> I'm new to Python, but not to programming.
>
> As a teaching exercise, I am converting a bunch of bash shell
> scripts to Python, so that they can be run on all three OS-es
> (Linux, Windows, MacOS).
>
> The scripts in questions make extensive use of Linux "zenity"
> dialogs.
>
> Is there an equivalent facility in Python 3? If so, what is it?

Look up zenity in Wikipeda. Scroll down to cross-platform script.

Bob Gailer
-- 
https://mail.python.org/mailman/listinfo/python-list