IUniformResourceLocator.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import pythoncom
  2. from win32com.shell import shell, shellcon
  3. import win32api, os
  4. class InternetShortcut:
  5. def __init__( self ):
  6. self._base = pythoncom.CoCreateInstance(
  7. shell.CLSID_InternetShortcut, None,
  8. pythoncom.CLSCTX_INPROC_SERVER, shell.IID_IUniformResourceLocator
  9. )
  10. def load( self, filename ):
  11. # Get an IPersist interface
  12. # which allows save/restore of object to/from files
  13. self._base.QueryInterface( pythoncom.IID_IPersistFile ).Load( filename )
  14. def save( self, filename ):
  15. self._base.QueryInterface( pythoncom.IID_IPersistFile ).Save( filename, 1 )
  16. def __getattr__( self, name ):
  17. if name != "_base":
  18. return getattr( self._base, name )
  19. temp_dir=win32api.GetTempPath()
  20. linkname=win32api.GetTempFileName(temp_dir, 'ish')[0]
  21. print 'Link:',linkname
  22. os.remove(linkname)
  23. linkname+='.url'
  24. ish=InternetShortcut()
  25. ish.SetURL('https://github.com/mhammond/pywin32')
  26. ish.save(linkname)
  27. ## IUniformResourceLocator also give access to IPropertySetStorage
  28. pss=ish.QueryInterface(pythoncom.IID_IPropertySetStorage)
  29. ps=pss.Open(shell.FMTID_InternetSite)
  30. property_ids=[(k,v) for k,v in shellcon.__dict__.iteritems() if k.startswith('PID_INTSITE_')]
  31. for pname, pval in property_ids:
  32. print pname, ps.ReadMultiple((pval,))[0]
  33. ps=pss.Open(shell.FMTID_Intshcut)
  34. property_ids=[(k,v) for k,v in shellcon.__dict__.iteritems() if k.startswith('PID_IS_')]
  35. for pname, pval in property_ids:
  36. print pname, ps.ReadMultiple((pval,))[0]
  37. new_sh=InternetShortcut()
  38. new_sh.load(linkname)
  39. new_sh.InvokeCommand('Open')