TankorSmash

Clipboard history

Jan 30th, 2012
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.64 KB | None | 0 0
  1. import time
  2. from os import SEEK_END
  3.  
  4. #not mine  #
  5. import win32clipboard as w
  6. import win32con
  7.  
  8.  
  9. def getText():
  10.     w.OpenClipboard()
  11.     try:
  12.         d=w.GetClipboardData(win32con.CF_TEXT)
  13.    
  14.     except TypeError:
  15.         d = 'Specified clipboard format is not available!'
  16.     w.CloseClipboard()
  17.     return d
  18.  
  19. def setText(aType,aString):
  20.     w.OpenClipboard()
  21.     w.EmptyClipboard()
  22.     w.SetClipboardData(aType,aString)
  23.     w.CloseClipboard()
  24.    
  25. #end not mine#
  26.  
  27. #----------------------------------------------------------------------
  28. def savePaste(pasteData):
  29.     """saves the paste to a file, timestamped:
  30.    FRI DEC 1 2012
  31.    PASTE: clipboard data here"""
  32.    
  33.     file = loadFile()
  34.    
  35.     print 'paste saved', pasteData
  36.    
  37.     timeData = time.strftime('%a %b %d, %Y at %I:%Ms%S')
  38.     #append time/date
  39.     #combiData = timeData, '\nPASTE:', pasteData
  40.     combiData = "###### {0} ######\n{1}\n".format(timeData, pasteData)
  41.     file.write(combiData)
  42.    
  43.    
  44.     pass
  45.  
  46.  
  47. #----------------------------------------------------------------------
  48. def loadFile():
  49.     """loads a text file with raw paste data, if none found, is created"""
  50.    
  51.     try :
  52.         f = open('paste data2.txt', 'r+')
  53.        
  54.         print 'found file'
  55.         #should seek to the end of a file
  56.         f.seek(0, SEEK_END)        
  57.     except IOError:
  58.        
  59.         print 'not found file'
  60.        
  61.         #create the file
  62.         open('paste data2.txt', 'w')
  63.        
  64.         f = open('paste data1.txt', 'r+')
  65.        
  66.         #should seek to the end of a file
  67.         #f.seek(-1)
  68.        
  69.         print 'properly made a file'
  70.    
  71.     #else:
  72.         #print 'dunno what happened'
  73.         #f = 'Invalid file error'
  74.        
  75.     #should seek to the end of a file
  76.     #f.seek(-1)        
  77.        
  78.     print 'about to return:', f
  79.     return f
  80.  
  81.  
  82. quit = False
  83.  
  84. print 'Starting'
  85.  
  86. #create a file
  87. loadFile()
  88.  
  89. #set oldPaste to current clipboard
  90. oldPaste = getText()
  91. while not quit:
  92.    
  93.     #grab current clipboard
  94.     curPaste = getText()
  95.    
  96.     #if the current paste and old paste are different
  97.     if not curPaste == oldPaste:
  98.        
  99.         if curPaste == 'quit':
  100.            
  101.             print 'caught "quit", so I"m quitting'
  102.             quit = True
  103.             break
  104.        
  105.        
  106.         print 'Clipboard has changed!'
  107.        
  108.         #save curPaste to a file or whatever
  109.         savePaste(curPaste)
  110.        
  111.         #update old paste so it doesn't get caught twice
  112.         oldPaste = curPaste
  113.        
  114.     #wait x seconds
  115.     time.sleep(0.1)
  116.        
  117.        
  118. print 'end of while loop'
Advertisement
Add Comment
Please, Sign In to add comment