View Single Post
Old 05-08-2015, 04:44 AM   #143
kacir
Wizard
kacir ought to be getting tired of karma fortunes by now.kacir ought to be getting tired of karma fortunes by now.kacir ought to be getting tired of karma fortunes by now.kacir ought to be getting tired of karma fortunes by now.kacir ought to be getting tired of karma fortunes by now.kacir ought to be getting tired of karma fortunes by now.kacir ought to be getting tired of karma fortunes by now.kacir ought to be getting tired of karma fortunes by now.kacir ought to be getting tired of karma fortunes by now.kacir ought to be getting tired of karma fortunes by now.kacir ought to be getting tired of karma fortunes by now.
 
kacir's Avatar
 
Posts: 3,463
Karma: 10684861
Join Date: May 2006
Device: PocketBook 360, before it was Sony Reader, cassiopeia A-20
Quote:
Originally Posted by BetterRed View Post
Quote:
Originally Posted by kacir View Post
I use AutoKey program for automating certain tasks.
One example is searching for author in Calibre in such a way that it doesn't matter whether I have firstname lastname or lastname, firstname.

Program that is run when I press F2 in my AutoKey is:
Code:
#### Beginning of the Script for F2 hotkey ####
import re

contents = clipboard.get_clipboard()

# re.sub(pattern, repl, string, count=0, flags=0)
output = re.sub(r'([^ ,]*),? (.*)', r'authors:"\1" and authors:"\2"', contents)

keyboard.send_keys(output)
#### End of the Script for F2 hotkey ####
It works in a simple way:
I find somewhere (Calibre itself, or Internet or any text) name of the author, I copy the name such as:
Hugo, Victor
to the clipboard, then I go to Calibre, position cursor to the Search bar and press F2. I get the following
authors:"Hugo" and authors:"Victor"
Well, I *used* to get that. In Beta when I use this script, I get
authors.;hugo; and authors.;victor;
In any other program where I can enter text this works OK.

My system:
Spoiler:

kacir@Petra:~ > calibre-debug -g
calibre 1.205 isfrozen: True is64bit: True
Linux-3.11.0-12-generic-x86_64-with-debian-wheezy-sid Linux ('64bit', 'ELF')
('Linux', '3.11.0-12-generic', '#19-Ubuntu SMP Wed Oct 9 16:20:46 UTC 2013')
Python 2.7.8
Linux: ('debian', 'wheezy/sid', '')


I can (and probably will) find a workaround by extending my script from four lines to eight ;-), but this behavior might indicate some problem.

I used to have this tiny hack built into Calibre as modification to the "find similar books" function, and I have even tried to persuade you to put it into main trunk.
@kacir - there's a plug-in that does something like that ==>> [GUI Plugin] Clipboard Search

I did a few tests on the Qt5'd version the other day on Windows 7, under calibre 1.48 64-bit & calibre 1.205 portable, seems to work as advertised. The Qt5d version is released, so you can install via Prefs->Plug-ins->Get New.

BR
The Clipboard Search doesn't have the functionality I was looking for.
BUT!
I finally got pisse^H^H^H^H^Hmotivated enough to sit down and do something to solve my problem, because it looks like AutoKey is unlikely to resolve the problem with QT5 and I won't be able to make it work the way I want.
So ... I installed the plugin and had a look inside.

It took me 5 minutes to modify the functionality of the code that in original plugin just surrounds the text in quotes.

Code:
I have replaced code:
#######################
    def search_using_clipboard_text(self, is_exact_search=False):
        cb = QApplication.instance().clipboard()
        txt = unicode(cb.text()).strip()
        if txt:
            if is_exact_search:
                # Surround search text with quotes if it does not have any already
                if not txt.startswith('"'):
                    txt = '"' + txt
                if not txt.endswith('"'):
                    txt += '"'
            self.gui.search.set_search_string(txt, store_in_history=True)
#######################

With the following
#######################
import re ## - put at the beginning of the file after other import statements
#######################
    def search_using_clipboard_text(self, is_exact_search=False):
        cb = QApplication.instance().clipboard()
        txt = unicode(cb.text()).strip()
        if txt:
            if is_exact_search:
            self.gui.search.set_search_string(txt, store_in_history=True)
#######################
Thank you for the inspiration.

This is even better - I do not need the AutoKey third-party program AND I do not have to do a private fork of the Calibre (as I used to do in the past) to make this work.

Thank you Kovid for an absolutely wonderful easily-user-extensible framework
Thank you KiwiDude for those fantastic plugins
kacir is offline   Reply With Quote