Scriptmaker.py: Difference between revisions

From Scribus Wiki
Jump to navigation Jump to search
Line 23: Line 23:


print "your script " + filename + " was created."
print "your script " + filename + " was created."
</syntaxhighlight>
===Example output from scriptmaker.py - testing1.py===
<syntaxhighlight lang=python>
#!/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)
</syntaxhighlight>
</syntaxhighlight>

Revision as of 17:09, 18 November 2016

Category:Scripting Index

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.

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