Troubleshooting Scripts: Difference between revisions

From Scribus Wiki
Jump to navigation Jump to search
Line 52: Line 52:
nrimages = int(nrimages)
nrimages = int(nrimages)
</syntaxhighlight>
</syntaxhighlight>
==Error-checking Errors==

Revision as of 14:30, 6 November 2016

This article is part of the Scripts series.

You are likely to find that a number of the example scripts on the wiki don't work, either generating errors, or perhaps nothing happens at all. This is because the acceptable syntax has changed over the years. On this page are some of the common problems I have encountered, often with my own scripts, which at one time worked, and now do not.

Invoking Scribus Commands

There are two ways to include the Scribus Scripter commands:

from scribus import *

and

import scribus

The first of these pulls in all of the Scripter commands into the interpreter, and is not the advocated method, since most scripts use a handful of Scribus Python commands. The second one only looks for a Scribus method when it is called, but it's important to tell Python that this is a Scribus command, so for example,

import scribus

if selectionCount() != 2:
    messageBox('Selection Count', "You must have 2 image frames selected",
                       ICON_WARNING, BUTTON_OK)

This is going to fail, and may even "hang" Scribus. This is because selectionCount(), messageBox() are Scribus commands, and ICON_WARNING and BUTTON_OK are Scribus-specific constants. So you need to prepend scribus. before each of these elements, like so:

import scribus

if scribus.selectionCount() != 2:
    scribus.messageBox('Selection Count', "You must have 2 image frames selected",
                       scribus.ICON_WARNING, scribus.BUTTON_OK)

Changes in Constants

This brings up another problem with some older scripts. In a messageBox() command like above, you used to be able to use a number for these constants, like

    scribus.messageBox('Selection Count', "You must have 2 image frames selected",
                       icon1=0, button1=1)

Trying this now will result in your script hanging and locking up Scribus, since there will be no OK button to close the messageBox().

I wrote some scripts in which I thought I was clever, using a messageBox() for input like this:

nrimages = scribus.messageBox('Pictures','- US Letter Paper -\n Four Images per Page?\nYes = 4, No = 6',button1=scribus.BUTTON_YES,button2=scribus.BUTTON_NO)

This worked at the time because clicking Yes would return a value of 3, and clicking No returned 4. Now two 5-digit numbers are returned, which wreaked havoc with the subsequent syntax based on the value of nrimages. So better to go with a valueDialog() method.

while ((nrimages != '4') and (nrimages != '6')):
    nrimages = scribus.valueDialog('Pictures','- US Letter Paper -\n Images per Page?\nOnly valid entries are 4 or 6','4')
nrimages = int(nrimages)

Error-checking Errors