Scriptmaker.py

From Scribus Wiki
Jump to: navigation, search
This article is part of the Scripts series.

Here is the results of a mental exercise with the starting idea of writing a Python script that will make the beginning of a script for Scribus. There is also on the wiki boilerplate.py, which would be something you copy and paste. With scriptmaker.py, you run it on the commandline and it creates the file for you once you give it a name. Note that it automatically adds your filename as a comment in the third line of the file.

scriptmaker.py

#!/usr/bin/env python
# -*- coding: utf-8  -*-
# scriptmaker.py
# creates a new filename.py with several basic elements for Scribus scripts

filename = raw_input("What will the name of the new script be? ") # raw_input for python 2.7

if ((filename[-3]+filename[-2]+filename[-1]) != ".py"): # if you didn't append your name with ".py", the script does it for you
    filename = filename + ".py"

boiler = ["#!/usr/bin/env python","# -*- coding: utf-8 -*-", "# " + filename, "\n","import sys","\n","try:","\timport scribus","except ImportError:","\tprint 'Unable to import the scribus module. This script will only run within'","\tprint 'the Python interpreter embedded in Scribus. Try Script->Execute Script.'","\tsys.exit(1)", "\n","if not  scribus.haveDoc():","\tscribus.messageBox('Error','You must have a document open',scribus.ICON_WARNING,scribus.BUTTON_OK)","\tsys.exit(2)","\n", "if scribus.selectionCount() == 0:","\tscribus.messageBox('Scribus - Script Error','There is no object selected. Please select a text frame and try again.',scribus.ICON_WARNING, scribus.BUTTON_OK)","\tsys.exit(2)", "\n", "if scribus.selectionCount() > 1:  # get rid of or modify this section if you want more than one object selected", "\tscribus.messageBox('Scribus - Script Error','You have more than one object selected. Please select one text frame and try again.', scribus.ICON_WARNING, scribus.BUTTON_OK)","\tsys.exit(2)","\n"]

file_object = open(filename, 'w')
for i in boiler:
    file_object.write(i + "\n")

print "your script " + filename + " was created."

Example output from scriptmaker.py - testing1.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# testing1.py


import sys


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)


if not  scribus.haveDoc():
	scribus.messageBox('Error','You must have a document open',scribus.ICON_WARNING,scribus.BUTTON_OK)
	sys.exit(2)


if scribus.selectionCount() == 0:
	scribus.messageBox('Scribus - Script Error','There is no object selected. Please select a text frame and try again.',scribus.ICON_WARNING, scribus.BUTTON_OK)
	sys.exit(2)


if scribus.selectionCount() > 1:  # get rid of or modify this section if you want more than one object selected
	scribus.messageBox('Scribus - Script Error','You have more than one object selected. Please select one text frame and try again.', scribus.ICON_WARNING, scribus.BUTTON_OK)
	sys.exit(2)