movewin.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #!/usr/bin/python3
  2. import os
  3. import sys
  4. import time
  5. import psutil
  6. # python3 movewin.py window-title x y
  7. # python3 movewin.py COMMA 723 943
  8. def winfo(name="WinfoWinName"):
  9. search = name
  10. cmd = "xwininfo -root -children -all | grep '{}'"
  11. cmd = cmd.format(search)
  12. print(cmd)
  13. r = os.popen(cmd)
  14. lines = r.readlines()
  15. _id = [] #"xxxx"
  16. if lines and lines[0]:
  17. _id.append( lines[0].split()[0] )
  18. print("ID:",_id)
  19. for line in lines:
  20. line = line.strip()
  21. print("-",line)
  22. return _id
  23. def movewin(_id="0xWinId",x=None,y=None):
  24. cmd="xdotool windowmove {} {} {}".format(_id,x,y)
  25. return cmd
  26. def sizewin(_id="0xWinId",x=None,y=None):
  27. cmd="xdotool windowsize {} {} {}".format(_id,x,y)
  28. return cmd
  29. def activate(_id="0xWinId"):
  30. cmd="xdotool windowactivate {}".format(_id)
  31. return cmd
  32. def system(cmd):
  33. print(cmd)
  34. os.system(cmd)
  35. def search_process(_file_path,exact=1):
  36. print("search_process",_file_path)
  37. pids = psutil.pids()
  38. count = 0
  39. out = []
  40. for pid in pids:
  41. try:
  42. p = psutil.Process(pid)
  43. except psutil.NoSuchProcess:
  44. break
  45. ps = p.cmdline()
  46. if len(ps) < 2:
  47. continue
  48. if "python" not in ps[0]:
  49. continue
  50. print(" ",[ps[1]])
  51. print("exact_search",exact)
  52. if exact:
  53. if str(_file_path) == str(ps[1]):
  54. print(ps)
  55. count += 1
  56. out.append(pid)
  57. else:
  58. if str(_file_path) in str(ps[1]):
  59. print(ps)
  60. count += 1
  61. out.append(pid)
  62. print("search_process",count)
  63. return out
  64. def process_kill(path):
  65. pids = search_process(path,exact=0)
  66. for pid in pids:
  67. print("process_kill:",pid)
  68. p = psutil.Process(pid)
  69. #p.name()
  70. #p.cmdline()
  71. p.terminate()
  72. p.wait()
  73. import inspect
  74. def get_lineno():
  75. callerframerecord = inspect.stack()[1] # 0 represents this line
  76. # 1 represents line at caller
  77. frame = callerframerecord[0]
  78. info = inspect.getframeinfo(frame)
  79. #print(info.filename) # __FILE__ -> Test.py
  80. #print(info.function) # __FUNCTION__ -> Main
  81. #print(info.lineno) # __LINE__ -> 13
  82. return info.lineno
  83. if __name__ == "__main__":
  84. print("# python3 movewin.py window-title x y")
  85. print("# python3 movewin.py COMMA 723 943")
  86. import random
  87. a=random.randint(100,400)
  88. b=random.randint(100,400)
  89. search = "ASD"
  90. try:
  91. search = sys.argv[1]
  92. search = search.replace("'","")
  93. except:pass
  94. try:
  95. a = sys.argv[2]
  96. except:pass
  97. try:
  98. b = sys.argv[3]
  99. except:pass
  100. _ids = winfo(search)
  101. for _id in _ids:
  102. c1 = sizewin(_id,a,b)
  103. c2 = movewin(_id,a,b)
  104. c3 = activate(_id)
  105. system(c1)
  106. time.sleep(0.1)
  107. system(c2)
  108. time.sleep(0.1)
  109. system(c3)
  110. def check_is_started(CAPTION,_file_path,sleep=0):
  111. if sleep:
  112. time.sleep(sleep)
  113. pids = search_process(_file_path)
  114. if len(pids) >= 2:
  115. search = CAPTION[:]
  116. _ids = winfo(search)
  117. for _id in _ids:
  118. c3 = activate(_id)
  119. print("check_is_started CMD:",c3)
  120. os.system(c3)
  121. time.sleep(1)
  122. sys.exit()