View Single Post
Old 02-23-2023, 10:24 AM   #1024
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,204
Karma: 1995558
Join Date: Aug 2015
Device: Kindle
Quote:
Originally Posted by Dfaure View Post
Cf. the tag_field initialization, what is the way to get the name of the selected column in the template code without being forced to redefine it again in the template code?
I defined a template function called current_column (see the code at the end of this post) that gets the selected column name, if @chaley has no interest in adding something similar to calibre, I will add it to the next version of Action Chains.

For now, you can define it as a custom template function in the module editor (see instructions below). After defining this function in the module editor, you can call it from your template:

Code:
program:
    col_name = current_column()
Quote:
Originally Posted by Dfaure View Post
When setting up the feature, I noticed that when dealing with Kobo readers, the device name reported was one of those I had specified via the Kobo Utilities plugin. I was wondering how I could get a similar customization for other device brands?
I don't use the Kobo Utilities plugin, so I am not sure what you are talking about here. Maybe someone else can help with this.

Quote:
Originally Posted by Dfaure View Post
Given the previous consideration, what would be the best way to handle a bunch of associations between a <tag_text> and a <device_name> in my action chain, i.e. how could I use Chain Variables for that ?
I am not sure what you want exactly. If you are talking about friendly names for your devices, there is a function defined below (friendly_connected_device_name) that helps you map the names. First, you need to edit the dictionary (highlighted in blue).

After defining this function in the module editor, you can call it from your template:

Code:
program:
    device = friendly_connected_device_name('main')
Quote:
Originally Posted by Dfaure View Post
Anyway, I also want to thank you for the quality of the tools and the environment they rely on.
Thanks.

To add the code below that contains both functions:
Action Chains > Manage Modules > Create Module > Copy/Paste the code below


Code:
import traceback
from calibre_plugins.action_chains.templates import TemplateFunction
from calibre.utils.formatter_functions import BuiltinConnectedDeviceName

class CurrentColumn(TemplateFunction):

    name = 'current_column'
    arg_count = 0

    def evaluate(self, formatter, kwargs, mi, locals):
        from calibre.gui2.ui import get_gui
        lv = get_gui().library_view
        col = lv.column_map[lv.currentIndex().column()]
        return col

class FriendlyConnctedDeviceName(TemplateFunction):

    name = 'friendly_connected_device_name'
    arg_count = 1

    def evaluate(self, formatter, kwargs, mi, locals, storage_location):
        from calibre.utils.formatter_functions import BuiltinConnectedDeviceName

        friendly_names = {
            'name_1': 'friendly_name_1',
            'name_2': 'friendly_name_2',
            'name_3': 'friendly_name_3'
        }

        try:
            name = BuiltinConnectedDeviceName.evaluate(self, formatter, kwargs, mi, locals, storage_location)
            return friendly_names.get(name, name)
        except:
            traceback.print_exc()
            raise
Edit: Defining the function as pointed out by chaley in the next post, gives you the advantage of being able to use it outside Action Chains as well.

Last edited by capink; 02-23-2023 at 01:36 PM.
capink is offline   Reply With Quote