Python + Win32: Cut/Paste w/o GUI

Matt Gerrans mgerrans at mindspring.com
Sat Oct 5 00:20:20 EDT 2002


At first you said "paste from an arbitrary Windows app" and I was not sure
what you were trying to do, but if what you really meant was drag-and-drop
from Explorer to a shortcut, for instance, then there is hope.   Try the
following; if you run it with no parameters, it will create a shortcut to
itself on the desktop.   After that, if you drag-and-drop another icon onto
that shorcut, it will pop up a message (I know, this is not entirely console
stuff, but I wanted the results to be clear and anyway, you just wanted to
get the parameters).

import os,sys,win32com.client,win32ui

if len(sys.argv) == 1:
   shell = win32com.client.Dispatch("WScript.Shell")
   desktop = shell.SpecialFolders("Desktop")

   # Might want to get python location from registry, but I'm
   # being lazy.
   python = 'c:\\Python22\\Python.exe'
   target = os.path.join(desktop,'Shorty.lnk')
   link = shell.CreateShortcut( python + ' ' + target  )
   link.TargetPath = sys.argv[0]
   try:
      link.Save()
   except:
      print 'Failed to save "%s", maybe it already exists.'%target
else:
   win32ui.MessageBox( "Quit dropping your %s's on me!" % sys.argv[1] )

"Tim Daneliuk" <tundra at tundraware.com> wrote:
> Sorry, I was not specific enough.  What you suggest should work.  But,
what
> I want is so be able to paste the text to the *icon that invokes the
shortcut*
> thereby starting the program with the desired text available as described.
> Sort of a drag-n-drop of text onto a non-GUI script is what I'm after...






More information about the Python-list mailing list