Scripter/Snippet/Main: Difference between revisions

From Scribus Wiki
Jump to navigation Jump to search
(New page: <pre> #!/usr/bin/env python # -*- coding: utf-8 -*- from __future__ import division import sys try: import scribus except ImportError,err: print "This Python script is written for...)
 
No edit summary
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{Scripting Index}}
See also the <kbd>[[boilerplate.py]]</kbd> script in the Scribus documentation.
<pre>
<pre>
#!/usr/bin/env python
#!/usr/bin/env python

Latest revision as of 08:22, 3 April 2012

This article is part of the Scripts series.

See also the boilerplate.py script in the Scribus documentation.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import division
import sys

try:
    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)


def main(argv):
    """This is a documentation string. Write a description of what your code
    does here. You should generally put documentation strings ("docstrings")
    on all your Python functions."""
    #########################
    #  YOUR CODE GOES HERE  #
    #########################

    # ... some code...

    scribus.progressTotal(n)

    scribus.setRedraw(False)

    # some code which manipulates frame and you don't want scribus to update while doing it

    i=i+1
    scribus.progressSet(i)

    scribus.progressReset() 

    # let scribus redraw again

    scribus.setRedraw(True)
    scribus.progressReset()



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)