aoloe / scribus-script-repository

Repository with python scripts from the Scribus community
36 stars 11 forks source link

check tdb's script for creating captions #18

Open aoloe opened 9 years ago

aoloe commented 9 years ago

http://www.tdb.fi/~tdb/tmp/image-frame.py.txt

import scribus

caption_line_height = 3.5
padding = 3
pt_per_mm = 72/25.4
corner_radius = -2*pt_per_mm
background_color = "White"
border_width = 0.25*pt_per_mm
border_color = "Black"

# Find the selected image and text frames
image = None
caption = None
i = 0
while 1:
    obj_name = scribus.getSelectedObject(i)
    if not obj_name:
        break
    obj_type = scribus.getObjectType(obj_name)
    if obj_type=="ImageFrame":
        image = obj_name
    elif obj_type=="TextFrame":
        caption = obj_name
    i += 1

if image:
    image_size = scribus.getSize(image)
    image_pos = scribus.getPosition(image)
    frame_height = image_size[1]+padding*2
    # If a text frame was present, match its size and position with the image
    if caption:
        caption_size = scribus.getSize(caption)
        caption_height = caption_line_height*int(caption_size[1]/caption_line_height+0.1)
        frame_height += caption_height+0.5
        scribus.sizeObject(image_size[0], caption_height, caption)
        scribus.moveObjectAbs(image_pos[0], image_pos[1]+image_size[1]+1, caption)

    # Create the frame rectangle
    frame = scribus.createRect(image_pos[0]-padding, image_pos[1]-padding, image_size[0]+padding*2, frame_height)
    scribus.setFillColor(background_color, frame)
    scribus.setCornerRadius(corner_radius, frame)
    scribus.setLineColor(border_color, frame)
    scribus.setLineWidth(border_width, frame)

    # Move the frame rectangle behind the other objects
    scribus.deselectAll()
    scribus.selectObject(frame)
    scribus.moveSelectionToBack()

    # Create group of all the objects.  It works better by selecting the
    # objects, as the group is then left selected after the operation.
    scribus.selectObject(image)
    if caption:
        scribus.selectObject(caption)
    scribus.groupObjects()
luzpaz commented 9 years ago

I get this error:

'Scribus Python Console\n\nThis is a standard Python console with some \nknown limitations. Please consult the Scribus \nScripter documentation for futher information. '
Traceback (most recent call last):
  File "<console>", line 2, in <module>
TypeError: an integer is required
  File "<console>", line 1
    frame = scribus.createRect(image_pos[0]-padding, image_pos[1]-padding, image_size[0]+padding*2, frame_height)
    ^
IndentationError: unexpected indent
  File "<console>", line 1
    scribus.setFillColor(background_color, frame)
    ^
IndentationError: unexpected indent
  File "<console>", line 1
    scribus.setCornerRadius(corner_radius, frame)
    ^
IndentationError: unexpected indent
  File "<console>", line 1
    scribus.setLineColor(border_color, frame)
    ^
IndentationError: unexpected indent
  File "<console>", line 1
    scribus.setLineWidth(border_width, frame)
    ^
IndentationError: unexpected indent
  File "<console>", line 1
    scribus.deselectAll()
    ^
IndentationError: unexpected indent
  File "<console>", line 1
    scribus.selectObject(frame)
    ^
IndentationError: unexpected indent
  File "<console>", line 1
    scribus.moveSelectionToBack()
    ^
IndentationError: unexpected indent
  File "<console>", line 1
    scribus.selectObject(image)
    ^
IndentationError: unexpected indent
  File "<console>", line 1
    if caption:
    ^
IndentationError: unexpected indent
  File "<console>", line 1
    scribus.selectObject(caption)
    ^
IndentationError: unexpected indent
  File "<console>", line 1
    scribus.groupObjects()
    ^
IndentationError: unexpected indent
aoloe commented 9 years ago

@luzpaz you should save the script in a text file and run the file from scribus...

luzpaz commented 9 years ago

@aoloe Ok.. thanks. What about the patch at the end of the tp://www.tdb.fi/~tdb/tmp/image-frame.py.txt

# Requires the following patch to Scribus:
"""
--- scribus-1.4.2.dfsg.3+r18267.orig/scribus/plugins/scriptplugin/cmdsetprop.cpp
+++ scribus-1.4.2.dfsg.3+r18267/scribus/plugins/scriptplugin/cmdsetprop.cpp
@@ -340,16 +340,11 @@ PyObject *scribus_setlinestyle(PyObject*
 PyObject *scribus_setcornerrad(PyObject* /* self */, PyObject* args)
 {
    char *Name = const_cast<char*>("");
-   int w;
-   if (!PyArg_ParseTuple(args, "i|es", &w, "utf-8", &Name))
+   double w;
+   if (!PyArg_ParseTuple(args, "d|es", &w, "utf-8", &Name))
        return NULL;
    if(!checkHaveDocument())
        return NULL;
-   if (w < 0)
-   {
-       PyErr_SetString(PyExc_ValueError, QObject::tr("Corner radius must be a positive number.","python error").toLocal8Bit().constData());
-       return NULL;
-   }
    PageItem *currItem = GetUniqueItem(QString::fromUtf8(Name));
    if (currItem == NULL)
        return NULL;
"""
luzpaz commented 9 years ago
Traceback (most recent call last):
  File "<string>", line 8, in <module>
  File "/Users/sphenoid/Downloads/FFbrowser/image-frame.py", line 41, in <module>
    scribus.setCornerRadius(corner_radius, frame)
TypeError: integer argument expected, got float
luzpaz commented 9 years ago

Does the script need to be run when scribus is in certain condition on the canvas?