valid-page-number.diff (1,526 bytes)
diff --git a/scribus/plugins/scriptplugin/cmdutil.cpp b/scribus/plugins/scriptplugin/cmdutil.cpp
index 7e1b80050..dcf8a208c 100644
--- a/scribus/plugins/scriptplugin/cmdutil.cpp
+++ b/scribus/plugins/scriptplugin/cmdutil.cpp
@@ -162,6 +162,27 @@ bool checkHaveDocument()
return false;
}
+bool checkValidPageNumber(int page)
+{
+ const uint numPages = ScCore->primaryMainWindow()->doc->Pages->count();
+ if (page < 0 || page >= numPages)
+ {
+ PyErr_SetString(PyExc_ValueError, QObject::tr("%1 is not a valid page number.", "python error").arg(page).toLocal8Bit().constData());
+ return false;
+ }
+ return true;
+}
QStringList getSelectedItemsByName()
{
/*
diff --git a/scribus/plugins/scriptplugin/cmdutil.h b/scribus/plugins/scriptplugin/cmdutil.h
index 33f785dc2..099f7939c 100644
--- a/scribus/plugins/scriptplugin/cmdutil.h
+++ b/scribus/plugins/scriptplugin/cmdutil.h
@@ -54,6 +54,18 @@ PageItem* getPageItemByName(const QString& name);
// 2004-10-27 Craig Ringer see cmdutil.cpp for description
bool checkHaveDocument();
+/*!
+ * @brief Returns true if the page number is between 0 and the number of pages.
+ *
+ * If the page number is smaller than 0 or bigger than the number of pages
+ * a Python exception is set, before returning false.
+ *
+ * The caller is required to check the returned value and return nullptr on false.
+ *
+ * @author ale rimoldi
+ */
+bool checkValidPageNumber(int page);
+
// 2004-11-12 Craig Ringer see cmdutil.cpp for description
bool ItemExists(const QString& name);