View Single Post
Old 12-24-2020, 09:27 AM   #171
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,199
Karma: 1995558
Join Date: Aug 2015
Device: Kindle
Help on the chain variables action

The Chain Variables action enables the user to set variables to the chain, to be utilized in multiple ways like marking books or using them in conditional execution of actions. The user can set constant variables, or can set dynamic variables using Calibre's template. When setting dynamic variables using templates, only General Program Mode is supported. i.e templates starting with program.

In addition to user variables, the plugin offers some predefined variables that can be used by the user when setting dynamic variables. These Variables include:
  1. Runtime chain variables:
    • _chain_name
    • _chain_iteration
    • _chain_start_time
  2. Runtime action variables:
    • _action_name
    • _action_index
    • _action_comment
    • _previous_action_start_time
  3. Other variables:
    • location
    • _event_name
    • _event_args

The location values are: library, main, card and cardb. The value depends in which library/device view you are currently in.
The selection count is the number of books currently selected in books view.
The _chain_iteration can be used in conjunction with _chain_name, to prevent clashes with variables set by previous runs of the same chain.

The chain variables action, like other actions, can be used multiple times during the lifetime of the chain to either set new variables, or re-set previously set variable to new values.

All the variables, can be accessed in templates, by using the globals function like the example below

Code:
program:
    n = globals(_chain_name);
    i = globals(_chain_iteration);
    strcat(n, '_', i)
Notes:
  • Chain variables are only defined when you run the chain. While you are configuring the chain, if you try to access a user defined variable in one if you actions like (e.g. variable defined in a preceding action), like below"
    Code:
    program:
        v = globals(myvar)
    v will be assigned an empty string, since the chain has not run yet and the variable is undefined. This is fine for string variables. But if your variable is being used in mathematical operations like below:

    Code:
    program:
        v = 2 / globals(myvar)
    In that case, the template will return an error because an empty string cannot be used in mathematical operations. The solution is to provide a valid default value for the variable:

    Code:
    program:
        v = 2 / globals(myvar=1)

Last edited by capink; 01-14-2022 at 12:59 PM.
capink is offline   Reply With Quote