CopyObject() and pasteObject(): Difference between revisions

From Scribus Wiki
Jump to navigation Jump to search
Line 10: Line 10:
==paste2all.py==
==paste2all.py==
<syntaxhighlight lang="python">
<syntaxhighlight lang="python">
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
# File: paste2all.py
# File: paste2all.py
# © 2012.01.29 Gregory Pittman
# © 2012.01.29 Gregory Pittman
# This version 2014.10.11
# This program is free software; you can redistribute it and/or modify
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# (at your option) any later version.
"""
"""
USAGE
USAGE
You must have a document open. Select a single object. Run the script, which asks whether you want
You must have a document open. Select a single object. Run the script, which asks whether you want


all, odd, or even pages to get a copy of the selected object (no copy is made to the original
all, odd, or even pages to get a copy of the selected object (no copy is made to the original


page of the object). Any other input is ignored. When pasted, the copies go to the same page
page of the object). You may also specify the nth page of a recurring number of pages. For example,
 
if you specify 2/3, the object will be copied to the second of every 3 pages.
 
Any other input is ignored. When pasted, the copies go to the same page


coordinates of the original.
coordinates of the original.


The script will also work with a group, but if you select more than one item without grouping,  
The script does not work with groups, and in fact Scribus will hang and then crash if you have
 
selected a group. If you select more than one item without grouping, only one of those
 
will be copied and pasted.


an error is generated. This isn't completely necessary, but otherwise only one object would be


copied and pasted.


"""
"""
import scribus
import scribus
if scribus.haveDoc():
if scribus.haveDoc():
     if scribus.selectionCount() == 0:
     if scribus.selectionCount() == 0:
         scribus.messageBox('Scribus - Usage Error',
         scribus.messageBox('Scribus - Usage Error',
Line 45: Line 52:
         sys.exit(2)
         sys.exit(2)
     if scribus.selectionCount() > 1:
     if scribus.selectionCount() > 1:
        scribus.messageBox('Scribus - Usage Error', "You have more than one object selected.\nPlease select only one object and try again.", scribus.ICON_WARNING, scribus.BUTTON_OK)
scribus.messageBox('Scribus - Usage Error', "You have more than one object selected.\nPlease select one text frame and try again.", scribus.ICON_WARNING, scribus.BUTTON_OK)
        sys.exit(2)
sys.exit(2)
     paste2 = scribus.valueDialog('Paste to...',"Paste where?\n(all, odd, even)","all")
     paste2 = scribus.valueDialog('Paste to...',"Paste where?\n(all, odd, even)","all")
     selframe = scribus.getSelectedObject()
     selframe = scribus.getSelectedObject()
Line 53: Line 60:
     scribus.copyObject(selframe)
     scribus.copyObject(selframe)
     if (paste2 == 'all'):
     if (paste2 == 'all'):
        i = 1
i = 1
        while (i <= pages):
jump = 1
            if (i != currpage):
                scribus.gotoPage(i)
                scribus.pasteObject(selframe)
            i=i+1
     elif (paste2 == 'odd'):
     elif (paste2 == 'odd'):
         i = 1
         i = 1
         while (i <= pages):
         jump = 2
            if (i != currpage):
                scribus.gotoPage(i)
                scribus.pasteObject(selframe)
            i=i+2
     elif (paste2 == 'even'):
     elif (paste2 == 'even'):
         i = 2
         i = 2
         while (i <= pages):
         jump = 2
            if (i != currpage):
    elif ('/' in paste2):
                scribus.gotoPage(i)
        pattern = paste2.split('/')
                scribus.pasteObject(selframe)
        i = int(pattern[0])
            i=i+2
        jump = int(pattern[1])
       
    while (i <= pages):
if (i != currpage):
    scribus.gotoPage(i)
    scribus.pasteObject(selframe)
i = i + jump
 
     scribus.setRedraw(1)
     scribus.setRedraw(1)
     scribus.docChanged(1)
     scribus.docChanged(1)
     scribus.messageBox("Finished", "Done",icon=0,button1=1)
     scribus.messageBox("Finished", "Done",icon=0,button1=1)
else:
else:
     scribus.messageBox('Usage Error', 'You need a Document open', icon=0, button1=1)
     scribus.messageBox('Usage Error', 'You need a Document open', icon=0, button1=1)

Revision as of 12:30, 11 October 2014

This article is part of the Scripts series.

This is an introduction to a couple of new Scripter commands. What these allow for is for the copying of selected objects (individually) to all or a number of pages. You might consider this as a workaround for the issue some have brought up regarding Master Pages, so that something occurring on a Master Page might be editable on individual pages.

These commands are now available in 1.5.0svn. There has long been a command duplicateObject(), which makes one copy of a selected object, right on top of the object. Inside the workings of the command, what happens is that the object is copied to the clipboard, then immediately pasted.

To create these two new commands, these two operations inside of duplicateObject() were simply split up and separate commands created. What this allows you to do is to first copy an object, then paste it on some other page. Just as with copy and paste in the main Scribus window, an object pasted from the clipboard is assigned the same page coordinates as the original.

The first script below uses these commands to copy a selected object to all pages, odd pages, or even pages. If you specify anything else, nothing happens. The second script asks for a list of page numbers to paste your object to.

paste2all.py

# -*- coding: utf-8 -*-
# File: paste2all.py
# © 2012.01.29 Gregory Pittman
# This version 2014.10.11
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
"""
USAGE
You must have a document open. Select a single object. Run the script, which asks whether you want

all, odd, or even pages to get a copy of the selected object (no copy is made to the original

page of the object). You may also specify the nth page of a recurring number of pages. For example,

if you specify 2/3, the object will be copied to the second of every 3 pages.

Any other input is ignored. When pasted, the copies go to the same page

coordinates of the original.

The script does not work with groups, and in fact Scribus will hang and then crash if you have

selected a group. If you select more than one item without grouping, only one of those

will be copied and pasted.



"""
import scribus

if scribus.haveDoc():


    if scribus.selectionCount() == 0:
        scribus.messageBox('Scribus - Usage Error',
                           "There is no object selected.\nPlease try again.",
                           scribus.ICON_WARNING, scribus.BUTTON_OK)
        sys.exit(2)
    if scribus.selectionCount() > 1:
	scribus.messageBox('Scribus - Usage Error', "You have more than one object selected.\nPlease select one text frame and try again.", scribus.ICON_WARNING, scribus.BUTTON_OK)
	sys.exit(2)
    paste2 = scribus.valueDialog('Paste to...',"Paste where?\n(all, odd, even)","all")
    selframe = scribus.getSelectedObject()
    pages = scribus.pageCount()
    currpage = scribus.currentPage()
    scribus.copyObject(selframe)
    if (paste2 == 'all'):
	i = 1
	jump = 1
    elif (paste2 == 'odd'):
        i = 1
        jump = 2
    elif (paste2 == 'even'):
        i = 2
        jump = 2
    elif ('/' in paste2):
        pattern = paste2.split('/')
        i = int(pattern[0])
        jump = int(pattern[1])
        
    while (i <= pages):
	if (i != currpage):
	    scribus.gotoPage(i)
	    scribus.pasteObject(selframe)
	i = i + jump

    scribus.setRedraw(1)
    scribus.docChanged(1)
    scribus.messageBox("Finished", "Done",icon=0,button1=1)

else:
    scribus.messageBox('Usage Error', 'You need a Document open', icon=0, button1=1)
    sys.exit(2)

paste2pagelist.py

This second script allows you to enter a list of pages by page number, separated by space.

There is a check to make sure all of your numbers are within the correct range, but the list doesn't have to be in any particular order.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# File: paste2pagelist.py
# © 2012.03.22 Gregory Pittman
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

"""
USAGE

You must have a document open. Select a single object. Run the script, which asks for a list

of page numbers -- do not use commas to separate, just whitespace.

When pasted, the copies go to the same page coordinates of the original.

The script will work with a group. If you select more than one item without grouping, 

an error is generated.

"""

import scribus

if scribus.haveDoc():
    if scribus.selectionCount() == 0:
        scribus.messageBox('Scribus - Usage Error',
                           "There is no object selected.\nPlease try again.",
                           scribus.ICON_WARNING, scribus.BUTTON_OK)
        sys.exit(2)
    if scribus.selectionCount() > 1:
        scribus.messageBox('Scribus - Usage Error', "You have more than one object selected.
                                 \nPlease select one object and try again.", scribus.ICON_WARNING, scribus.BUTTON_OK)
        sys.exit(2)
    pagelist = scribus.valueDialog('Paste to...',"Paste to which pages?
                                 \n(page numbers, separated by white space)","1")
    pageslist = pagelist.split()
    selframe = scribus.getSelectedObject()
    pages = scribus.pageCount()
    for p in pageslist:
        p_no = int(p)
        if ((p_no > pages) or (p_no < 1)):
            scribus.messageBox('OOPS!', "You have a page number outside the range of pages in your document",
                      scribus.ICON_WARNING, scribus.BUTTON_OK)
            sys.exit(2)
    scribus.copyObject(selframe)
    for p in pageslist:
        p_no = int(p)
        scribus.gotoPage(p_no)
        scribus.pasteObject(selframe)
    scribus.setRedraw(1)
    scribus.docChanged(1)
    scribus.messageBox("Finished", "Done",icon=0,button1=1)
else:
    scribus.messageBox('Usage Error', 'You need a Document open', icon=0, button1=1)
    sys.exit(2)

invisibleImages.py

This script comes from an idea to be able to transfer all images in a document to their own layer, so that you might make the layer invisible and/or not print.

A modification to consider would be to check to see if there already is a layer named "Ghostlayer", and if so, skip the creation.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# File: invisibleImages.py
# © 2014.07.06 Gregory Pittman
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
"""
USAGE

You must have a document open.

This script moves all images to a new layer called "Ghostlayer",
the idea being that you can show/hide, print-export/not as desired.

"""
import scribus

if scribus.haveDoc():

  fantome = "Ghostlayer"
  scribus.createLayer(fantome)
  working = scribus.getActiveLayer()
  page = 1
  pagenum = scribus.pageCount()
  while (page <= pagenum):
    scribus.gotoPage(page)
    scribus.setActiveLayer(working)  # maybe not necessary?  
    pageitems = scribus.getPageItems()

    for item in pageitems:
      if (item[1] == 2):
	imagebox = item[0]
	scribus.selectObject(imagebox)
	scribus.copyObject(imagebox)
	scribus.setActiveLayer(fantome)
	scribus.pasteObject(imagebox)
	scribus.deleteObject(imagebox)
	scribus.setActiveLayer(working)
    page += 1
  scribus.setLayerPrintable(fantome, 0)  # comment this out to do manually later
  scribus.setLayerVisible(fantome, 0)    # comment this out to do manually later
  scribus.setRedraw(1)
  scribus.docChanged(1)
  scribus.messageBox("Finished", "That should do it!",icon=0,button1=1)

else:
    scribus.messageBox('Usage Error', 'You need a Document open', icon=0, button1=1)
    sys.exit(2)