Troubleshooting Scripts: Difference between revisions

From Scribus Wiki
Jump to navigation Jump to search
(Created page with "{{Scripting Index}} 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 be...")
 
Line 3: Line 3:


==Invoking Scribus Commands==
==Invoking Scribus Commands==
There are two ways to include the Scribus Scripter commands:
<syntaxhighlight lang="python">
from scribus import *
</syntaxhighlight>
and
<syntaxhighlight lang="python">
import scribus
</syntaxhighlight>
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,
<syntaxhighlight lang="python">
import scribus
if selectionCount() != 2:
    messageBox('Selection Count', "You must have 2 image frames selected",
                      ICON_WARNING, BUTTON_OK)
</syntaxhighlight>
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 commands, like so:
<syntaxhighlight lang="python">
import scribus
if scribus.selectionCount() != 2:
    scribus.messageBox('Selection Count', "You must have 2 image frames selected",
                      scribus.ICON_WARNING, scribus.BUTTON_OK)
</syntaxhighlight>

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