create_link.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # link.py
  2. # From a demo by Mark Hammond, corrupted by Mike Fletcher
  3. # (and re-corrupted by Mark Hammond :-)
  4. from win32com.shell import shell
  5. import pythoncom, os
  6. class PyShortcut:
  7. def __init__( self ):
  8. self._base = pythoncom.CoCreateInstance(
  9. shell.CLSID_ShellLink, None,
  10. pythoncom.CLSCTX_INPROC_SERVER, shell.IID_IShellLink
  11. )
  12. def load( self, filename ):
  13. # Get an IPersist interface
  14. # which allows save/restore of object to/from files
  15. self._base.QueryInterface( pythoncom.IID_IPersistFile ).Load( filename )
  16. def save( self, filename ):
  17. self._base.QueryInterface( pythoncom.IID_IPersistFile ).Save( filename, 0 )
  18. def __getattr__( self, name ):
  19. if name != "_base":
  20. return getattr( self._base, name )
  21. if __name__=='__main__':
  22. import sys
  23. if len(sys.argv)<2:
  24. print("Usage: %s LinkFile [path [, args[, description[, working_dir]]]]\n\nIf LinkFile does not exist, it will be created using the other args")
  25. sys.exit(1)
  26. file = sys.argv[1]
  27. shortcut = PyShortcut()
  28. if os.path.exists( file ):
  29. # load and dump info from file...
  30. shortcut.load( file )
  31. # now print data...
  32. print('Shortcut in file %s to file:\n\t%s\nArguments:\n\t%s\nDescription:\n\t%s\nWorking Directory:\n\t%s\nItemIDs:\n\t<skipped>'%(
  33. file,
  34. shortcut.GetPath(shell.SLGP_SHORTPATH)[0],
  35. shortcut.GetArguments(),
  36. shortcut.GetDescription(),
  37. shortcut.GetWorkingDirectory(),
  38. #shortcut.GetIDList(),
  39. ))
  40. else:
  41. if len(sys.argv) <3:
  42. print("Link file does not exist\nYou must supply the path, args, description and working_dir as args")
  43. sys.exit(1)
  44. # create the shortcut using rest of args...
  45. data = map( None, sys.argv[2:], ("SetPath", "SetArguments", "SetDescription", "SetWorkingDirectory") )
  46. for value, function in data:
  47. if value and function:
  48. # call function on each non-null value
  49. getattr( shortcut, function)( value )
  50. shortcut.save( file )