"ClipStack2" by NickDMax

Rating: (0 votes)

Report this Script

Name: ClipStack2
Uploaded By: NickDMax
Date Created: May 4, 2009 12:59 p.m.
Date Modified: May 4, 2009 12:59 p.m.
Description: A slightly more advanced version of ClipStack that uses the windows clipboard as the top item of the stack. Requires download and install of the Python Win32 extension. Note I prefer the older version -- this one almost needs a UI to keep track of what is in the ClipStack. -- This one is nice so long as you don't start interacting with outside programs.
Num Views: 1055
Num Downloads: 338

Contents

Download this script

###############################################################################
## ClipStack2.py -- A clipboard stack allowing you to copy items into a stack
## and then paste them back in a FILO way. So the first item copied to the stack
## is the last item pasted from it.
##  Copy item #1, Copy item #2, Copy item #3
##  Paste #3, #2, #1
## 
## Copy/Cut
## The selected item copied is placed onto the regular OS clipboard 
##   The last clipboard item (if text) is pushed onto the stack.
##
## Paste
## The current clipboard (if text) is pasted then the
##   top item is poped into the clipboard. 
##
## Warning: Since this uses the clipboard as the top item of the stack it
## is possible to loose items when mixing between this application and other
## windows since they do not push items onto the stack.
## For this reason there is a push command provided to force pushing the current
## contents of the clipboard onto the stack. So if you are going to copy from
## another program, you may first want to push the current clipboard onto the
## stack.
## 
## By: NickDMax

import pn
import scintilla
from pypn.decorators import script
import win32clipboard as winclip
import win32con

class PNClipStack2:
    """ Maintains a stack of text to paste  """
    def __init__(self):
        """ initialize inner data:
            stack -- the internal list used to store clipboard items """
        self.stack = []

    def push(self, text):
        self.stack.append(text)

    def pop(self):
        retValue = ''
        if len(self.stack) > 0:
            retValue = self.stack.pop()
        return retValue

    def clear(self):
        self.stack = []

    def getSize(self):
        return len(self.stack)

ClipStack2 = PNClipStack2()

@script('Stack Count', 'ClipStack2')
def clstk2Count():
    """ Prints out the size of the current ClipStack """
    pn.AddOutput('ClipStack Size: ' + str(ClipStack2.getSize()) + '\n')

@script('Push', 'ClipStack2')
def clstk2Push():
    """ Adds the current clipboard to the ClipStack """
    doc = pn.CurrentDoc()
    if winclip.IsClipboardFormatAvailable(win32con.CF_TEXT):
       winclip.OpenClipboard()
       cbdata = winclip.GetClipboardData(win32con.CF_TEXT)
       winclip.CloseClipboard()
       if cbdata is not None:
           ClipStack2.push(cbdata)


@script('Copy', 'ClipStack2')
def clstk2Copy():
    """ Adds the current selection to the ClipStack """
    doc = pn.CurrentDoc()
    if doc is not None:     #Lets try not to crash our script
        editor = scintilla.Scintilla(doc)
        start = editor.SelectionStart
        end = editor.SelectionEnd
        if (start == end):  #nothing is selected lets try to grab the current word.
            start = editor.WordStartPosition(start, True)
            end = editor.WordEndPosition(end, True)
        text = None
        if (start != end):
            text = editor.GetTextRange(start, end)
        if text is not None:
            clstk2Push()
            editor.CopyText(len(text), text)
 
@script('Cut', 'ClipStack2')
def clstk2Cut():
    """ Adds the current selection to the ClipStack 
        -- cuts it from document"""
    doc = pn.CurrentDoc()
    if doc is not None:     #Lets try not to crash our script
        editor = scintilla.Scintilla(doc)
        start = editor.SelectionStart
        end = editor.SelectionEnd
        if (start == end):  #nothing is selected lets try to grab the current word.
            start = editor.WordStartPosition(start, True)
            end = editor.WordEndPosition(end, True)
        text = None
        if (start != end):
            text = editor.GetTextRange(start, end)
        if text is not None:
            clstk2Push()
            editor.Cut()
 

@script('Paste', 'ClipStack2')
def clstk2Paste():
    """ Pastes the top item from the ClipStack into the document """
    doc = pn.CurrentDoc()
    if doc is not None:     #Lets try not to crash our script
        editor = scintilla.Scintilla(doc)
        if winclip.IsClipboardFormatAvailable(win32con.CF_TEXT):
            editor.Paste()
        else:
            pn.AddOutput("Clipboard not text.\n")
        text = ClipStack2.pop()
        editor.CopyText(len(text), text)
 
@script('Clear', 'ClipStack2')
def clstk2Cear():
    """ Clears the current ClipStack """
    ClipStack2.clear()
    winclip.OpenClipboard()
    winclip.EmptyClipboard()
    winclip.CloseClipboard()

Comments: