Currently working with Pymel inside of Maya. I'm struggling to find a way to "alt-tab"/switch onto another open application after running a script.
Say if i clicked an "export to desktop" button, the script would then tab me onto my desktop.
Any input would be really useful, as Im very much flying blind at the moment.
Replies
http://docs.python.org/2.7/library/subprocess.html
Quick example: Let's say that you want to open up a texture in photoshop.
Quick note: Subprocess requires you to have the arguments in a tuple or list. So, something like "spam eggs" would cause an error, while or ('spam', 'eggs') would work. This is why my example code has double brackets.
As for showing the desktop, there's apparently some Windows scripting that you can do, but I haven't figured it out yet.
Cheers!
you can of course extend that to open the program like you already did, if the window is not found etc.
i'm using global variables to get the string to the windows callback function, you COULD use the lParam value to pass an object that contains the string for a more elegant approach, but yeah, a bit more complicated
import ctypes EnumWindowsProc = ctypes.WINFUNCTYPE(ctypes.c_bool, ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int)) EnumWindows = ctypes.windll.user32.EnumWindows GetWindowText = ctypes.windll.user32.GetWindowTextW GetWindowTextLength = ctypes.windll.user32.GetWindowTextLengthW # the hwnd of the window, if found, None otherwise gTheWindow = None # a string that is contained in your window's title gWindowTitle = None def _enumWindowsCallback(hwnd, lParam): """ will be called from windows for every top-level window """ global gTheWindow # we need to write to that global var later length = GetWindowTextLength(hwnd) buff = ctypes.create_unicode_buffer(length + 1) GetWindowText(hwnd, buff, length + 1) if gWindowTitle in buff.value: print "found the window" gTheWindow = hwnd return False # break the enumeration loop return True # check the next window def findWindow(): """ return True if window is found the windows hwnd will be saved in `gTheWindow` """ EnumWindows(EnumWindowsProc(_enumWindowsCallback), 0) if gTheWindow is None: print "no window with title %s found" % windowTitle return (gTheWindow is not None) # this is the function you want to call def bringWindowToFront(title): """ try to set focus (should automatically bring to front) on the window that has `title` in it's title """ global gWindowTitle gWindowTitle = title if findWindow(): ctypes.windll.user32.SetFocus(gTheWindow)win32gui.GetForegroundWindow() has a few tricks based around it.
HOWEVER. For the life of me i cannot get the win32 module to install now...
use ctypes, it is a standard-module and you can do everything with it that you can do with Win32 module, maybe with a little bit more work on your side
Actually if you look in my SIG at the psLink video that was all done with the openmaya api and comtypes with maya's built in python.