View Single Post
Old 07-14-2023, 04:29 PM   #1132
capink
Wizard
capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.
 
Posts: 1,200
Karma: 1995558
Join Date: Aug 2015
Device: Kindle
Quote:
Originally Posted by chaley View Post
@capink: A question ...

I sometimes want to run a (python) template over the selected books. This isn't the easiest thing to do.

Solutions I have found:
  • Select the books then use the template tester. Although this works, it isn't an obvious or particularly efficient solution. For example it doesn't support keyboard shortcuts. It does have the advantage that I can see error output.
  • Write a Python code action. This works, but I must implement in the template iteration over the currently selected books. In addition, there isn't a good way to show errors.
  • Write a Formulas action. Here I again must implement my own selection iteration. However, it has the advantage that I can display errors. However, I can't find a way to show nothing if there is no output.
  • Use a Single Field Edit. This solution takes care of iteration. However, it really wants to modify some column, which in this case I don't want to do. The work-around: pick a column like Title and return that value so nothing changes. In addition, it isn't easy to see errors without using an "error column" (long comments) and writing the errors into that.
To solve this problem I have considered making a calibre action that runs a template over the selected books. If the template produces no output then the action doesn't show a dialog. If the template does produce output for any/all books then it opens a dialog like the middle part of the template tester, showing the output per book.

My problem with this solution is that it feels like I am reinventing a part of Action Chains, which seems a bit silly to me. A Formulas-like action that iterates over the selection and shows output only if there is any is more-or-less the same thing.

What are your thoughts? How would you solve this problem?
If you don't want to modify the values it is better to steer away from SFE. Two solutions spring to mind:
  1. An iterate checkbox was introduced to the Chains Variables Action. This will run the template for all the books in the current scope (default is selected books).
  2. The Second solution is using a Formulas Action with a combination of from_selection() and book_field()
    Code:
    program:
        for book_id in from_selection('id'):
            title = book_field(book_id, 'title')
        rof;
    This might not be convenient as it does not allow the usage of a short form like $title.

If either of these solutions not sufficient, please let me know why, so we can work out a solution together.

A solution is not necessarily a new action, it might be a matter of a new template function. While we are at it, is there for a way for a function to know which template mode it is running in, to modify the output depending on the mode. e.g. from_selection() could return a python list instead of a string if running in a python mode.

Edit: As for errors, you can save them in a global variable, and display them using a Formula Action at the end of the chain. This works for any kind of solution you choose, whether a Formula Action, a Chains Variables Action or even a "Run Python Code" Action.

Last edited by capink; 07-14-2023 at 06:49 PM.
capink is offline   Reply With Quote