Author Topic: boilerplate & messagebox syntax  (Read 930 times)

trebor

  • Newbie
  • *
  • Posts: 7
  • Karma: 0
  • Scribus User
boilerplate & messagebox syntax
« on: September 04, 2015, 10:10:32 am »
I have had a few of the scripts shipped with the scribus 1.5.* versions freeze Scibus because the message box has no buttons to dismiss it.

So I looked at some of the other sample scripts and was able to modify the 'ExtractText.py' script to avoid this. I then decided I should put it into the boiler plate... see the sample script  files: if you are on Linux & using the 15.0 version from the Scribus Friends ppa they should be here...
/usr/share/scribus-ng/samples/ExtractText.py
/usr/share/scribus-ng/samples/boilerplate.py

My changed script...
Code: [Select]
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""

(C)2006.03.04 Gregory Pittman

(C)2008.02.28 Petr Vanek - fileDialog replaces valueDialog

this version 2008.02.28

This program is free software; you can redistribute it and/or modify
it under the terms of the  GPL, v2 (GNU General Public License as published by
the Free Software Foundation, version 2 of the License), or any later version.
See the Scribus Copyright page in the Help Browser for further informaton
about GPL, v2.

SYNOPSIS

This script takes the current document and extracts all the text from text frames,
and also gets the pathnames to all images. This is then saved to a file named
by the user.

REQUIREMENTS

You must run from Scribus and must have a file open.

USAGE

Start the script. A file dialog appears for the name of the file
to save to. The above information is saved to the file.

"""
# Craig Bradney, Scribus Team
# 10/3/08: Added to Scribus 1.3.3.12svn distribution "as was" from Scribus wiki for bug #6826, script is GPLd
"""
# 20150904 - trebor Modified to fix the scribus.messageBox & incorporated into the Scribus 1.5 boiler plate script template
/usr/share/scribus-ng/samples/boilerplate.py
"""
import sys

try:
    # Please do not use 'from scribus import *' . If you must use a 'from import',
    # Do so _after_ the 'import scribus' and only import the names you need, such
    # as commonly used constants.
    import scribus
except ImportError,err:
    print "This Python script is written for the Scribus scripting interface."
    print "It can only be run from within Scribus."
    sys.exit(1)

#########################
# YOUR IMPORTS GO HERE  #
#########################

def main(argv):
    """"""
def exportText(textfile):
    page = 1
    pagenum = scribus.pageCount()
    T = []
    content = []
    while (page <= pagenum):
        scribus.gotoPage(page)
        d = scribus.getPageItems()
        strpage = str(page)
        T.append('Page '+ strpage + '\n\n')
        for item in d:
            if (item[1] == 4):
                contents = scribus.getAllText(item[0])
                if (contents in content):
                    contents = 'Duplication, perhaps linked-to frame'
                T.append(item[0]+': '+ contents + '\n\n')
                content.append(contents)
            elif (item[1] == 2):
                imgname = scribus.getImageFile(item[0])
                T.append(item[0]+': ' + imgname + '\n')
        page += 1
        T.append('\n')
    output_file = open(textfile,'w')
    output_file.writelines(T)
    output_file.close()
    endmessage = textfile + ' was created'
    # See help - /usr/share/doc/scribus-ng/en/scripterapi-dialogs.html
    # Don't know why the scribus. syntax is required
    scribus.messageBox("Finished", endmessage, scribus.ICON_INFORMATION, scibus.BUTTON_OK)

if scribus.haveDoc():
    textfile = scribus.fileDialog('Enter name of file to save to', \
                                  filter='Text Files (*.txt);;All Files (*)')
    try:
        if textfile == '':
            raise Exception
        exportText(textfile)
    except Exception, e:
        print e

else:
    scribus.messageBox('Export Error', 'You need a Document open, and a frame selected.', scribus.ICON_WARNING, scribus.BUTTON_OK)

def main_wrapper(argv):
    """The main_wrapper() function disables redrawing, sets a sensible generic
    status bar message, and optionally sets up the progress bar. It then runs
    the main() function. Once everything finishes it cleans up after the main()
    function, making sure everything is sane before the script terminates."""
    try:
        scribus.statusMessage("Running script...")
        scribus.progressReset()
        main(argv)
    finally:
        # Exit neatly even if the script terminated with an exception,
        # so we leave the progress bar and status bar blank and make sure
        # drawing is enabled.
        if scribus.haveDoc():
            scribus.setRedraw(True)
        scribus.statusMessage("")
        scribus.progressReset()

# This code detects if the script is being run as a script, or imported as a module.
# It only runs main() if being run as a script. This permits you to import your script
# and control it manually for debugging.
if __name__ == '__main__':
    main_wrapper(sys.argv)

Some scripts in the samples work with 'messagebox' (not 'scribus.messagebox') format..
No idea why - I couldn't get it to work this way in the extracttext version.
This from the word count sample file
Code: [Select]
messageBox(TITLE, "Can't count words in a non-text frame", ICON_INFORMATION)
Hope you find this useful. Thanks Scribus team! :-*

 

palmate