Added -p and -b flag

This commit is contained in:
Peter 2020-01-08 13:56:50 +08:00
parent dc0e1a5df9
commit 585a3c2a60
3 changed files with 91 additions and 10 deletions

View File

@ -2,7 +2,7 @@
CSC=csc
CSC=mcs
PROG=wmctrl.exe
PROG=./wmctrl.exe
@ -15,4 +15,5 @@ $(PROG): wmctrl.cs
test:
$(PROG) -h
$(PROG) -l
$(PROG) -a gvim
$(PROG) -a devenv
#you can test the others yourself :D

View File

@ -1,14 +1,16 @@
# wmctrl-for-windows
This is a fork of https://github.com/ebranlard/wmctrl-for-windows with two new flags added for PID and process title :D
Command line implementation of wmctrl for windows (pseudo-equivalent).
Only few features of the linux tool wmctrl are implemented so far, that is:
- List the window titles, process name and ID
- Switch the focus to a given window
- Switch the focus to a given window (Based on process title, process name or PID)
The program is written in C-sharp.
This repository contains the latest binary and the source code (compatible Mono or Microsoft).
## Binaries
The latest binary is available [here](https://github.com/elmanuelito/wmctrl-for-windows/raw/master/_bin/wmctrl.exe)
You can get the binaries from the releases tab of this repo
## Features/ usage
@ -18,6 +20,8 @@ options:
-h : show this help
-l : list windows
-a <PNAME> : switch to the window of the process name <PNAME>
-b <PTITLE> : switch to the window of the process title <PTITLE>
-p <PID> : switch to the window of the process ID <PID>

View File

@ -33,6 +33,62 @@ public class wmctrl
}
}
// --------------------------------------------------------------------------------
// --- Switch to Window (TITLE)
// --------------------------------------------------------------------------------
public static int SwitchToWindowTitle(string procTitle)
{
int ret = -1;
Process[] procs = Process.GetProcesses();
foreach (Process proc in procs)
{
if (! String.IsNullOrEmpty(proc.MainWindowTitle) && string.Compare(procTitle, proc.MainWindowTitle) == 0)
{
Console.WriteLine("Process Name: {0} ID: {1} Title: {2}", proc.ProcessName, proc.Id, proc.MainWindowTitle);
SwitchToThisWindow(proc.MainWindowHandle); //Just going to use the first window as per usual
ret = 0;
break;
}
}
if (ret == -1)
{
Console.WriteLine("Error: No process found for title: {0}", procTitle);
}
return ret;
}
// --------------------------------------------------------------------------------
// --- Switch to Window (PID)
// --------------------------------------------------------------------------------
public static int SwitchToWindowPID(string procPIDstr)
{
int ret = -1;
int procPID;
if (Int32.TryParse(procPIDstr, out procPID)){
Process[] procs = Process.GetProcesses();
foreach (Process proc in procs)
{
if (procPID == proc.Id) //Can processes have no PID in MS windows? I have no idea.
{
Console.WriteLine("Process Name: {0} ID: {1} Title: {2}", proc.ProcessName, proc.Id, proc.MainWindowTitle);
SwitchToThisWindow(proc.MainWindowHandle); //Just going to use the first window as per usual
ret = 0;
break;
}
}
if (ret == -1)
{
Console.WriteLine("Error: No process found for PID: {0}", procPIDstr);
}
}
else
{
Console.WriteLine("Error: Invalid PID: {0}", procPIDstr);
}
return ret;
}
// --------------------------------------------------------------------------------
// --- List Windows info
@ -62,6 +118,8 @@ public class wmctrl
Console.WriteLine(" -h : show this help");
Console.WriteLine(" -l : list windows");
Console.WriteLine(" -a <PNAME> : switch to the window of the process name <PNAME>");
Console.WriteLine(" -b <PTITLE> : switch to the window of the process title <PTITLE>");
Console.WriteLine(" -p <PID> : switch to the window of the process ID <PID>");
Console.WriteLine("");
}
@ -99,6 +157,24 @@ public class wmctrl
status=-1;
}
break;
case "-b": // Switch to window (TITLE)
if (i+1<nArgs) {
status=SwitchToWindowTitle(args[i+1]);
i=i+2;
}else{
Console.WriteLine("Error: command line option -a needs to be followed by a process title.");
status=-1;
}
break;
case "-p": // Switch to window (PID)
if (i+1<nArgs) {
status=SwitchToWindowPID(args[i+1]);
i=i+2;
}else{
Console.WriteLine("Error: command line option -a needs to be followed by a process ID.");
status=-1;
}
break;
case "-l": // List Windows
status=ListWindows();
i++;