![]() |
#1441 |
Addict
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 307
Karma: 1599870
Join Date: Jun 2012
Device: none
|
Need help with module to add empty book
I'm trying to create a module that adds an empty book and fills in some default metadata. Everything works except that the book doesn't show up in the book list until I reload the library (for example by restarting Calibre or switching to a different library and back). Here is my code:
Code:
from calibre_plugins.action_chains.actions.base import ChainAction from calibre.ebooks.metadata.book.base import Metadata class TestAddEmpty(ChainAction): name = 'Add Empty' def run(self, gui, settings, chain): db = gui.current_db ids, duplicates = db.new_api.add_books([(Metadata(None), {})]) # What do I put here? newid = ids[0] gui.library_view.select_rows([newid]) Thanks. |
![]() |
![]() |
![]() |
#1442 |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 6,511
Karma: 12595249
Join Date: Jun 2009
Location: Madrid, Spain
Device: Kobo Clara/Aura One/Forma,XiaoMI 5, iPad, Huawei MediaPad, YotaPhone 2
|
Need help with an action chain in the same way:
For [1] I've found the proper resource For [2] I can do a module which:
I have the following doubts:
Sorry for the long post, but I've done very basic things at Action Chains till now. |
![]() |
![]() |
![]() |
#1443 | |
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 1,174
Karma: 1988646
Join Date: Aug 2015
Device: Kindle
|
Quote:
Code:
gui.iactions['Add Books'].refresh_gui(1,set_current_row=0) Code:
gui.library_view.model().books_added(1) |
|
![]() |
![]() |
![]() |
#1444 | ||
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 1,174
Karma: 1988646
Join Date: Aug 2015
Device: Kindle
|
Quote:
Code:
chain.chain_vars.get('variable_name', '') Quote:
Code:
if len(gui.current_view().selectionModel().selectedRows()) !=1: do_something |
||
![]() |
![]() |
![]() |
#1445 | |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 6,511
Karma: 12595249
Join Date: Jun 2009
Location: Madrid, Spain
Device: Kobo Clara/Aura One/Forma,XiaoMI 5, iPad, Huawei MediaPad, YotaPhone 2
|
Quote:
|
|
![]() |
![]() |
![]() |
#1446 | |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,269
Karma: 7955525
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
Code:
python: def evaluate(book, context): db = context.db from calibre.ebooks.metadata.book.base import Metadata ids, duplicates = db.new_api.add_books([(Metadata(None), {})]) print(ids, duplicates) from calibre.gui2.ui import get_gui gui = get_gui() gui.current_db.data.refresh() gui.library_view.model().resort() gui.tags_view.recount() gui.library_view.set_current_row(book_id=ids[0]) return 'a string' |
|
![]() |
![]() |
![]() |
#1447 |
Addict
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 307
Karma: 1599870
Join Date: Jun 2012
Device: none
|
It worked!
Thanks, chaley. You're awesome. ![]() I tweaked it a little, and here is the complete module code that adds an empty book, in case it's of interest to anyone else: Code:
from calibre_plugins.action_chains.actions.base import ChainAction from calibre.ebooks.metadata.book.base import Metadata class AddEmpty(ChainAction): name = 'Add Empty' def run(self, gui, settings, chain): db = gui.current_db ids, duplicates = db.new_api.add_books([(Metadata(None), {})]) db.data.refresh() gui.library_view.model().resort() gui.tags_view.recount() newid = ids[0] gui.library_view.select_rows([newid]) |
![]() |
![]() |
![]() |
#1448 |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 6,511
Karma: 12595249
Join Date: Jun 2009
Location: Madrid, Spain
Device: Kobo Clara/Aura One/Forma,XiaoMI 5, iPad, Huawei MediaPad, YotaPhone 2
|
Thank you, as I'm going to use it as the base for duplicating empty books (I use it from time to time).
|
![]() |
![]() |
![]() |
#1449 | |
Addict
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 307
Karma: 1599870
Join Date: Jun 2012
Device: none
|
Quote:
This one creates the book with the metadata already copied from the book that was selected when you activate the plugin, so it's probably more efficient than creating a new book and copying over metadata fields afterward. A couple of notes: 1. It only allows you to duplicate one book at a time, because I didn't need anything more robust when I wrote it. The code is simple enough though that if you're at all familiar with Python it shouldn't be too hard to modify it to handle multiple books, if that's what you need. 2. It only duplicates the metadata; it doesn't copy the book formats. Since you mentioned duplicating empty books, I figure that shouldn't be a problem for you. Here is the source code: Code:
from calibre_plugins.action_chains.actions.base import ChainAction class DuplicateBook(ChainAction): name = 'Duplicate Book' def run(self, gui, settings, chain): db = gui.current_db rows = gui.current_view().selectionModel().selectedRows() book_ids = [ gui.library_view.model().db.id(row.row()) for row in rows ] #Only applies if exactly one book is selected. if len(book_ids) == 1: book_id = book_ids[0] #get the metadata from the existing book. meta = db.new_api.get_metadata(book_id) ids, duplicates = db.new_api.add_books([(meta, {})]) newid = ids[0] db.data.refresh() gui.library_view.model().resort() gui.tags_view.recount() gui.library_view.select_rows([newid]) else: raise Exception("Please select only one book first.") |
|
![]() |
![]() |
![]() |
#1450 | |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 6,511
Karma: 12595249
Join Date: Jun 2009
Location: Madrid, Spain
Device: Kobo Clara/Aura One/Forma,XiaoMI 5, iPad, Huawei MediaPad, YotaPhone 2
|
Quote:
|
|
![]() |
![]() |
![]() |
#1451 |
Member
![]() Posts: 10
Karma: 10
Join Date: Nov 2023
Device: none
|
![]()
I have a basic question because I dont understand why it do not work as I want it to. Asfar as I understood you can set conditions before a specific task or a chain starts. But my condition do not work. If I have 2 Books where one is in a"Series" and the other is a "Single". It do not get the two right.
My Code condition A: program: ifempty(field('series'), 'text_if_empty') text = text_if_empty chain -> edit Single field choose in field XY "Single" My Code condition B: program: ifempty(field('series'), 'text_if_empty') text != text_if_empty chain -> edit Single field choose in field XY "Series" Here also screenshots: What do I understand wrong here? Does is not check the condition at every Book selected? Or is the Text check with = and != not possible? Its my first try with the plugin and I tested some possibilitys and read the instructions but dont get what I understood wrong. ![]() |
![]() |
![]() |
![]() |
#1452 |
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 1,174
Karma: 1988646
Join Date: Aug 2015
Device: Kindle
|
Conditions does not check all selected books because they are meant quick branching tool based on things that can be tested quickly like selection_count etc.
If you want to check every book, and only process books that have series column (I suppose that what you want to do), you add a "Search using template" Action and do the following:
As an aside, attach your screenshots to the post instead of using external sites as they are not showing. |
![]() |
![]() |
![]() |
Thread Tools | Search this Thread |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Action Chains Resources | capink | Plugins | 68 | 01-20-2025 01:06 PM |
[Editor Plugin] Editor Chains | capink | Plugins | 94 | 07-03-2024 07:26 PM |
[GUI Plugin] Noosfere_util, a companion plugin to noosfere DB | lrpirlet | Plugins | 2 | 08-18-2022 03:15 PM |
[GUI Plugin] Save Virtual Libraries To Column (GUI) | chaley | Plugins | 14 | 04-04-2021 05:25 AM |