Wednesday, November 19, 2014

Clipboard Timestamp

Clipboard Timestamp

Launchy that timestamp into your clipboard

.background

Looking back through my blog, one of my most popular posts was how to write a timestamp script for Notepad++ in Python. I've been finding myself wishing I had that feature in more and more place, including web apps.

I had the idea to write a script that would generate the timestamp I wanted, and then place it in the Windows clipboard. This would allow me to generate and paste a timestamp anywhere I wanted. I ended up writing 2 scripts, the first was in Python and later I wrote a second in Powershell. Each as their advantages and disadvantages detailed below.

.launch

I tried a few techniques for calling the script, but in the end I settled on a tool I already use, called Launchy. Launchy is a super-simple (cross platform) application launcher with some powerful extra features. One of these features is that it can call scripts, and it even gives focus back to the application I was using after it is done!

http://www.launchy.net

.python

It's extremely safe to say that I don't know Python, but I've been programming since I was very little so I know how to hack around a language. It took me longer to figure out the little intricacies of the language than to write the actual script. I found an example on stackoverflow and then incorporated it with my existing code from Notepad++.

The Python community has a couple different ways of accessing the clipboard, but the one I liked best was to use a built-in GUI library called Tkinter. I don't like having to install a separate library when a built-in method is available.

When you run the script, a command window flashes temporarily, which was annoying. In Windows just rename your script to “.pyw” to avoid this. There are similar solutions for other platforms, just in case you are interested.

You have to have Python installed for this script to work, which could be a big downside if you don't already have it installed. I don't have a problem with this, but I have a hard time recommending a script that requires that you install a toolset you may never use for anything else.

# cdate.pyw
# add date/time to clipboard
# output example: 10/14/2014 JMJ
import time, sys
s_time = "%s JMJ" % (time.strftime('%m-%d-%Y'))

#s_time contains the timestamp text, now add it to the clipboard
from Tkinter import Tk
r = Tk()
r.withdraw()
r.clipboard_clear()
r.clipboard_append(s_time)
r.destroy()

sys.exit(0)


references:
http://stackoverflow.com/questions/579687/how-do-i-copy-a-string-to-the-clipboard-on-windows-using-python/4203897#4203897
http://stackoverflow.com/questions/1689015/run-python-script-without-dos-shell-appearing

.powershell

While researching how to link my python script into Launchy, I ran into an article explaining how to launch powershell commands. It made a lot of sense to write the same script in Powershell, since no additional software would be required.

Powershell has a command called “clip” that allows you to pipe text directly into the Windows clipboard. Very nice. I had the command written in just a few minutes.

However, powershell scripts do not like to run without opening a command window. It is very annoying. I ended up using a technique where you create a VBS script (!!!) to call the powershell command. I feel weird about using VBS in this way, but it works. Unfortunately it requires you add a bunch of quotes to escape out all the layers of script calls (VBS > CMD > PowerShell). If you don't care about initials you can remove that part entirely. If your initials are “JMJ” perhaps you have it best of all.


‘pclip.vbs
'add date/time to clipboard via powershell (yes, there are a lot of escaped quotes in this code)
‘output example: 10/14/2014 JMJ
command = "powershell.exe -nologo -command (get-date -format d).ToString() + """""" JMJ"""""" | clip "
set shell = CreateObject("WScript.Shell")
shell.Run command,0


references:
http://technet.microsoft.com/en-us/library/ee692801.aspx
http://www.faqforge.com/windows/how-to-execute-powershell-scripts-without-pop-up-window

.set up Launchy

  1. Under the Launchy options, switch to the Catalog tab.
  2. Add a new Directory (+), and include the folder where your scripts are located.
  3. Under File Types, enter either *.pyw or *.vbs (depending on which solution you went with)

The great thing about Launchy is that it will return focus to the application I was using after I run the script. This makes the script very easy to call from other programs and online apps.

.wrap-up

I can't decide which is going to be more useful, having a simple timestamp available inside any program or website I'm using, or the fact that I can build more scripts and run them very easily through Launchy.

No comments:

Post a Comment