Skip to content

Instantly share code, notes, and snippets.

@andras-tim
Last active May 10, 2019 14:26
Show Gist options
  • Save andras-tim/1132ebb4936bade18a59f7ddaafcc932 to your computer and use it in GitHub Desktop.
Save andras-tim/1132ebb4936bade18a59f7ddaafcc932 to your computer and use it in GitHub Desktop.
Call user32.ShowWindow on (Internet) Explorer windows by path

show_window.py

Call ShowWindow() of shell32.dll for manage size of (Internet) Explorer windows by path.

Install

pip install --user pywin32
import os
import sys
import ctypes
from pathlib import Path
from win32com.client import Dispatch
COMMANDS = {
'normal': 1,
'min': 2,
'max': 3,
}
def main():
command, absolute_path = get_argsuments()
window = getExplorerWindowByPath(absolute_path)
if window is None:
print_err('There is not any Explorer window with path {}'.format(absolute_path))
exit(3)
showWindow(window, command)
def get_argsuments():
arguments = sys.argv[1:]
if len(arguments) != 2:
print_err('Bad arguments; got {count} required 2{nl}Valid parameters: COMMANDS PATH{nl}'.format(
count=len(arguments),
nl=os.linesep
))
exit(1)
command_name, path = arguments
command = COMMANDS.get(command_name)
if command is None:
print_err('Unknown command {!r}'.format(command_name))
print_commands()
exit(2)
absolute_path = os.path.abspath(path)
return command, absolute_path
def print_commands():
print_err('Available commands:{}{}'.format(
os.linesep,
os.linesep.join([' {}'.format(k) for k in COMMANDS])
))
def getExplorerWindowByPath(absolute_path):
url = Path(absolute_path).as_uri()
for window in getExplorerWindows():
if window.LocationUrl.lower() == url.lower():
return window
return None
def getExplorerWindows():
return Dispatch("Shell.Application").Windows()
def showWindow(window, command):
ctypes.windll.user32.ShowWindow(window.hwnd, command)
def print_err(text):
print(text, file=sys.stderr)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment