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>
Here's a bonus troubleshooting feature in the above example. Basically, what this sequence is doing is trying ensure that the user enters only a 4 or 6 for the input, so if he puts in anything else, the '''while''' just keeps cranking the '''valueDialog()''' over and over. When I first wrote it, I had the logic like this:
<syntaxhighlight lang="python">
while ((nrimages != '4') or (nrimages != '6')):
</syntaxhighlight>
What this '''or''' is doing in there is causing the '''while''' command to continue until the value of '''nrimages''' is 4 AND 6 at the same time! An example of a never-ending loop.
==Error-checking Errors==
==Error-checking Errors==
Here is something you may see at the beginning of many scripts:
Here is something you may see at the beginning of many scripts:

Revision as of 14:46, 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)

Here's a bonus troubleshooting feature in the above example. Basically, what this sequence is doing is trying ensure that the user enters only a 4 or 6 for the input, so if he puts in anything else, the while just keeps cranking the valueDialog() over and over. When I first wrote it, I had the logic like this:

while ((nrimages != '4') or (nrimages != '6')):

What this or is doing in there is causing the while command to continue until the value of nrimages is 4 AND 6 at the same time! An example of a never-ending loop.

Error-checking Errors

Here is something you may see at the beginning of many scripts:

try:
    import scribus
except ImportError:
    print "Unable to import the 'scribus' module. This script will only run within"
    print "the Python interpreter embedded in Scribus. Try Script->Execute Script."
    sys.exit(1)

The print command is going to send output to the console, since it would be invoked if you tried to run this script there, outside of Scribus. But look at this sequence, coming right after the above error check:

try:
    from PIL import Image
except ImportError:
    print "Unable to import the Python Imaging Library module."
    sys.exit(1)

If you get to this point, you have already checked to make sure this is being run inside of Scribus, but when this check for PIL fails, it's printing the error message to the console, so from the user's point-of-view, the script runs but nothing happens. So inside of Scribus you need to use a messageBox() instead:

try:
    from PIL import Image
except ImportError:
    scribus.messageBox("PIL","Unable to import the Python Imaging Library module.",scribus.ICON_WARNING,scribus.BUTTON_OK)
    sys.exit(1)