Advertisement
Snuggledash

Restart Dropbox in case it hangs itself, again *sigh*

Nov 25th, 2014
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.00 KB | None | 0 0
  1. import os, subprocess, ctypes, struct
  2.  
  3. # TODO: consolidate tray refresher methods into class
  4.  
  5. WM_MOUSEMOVE = 0x200
  6. winapi = ctypes.windll.user32
  7.  
  8. class Rectangle(ctypes.Structure):
  9.     _fields_ = [("left", ctypes.c_int32),
  10.                 ("top", ctypes.c_int32),
  11.                 ("right", ctypes.c_int32),
  12.                 ("bottom", ctypes.c_int32)]
  13.  
  14. def _atLeastWin7():
  15.     packed_version = ctypes.windll.kernel32.GetVersion()
  16.     (major, minor, build) = struct.unpack('<BBH', struct.pack('<L', packed_version))
  17.     return major > 6 or (major == 6 and minor >= 1)
  18.  
  19. def _findWindow(parent, winclass):
  20.     return winapi.FindWindowExW(parent, None, winclass, None)
  21.  
  22. def _findTray():
  23.     hwnd1 = _findWindow(None, "Shell_TrayWnd")
  24.     if hwnd1:
  25.         hwnd2 = _findWindow(hwnd1, "TrayNotifyWnd")
  26.         if hwnd2:
  27.             hwnd3 = _findWindow(hwnd2, "SysPager")
  28.             if hwnd3:
  29.                 return _findWindow(hwnd3, "ToolbarWindow32")
  30.  
  31. def _findOverflowTray():
  32.     hwnd1 = _findWindow(None, "NotifyIconOverflowWindow")
  33.     if hwnd1:
  34.         return _findWindow(hwnd1, "ToolbarWindow32")
  35.  
  36. def _fakeCursorMovement(hwnd, x, y):
  37.     # XXX: check that x, y are short ints
  38.     return winapi.SendMessageW(hwnd, WM_MOUSEMOVE, 0, (y << 16) + x) == 0
  39.  
  40. def _touchWindow(hwnd):
  41.     r = Rectangle()
  42.     if winapi.GetClientRect(hwnd, ctypes.byref(r)):
  43.         for x in range(0, r.right, 5):
  44.             for y in range(0, r.bottom, 5):
  45.                 _fakeCursorMovement(hwnd, x, y)
  46.  
  47. def refreshTray():
  48.     hwnd = _findTray()
  49.     if hwnd:
  50.         _touchWindow(hwnd)
  51.     if _atLeastWin7():
  52.         hwnd = _findOverflowTray()
  53.         if hwnd:
  54.             _touchWindow(hwnd)
  55.        
  56. if __name__ == '__main__':
  57.     print("Killing Dropbox...")
  58.     os.system("taskkill /F /IM Dropbox.exe")
  59.  
  60.     print("Refreshing tray...")
  61.     refreshTray()
  62.    
  63.     print("Restarting Dropbox...")
  64.     app = os.environ["APPDATA"] + "\\Dropbox\\bin\\Dropbox.exe"
  65.     subprocess.Popen([app, "/systemstartup"])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement