├── .gitignore ├── Makefile ├── README.md ├── _bin └── wmctrl.exe ├── _dev ├── CommandLineArgs.cs ├── ListWindows.cs ├── OpenAndType.cs └── SwitchWindow.cs ├── _ref └── wmctrl.man └── wmctrl.cs /.gitignore: -------------------------------------------------------------------------------- 1 | wmctrl.exe 2 | _dev/*.exe 3 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | 2 | CSC=csc 3 | CSC=mcs 4 | 5 | PROG=wmctrl.exe 6 | 7 | 8 | 9 | all: $(PROG) test 10 | 11 | $(PROG): wmctrl.cs 12 | $(CSC) wmctrl.cs 13 | 14 | 15 | test: 16 | $(PROG) -h 17 | $(PROG) -l 18 | $(PROG) -a gvim 19 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # wmctrl-for-windows 2 | Command line implementation of wmctrl for windows. 3 | Only few features of the linux tool wmctrl are implemented so far, that is: 4 | 5 | - List the window titles, process name and ID 6 | - Switch the focus to a given window 7 | 8 | The program is written in C-sharp. 9 | This repository contains the latest binary and the source code (compatible Mono or Microsoft). 10 | 11 | ## Binaries 12 | The latest binary is available [here](https://github.com/elmanuelito/wmctrl-for-windows/raw/master/_bin/wmctrl.exe) 13 | 14 | ## Features/ usage 15 | Typical example: 16 | ```bash 17 | # Jump to google chrome window 18 | wmctrl -a chrome 19 | ``` 20 | 21 | 22 | Usage: 23 | ```bash 24 | wmctrl [options] [args] 25 | 26 | options: 27 | -h : show this help 28 | -l : list windows 29 | -a : switch to the window of the process name 30 | ``` 31 | 32 | 33 | ## Compilation 34 | Compile (using Mono or Miscrosoft Visual Studio tools). 35 | You can use the Makefile provided in this repository. 36 | (You need csc.exe or mcs.exe in your system path) 37 | 38 | ## Installation 39 | Put the exe file somewhere in your system path. 40 | -------------------------------------------------------------------------------- /_bin/wmctrl.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ebranlard/wmctrl-for-windows/0be62a50465e5c2e60a509b746539995ab65ca2b/_bin/wmctrl.exe -------------------------------------------------------------------------------- /_dev/CommandLineArgs.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | public class CommandLine 4 | { 5 | public static int SwitchToWindow(){ 6 | Console.WriteLine("SwitchToWindow"); 7 | return 0; 8 | } 9 | 10 | public static int ListWindows(){ 11 | Console.WriteLine("ListWindows"); 12 | return 0; 13 | } 14 | 15 | 16 | public static int Main(string[] args) 17 | { 18 | int status=0; // Return status for Main 19 | 20 | // -------------------------------------------------------------------------------- 21 | // --- Parsing arguments 22 | // -------------------------------------------------------------------------------- 23 | int nArgs=args.Length; 24 | Console.WriteLine("Number of command line parameters = {0}", args.Length); 25 | 26 | int i=0; 27 | while (i 9 | using System.Windows.Forms; // SendKeys 10 | // using System.Threading; // For Thread.Sleep 11 | 12 | class Program 13 | { 14 | //dll import (can't be in method, but needs to be in class 15 | [DllImport("user32.dll")] 16 | public static extern int SetForegroundWindow(IntPtr point); 17 | static void Main() 18 | { 19 | // -------------------------------------------------------------------------------- 20 | // --- 21 | // -------------------------------------------------------------------------------- 22 | Process p = Process.Start("notepad.exe"); 23 | p.WaitForInputIdle(); 24 | IntPtr h = p.MainWindowHandle; 25 | SetForegroundWindow(h); 26 | SendKeys.SendWait("k"); 27 | // Thread.Sleep(1000); 28 | IntPtr processFoundWindow = p.MainWindowHandle; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /_dev/SwitchWindow.cs: -------------------------------------------------------------------------------- 1 | // //declarations 2 | using System; // For IntPtr 3 | using System.Runtime.InteropServices; // DllImport 4 | using System.Diagnostics; // Process 5 | // using System.Windows.Automation; // UIAutomationClient.dll 6 | // using System.Threading; // For Thread.Sleep 7 | 8 | class Program 9 | { 10 | //dll import (can't be in method, but needs to be in class 11 | [DllImport("user32.dll")] 12 | public static extern void SwitchToThisWindow(IntPtr hWnd); 13 | static void Main() 14 | { 15 | // -------------------------------------------------------------------------------- 16 | // --- 17 | // -------------------------------------------------------------------------------- 18 | String procName = "MATLAB"; 19 | //function which calls switchWindow() is here but not important 20 | // // 21 | Process[] procs = Process.GetProcessesByName(procName); 22 | int nProcs = procs.Length; 23 | if (nProcs < 1) { 24 | Console.WriteLine("No process found for name: {0}", procName); 25 | }else{ 26 | // We'll use the first window we found 27 | Process proc=procs[0]; 28 | if (nProcs >1) { 29 | Console.WriteLine("{0} processes found with name: {0}",nProcs,procName); 30 | Console.WriteLine("Using first process:"); 31 | Console.WriteLine("Process Name: {0} ID: {1} Title: {2}", proc.ProcessName, proc.Id, proc.MainWindowTitle); 32 | } 33 | // --- Switching to window using user32.dll function 34 | SwitchToThisWindow(proc.MainWindowHandle); 35 | 36 | // --- Alternative method using AutomationElement 37 | // AutomationElement element = AutomationElement.FromHandle(proc.MainWindowHandle); 38 | // if (element != null) { element.SetFocus(); } 39 | } // Proc Window found 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /_ref/wmctrl.man: -------------------------------------------------------------------------------- 1 | wmctrl(1) - Linux man page 2 | Name 3 | 4 | wmctrl - interact with a EWMH/NetWM compatible X Window Manager. 5 | Synopsis 6 | 7 | wmctrl [ options | actions ]... 8 | Description 9 | 10 | wmctrl is a command that can be used to interact with an X Window manager that is compatible with the EWMH/NetWM specification. wmctrl can query the window manager for information, and it can request that certain window management actions be taken. 11 | 12 | wmctrl is controlled entirely by its command line arguments. The command line arguments are used to specify the action to be performed (with options that modify behavior) and any arguments that might be needed to perform the actions. 13 | 14 | The following sections define the supported actions and options. Arguments to the actions and options are written in the form in the descriptions below. The detailed syntax for writing arguments are in a single section dedicated to that purpose. 15 | Actions 16 | 17 | The following command line arguments can be specified to invoke a wmctrl action. Only one action can be executed with the invocation of the wmctrl command. 18 | 19 | -a 20 | Switch to the desktop containing the window , raise the window, and give it focus. 21 | -b ( add | remove | toggle),prop1 [,prop2 ] 22 | Add, remove, or toggle up to two window properties simultaneously. The window that is being modified must be identified with a -r action. The property change is achived by using the EWMH _NET_WM_STATE request. The supported property names (for prop1 and prop2) are modal, sticky, maximized_vert, maximized_horz, shaded, skip_taskbar, skip_pager, hidden, fullscreen, above and below. Two properties are supported to allow operations like maximizing a window to full screen mode. Note that this action is made up of exactly two shell command line arguments. 23 | -c 24 | Close the window gracefully. 25 | -d 26 | 27 | List all desktops managed by the window manager. One line is output for each desktop, with the line broken up into space separated columns. The first column contains an integer desktop number. The second column contains a '*' character for the current desktop, otherwise it contains a '-' character. The next two columns contain the fixed string DG: and then the desktop geometry as 'x' (e.g. '1280x1024'). The following two columns contain the fixed string VP: and then the viewport position in the format ',' (e.g. '0,0'). The next three columns after this contains the fixed string WA: and then two columns with the workarea geometry as 'X,Y and WxH' (e.g. '0,0 1280x998'). The rest of the line contains the name of the desktop (possibly containing multiple spaces). 28 | -e 29 | Resize and move a window that has been specified with a -r action according to the argument. 30 | -g w,h 31 | 32 | Change the geometry (common size) of all desktops so they are w pixels wide and h pixels high. w and h must be positive integers. A window manager may ignore this request. 33 | 34 | -h 35 | 36 | Print help text about program usage. 37 | -I name 38 | Set the icon name (short title) of the window specified by a -r action to name. 39 | -k ( on | off ) 40 | Turn on or off the window manager's "show the desktop" mode (if the window manager implements this feature). 41 | -l 42 | 43 | List the windows being managed by the window manager. One line is output for each window, with the line broken up into space separated columns. The first column always contains the window identity as a hexadecimal integer, and the second column always contains the desktop number (a -1 is used to identify a sticky window). If the -p option is specified the next column will contain the PID for the window as a decimal integer. If the -G option is specified then four integer columns will follow: x-offset, y-offset, width and height. The next column always contains the client machine name. The remainder of the line contains the window title (possibly with multiple spaces in the title). 44 | 45 | -m 46 | 47 | Display information about the window manager and the environment. 48 | 49 | -n N 50 | 51 | Change the number of desktops to N (a non-negative integer). 52 | -N name 53 | Set the name (long title) of the window specified by a -r action to name. 54 | -o x,y 55 | 56 | Change the viewport for the current desktop. The values x and y are numeric offsets that specify the position of the top left corner of the viewport. A window manager may ignore this request. 57 | -r 58 | Specify a target window for an action. 59 | -R 60 | Move the window to the current desktop, raise the window, and give it focus. 61 | -s 62 | Switch to the desktop . 63 | -t 64 | Move a window that has been specified with the -r action to the desktop . 65 | -T name 66 | Set the both the name (long title) and icon name (short title) of the window specified by a -r action to name. This action is like using the -N and -I actions at the same time (which would otherwise be impossible since wmctrl can execute only one action at a time). 67 | 68 | Options 69 | 70 | The following options modify the default actions, or they modify the interpretation of arguments. 71 | 72 | -F 73 | 74 | Window name arguments () are to be treated as exact window titles that are case sensitive. Without this options window titles are considered to be case insensitive substrings of the full window title. 75 | 76 | -G 77 | 78 | Include geometry information in the output of the -l action. 79 | 80 | -i 81 | 82 | Interpret window arguments () as a numeric value rather than a string name for the window. If the numeric value starts with the prefix '0x' it is assumed to be a hexadecimal number. 83 | 84 | -p 85 | 86 | Include PIDs in the window list printed by the -l action. Prints a PID of '0' if the application owning the window does not support it. 87 | 88 | -u 89 | 90 | Override auto-detection and force UTF-8 mode. 91 | 92 | -v 93 | 94 | Provide verbose output. This is really useful when debugging wmctrl itself. 95 | -w [ [,]... ] 96 | Use workarounds specified in the argument. 97 | -x 98 | 99 | Include WM_CLASS in the window list or interpret as the WM_CLASS name. 100 | 101 | Arguments 102 | 103 | 104 | 105 | A Desktop is always specified by an integer which represents the desktop numbers. Desktop numbers start at 0. Use -1 to pin to all desktops. 106 | 107 | A move and resize argument has the format 'g,x,y,w,h'. All five components are integers. The first value, g, is the gravity of the window, with 0 being the most common value (the default value for the window). Please see the EWMH specification for other values. 108 | The four remaining values are a standard geometry specification: x,y is the position of the top left corner of the window, and w,h is the width and height of the window, with the exception that the value of -1 in any position is interpreted to mean that the current geometry value should not be modified. 109 | 110 | 111 | This argument specifies a window that is the target of an action. By default the argument is treated as if were a string, and windows are examined until one is found with a title the contains the specified string as a substring. The substring matching is done in a case insensitive manner. The -F option may be used to force exact, case sensitive title matching. The option -i may be used to interpret the window target as a numeric window identity instead of a string. 112 | The window name string :SELECT: is treated specially. If this window name is used then wmctrl waits for the user to select the target window by clicking on it. 113 | 114 | The window name string :ACTIVE: may be used to instruct wmctrl to use the currently active window for the action. 115 | 116 | There is only one work around currently implemeted. It is specified by using the string DESKTOP_TITLES_INVALID_UTF8 and it causes the printing of non-ASCII desktop tiles correctly when using Window Maker. 117 | 118 | Examples 119 | 120 | Getting a list of windows managed by the window manager 121 | 122 | wmctrl -l 123 | Getting a list of windows with PID and geometry information. 124 | wmctrl -p -G -l 125 | Going to the window with a name containing 'emacs' in it 126 | wmctrl -a emacs 127 | Shade a window with a title that contains the word 'mozilla' 128 | wmctrl -r mozilla -b add,shaded 129 | Close a very specifically titled window sticky 130 | wmctrl -F -c 'Debian bug tracking system - Mozilla' 131 | Toggle the 'stickiness' of a window with a specific window identity 132 | wmctrl -i -r 0x0120002 -b add,sticky 133 | Change the title of window to a specified string but choose the window by clicking on it 134 | wmctrl -r :SELECT: -T "Selected Window" 135 | -------------------------------------------------------------------------------- /wmctrl.cs: -------------------------------------------------------------------------------- 1 | using System; // For IntPtr 2 | using System.Runtime.InteropServices; // DllImport 3 | using System.Diagnostics; // Process 4 | 5 | public class wmctrl 6 | { 7 | 8 | // -------------------------------------------------------------------------------- 9 | // --- Switch to Window 10 | // -------------------------------------------------------------------------------- 11 | //dll import (can't be in method, but needs to be in class 12 | [DllImport("user32.dll")] 13 | public static extern void SwitchToThisWindow(IntPtr hWnd); 14 | 15 | public static int SwitchToWindow(string procName){ 16 | // Getting window matching 17 | Process[] procs = Process.GetProcessesByName(procName); 18 | int nProcs = procs.Length; 19 | if (nProcs < 1) { 20 | Console.WriteLine("Error: No process found for name: {0}", procName); 21 | return -1 ; 22 | }else{ 23 | // We'll use the first window we found 24 | Process proc=procs[0]; 25 | if (nProcs >1) { 26 | Console.WriteLine("{0} processes found with name: {1}",nProcs,procName); 27 | Console.WriteLine("Using first process:"); 28 | Console.WriteLine("Process Name: {0} ID: {1} Title: {2}", proc.ProcessName, proc.Id, proc.MainWindowTitle); 29 | } 30 | // --- Switching to window using user32.dll function 31 | SwitchToThisWindow(proc.MainWindowHandle); 32 | return 0; 33 | } 34 | } 35 | 36 | 37 | // -------------------------------------------------------------------------------- 38 | // --- List Windows info 39 | // -------------------------------------------------------------------------------- 40 | public static int ListWindows(){ 41 | Process[] processlist = Process.GetProcesses(); 42 | Console.WriteLine("ID: \t Name:\t Title:"); 43 | Console.WriteLine("-------------------------------------------------"); 44 | foreach (Process proc in processlist) 45 | { 46 | if (!String.IsNullOrEmpty(proc.MainWindowTitle)) 47 | { 48 | Console.WriteLine("{0}\t {1}\t {2}", proc.Id,proc.ProcessName, proc.MainWindowTitle); 49 | } 50 | } 51 | return 0; 52 | } 53 | 54 | // -------------------------------------------------------------------------------- 55 | // --- Print command usage 56 | // -------------------------------------------------------------------------------- 57 | public static void print_usage(){ 58 | Console.WriteLine(""); 59 | Console.WriteLine("usage: wmctrl [options] [args]"); 60 | Console.WriteLine(""); 61 | Console.WriteLine("options:"); 62 | Console.WriteLine(" -h : show this help"); 63 | Console.WriteLine(" -l : list windows"); 64 | Console.WriteLine(" -a : switch to the window of the process name "); 65 | Console.WriteLine(""); 66 | 67 | } 68 | 69 | // -------------------------------------------------------------------------------- 70 | // --- Main Program 71 | // -------------------------------------------------------------------------------- 72 | public static int Main(string[] args) 73 | { 74 | int status=0; // Return status for Main 75 | 76 | // -------------------------------------------------------------------------------- 77 | // --- Parsing arguments 78 | // -------------------------------------------------------------------------------- 79 | int nArgs=args.Length; 80 | if (nArgs==0){ 81 | Console.WriteLine("Error: insufficient command line arguments"); 82 | print_usage(); 83 | return 0; 84 | } 85 | int i=0; 86 | while (i