Skip to content

Instantly share code, notes, and snippets.

@hcoyote
Created February 13, 2012 21:32
Show Gist options
  • Save hcoyote/1820664 to your computer and use it in GitHub Desktop.
Save hcoyote/1820664 to your computer and use it in GitHub Desktop.
Use wmctrl to find a window or exec a command if window name not found.
#!/usr/bin/perl
use strict;
use warnings;
my $debug = 1;
my $wmctrl = '/usr/bin/wmctrl';
my $window_name = shift;
my $exec_command = shift;
print "Window name is $window_name\n" if $debug;
print "Exec command is $exec_command\n" if $debug;
open(WMDESKLIST, "$wmctrl -d |") or
die "could not open $wmctrl to find current desk: $!\n";
my $desk;
while (my $line = <WMDESKLIST>) {
($desk) = split(/:/, $line);
if ($desk =~ /\*/) {
$desk =~ m/^(\d+)/;
$desk = $1;
last;
}
}
close(WMDESKLIST);
open(WMWINLIST, "$wmctrl -l |") or
die "could not open $wmctrl to find current window list: $!\n";
my $win;
while (my $line = <WMWINLIST>) {
print $line if $debug;
if ($line =~ m^(0x[0-9a-f]+) [ -]\d+\s+(\w+([\w\.]+)?|N/A)\s+$window_name^) {
print "==> $line" if $debug;
$win = $1;
last;
}
}
close(WMWINLIST);
if (not defined $win) {
exec $exec_command;
} else {
exec "$wmctrl", "-i", "-R", $win;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment