SHARE
TWEET

Untitled

a guest Nov 14th, 2011 974 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/python
  2.  
  3. def pbcopy(s):
  4.     "Copy string argument to clipboard"
  5.     board = AppKit.NSPasteboard.generalPasteboard()
  6.     board.declareTypes_owner_([AppKit.NSStringPboardType], None)
  7.     newStr = Foundation.NSString.stringWithString_(s)
  8.     newData = \
  9.         newStr.nsstring().dataUsingEncoding_(Foundation.NSUTF8StringEncoding)
  10.     board.setData_forType_(newData, AppKit.NSStringPboardType)
  11.  
  12. def pbpaste():
  13.     "Returns contents of clipboard"
  14.     board = AppKit.NSPasteboard.generalPasteboard()
  15.     content = board.stringForType_(AppKit.NSStringPboardType)
  16.     return content
  17. class PasteBoard(object):
  18.     def copy(self, s):
  19.         if not isinstance(s, basestring):
  20.             s = repr(s)
  21.         pbcopy(s)
  22.     paste = property(lambda self: pbpaste(), fset=copy)
  23.     copy = property(lambda self: pbpaste(), fset=copy)
  24.  
  25.     def lines():
  26.         def fget(self):
  27.             return pbpaste().replace("\r","\n").split("\n")
  28.  
  29.         def fset(self, l):
  30.             pbcopy('\n'.join(unicode(i) for i in l))
  31.  
  32.         return {'fget':fget, 'fset':fset}
  33.     lines = property(**lines())
  34.  
  35.     def split():
  36.         def fget(self):
  37.             def _(sep):
  38.                 return pbpaste().replace("\r"," ").replace("\n"," ").split(sep)
  39.             return _
  40.  
  41.         def fset(self, t):
  42.             pbcopy(unicode(t[0]).join(unicode(i) for i in t[1]))
  43.  
  44.         return {'fget':fget, 'fset':fset}
  45.     split = property(**split())
  46.     join = split
  47.  
  48.     def words():
  49.         def fget(self):
  50.             return pbpaste().replace("\r"," ").replace("\n"," ").split(" ")
  51.  
  52.         def fset(self, l):
  53.             pbcopy(' '.join(unicode(i) for i in l))
  54.  
  55.         return {'fget':fget, 'fset':fset}
  56.     words = property(**words())
  57.  
  58.     def to_plain(self):
  59.         pbcopy(pbpaste())
  60.  
  61.     def to_ascii(self):
  62.         pbcopy(pbpaste().encode("ASCII", "ignore"))
  63.  
  64.     def to_nonascii(self):
  65.         pbcopy(''.join(char for char in pbpaste() if ord(char)>128))
  66.  
  67.     def to_indent(self):
  68.         pbcopy('\n'.join('\t'+line for line in pbpaste().split("\n")))
  69.  
  70.     def to_dedent(self):
  71.         lines = pbpaste().replace("\t", "    ").split("\n")
  72.         lines = '\n'.join(line[4:] for line in lines)
  73.         pbcopy(lines)
  74.  
  75.     def to_title(self):
  76.         pbcopy(pbpaste().title())
  77.  
  78. pb = PasteBoard()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top