linux - xwininfo 将选定的窗口置于最前面

标签 linux perl

我正在寻找此页面上代码的 Perl 版本: http://www.linuxquestions.org/questions/linux-general-1/how-to-bring-up-application-window-to-front-from-shell-script-83545/

以便使我能够将选定的窗口置于最前面。

我在 linux 机器上使用 xwininfo 来选择和获取窗口的 id(十六进制)。

有什么帮助吗?

#!/usr/bin/perl
# v5.10.1 / linux
use strict;
use warnings;
use Shell;
use Tk;

# to enable you to pick a window to record +
my $result  =`xwininfo | grep -e Width -e Height -e Absolute -e "xwininfo: Window id:"`;

# get picked window's position (abs_x and abs_y)
# its width and heigh too, and make these divisible by 2 with no remainder (for even)
my ($abs_x, $abs_y, $window_width, $window_height, $windowid);
my @linesofoutput= $result=~/(.*?)\n/g; # needs checking to see if I'm getting the lines right with this?!

 foreach my $temp(@linesofoutput){

    if ($temp  =~ /^\s*Absolute upper-left X:/){ 
                 my  @words = split(/\s/, $temp);           # make an array out of the line, splitting on any whitespace character: space, tab, newline, etc
                 @words = grep(!/^$/, @words);            # remove empty elements.  check this(not, starting with, ending with)???                       
                # print "$words[0] \t $words[1] \t $words[2] \t $words[3]\n";  
                 $abs_x = $words[3];
                 # print "Got Absolute X:\t $abs_x\n";
             }
             elsif($temp  =~ /^\s*Absolute upper-left Y:/){
                   my  @words = split(/\s/, $temp);
                 @words = grep(!/^$/, @words);
                 $abs_y = $words[3];
             }
             elsif($temp  =~ /^\s*Width:/){
                my  @words = split(/\s/, $temp);           # make an array out of the line, splitting on any whitespace character: space, tab, newline, etc
                 @words = grep(!/^$/, @words);            # remove empty elements.  check this(not, starting with, ending with)???                        
                 #print "$words[0] \t $words[1]\n";  
                 $window_width = $words[1];
                 # print "Got width:\t  $window_width\n";
             }
             elsif($temp  =~ /^\s*Height:/){
                my  @words = split(/\s/, $temp);
                 @words = grep(!/^$/, @words);
                 $window_height = $words[1];
             }
             elsif($temp  =~ /^\s*xwininfo: Window id:/){
                my  @words = split(/\s/, $temp);
                 @words = grep(!/^$/, @words);
               # print "Window id: $words[3]\n";
                $windowid = $words[3];
             }
              else{
               #print "Something is wrong!\n";
               }
}

# make window_width and height divisible by 2 (ffmpeg requires even)
if ($window_width %2 != 0 ){ $window_width = $window_width -1;}
if ($window_height %2 != 0){ $window_height = $window_height -1;} # height problem...

# make a [ ] frame around the window # think about --- if the selected window were to be moved and/or resized???
my $offset = 3;  
my $lenght = 70;
my $colour = "#83857d"; # # maybe remove/fade the shadow a bit
my $mw = MainWindow->new(-background => "$colour",); 
my $abs_yM3 = $abs_y - $offset;    
my $abs_xM3 = $abs_x -   $offset;                                                                   
$mw->geometry("$lenght"."x"."1+$abs_xM3+$abs_yM3");        
$mw->resizable(0,0); 
$mw->overrideredirect(1);

my $mw2 = MainWindow->new(-background => "$colour",);                                                                   
$mw2->geometry("1"."x"."$lenght+$abs_xM3+$abs_yM3");     
$mw2->resizable(0,0); 
$mw2->overrideredirect(1);

my $abs_xPwless70P3 = $abs_x + $window_width - $lenght + $offset; 
my $abs_yPhP3 = $abs_y + $window_height + $offset;   
my $mw3 = MainWindow->new(-background => "$colour",);                                                                   
$mw3->geometry("$lenght"."x"."1+$abs_xPwless70P3+$abs_yPhP3");     
$mw3->resizable(0,0); 
$mw3->overrideredirect(1);

my $abs_xPwP3 = $abs_x + $window_width + $offset; 
my $abs_yPhless70P3 = $abs_y + $window_height -$lenght + $offset;   
my $mw4 = MainWindow->new(-background => "$colour",);                                                                   
$mw4->geometry("1"."x"."$lenght+$abs_xPwP3+$abs_yPhless70P3");     
$mw4->resizable(0,0); 
$mw4->overrideredirect(1);

 # bring the selected window to the front... but keep the frame above all?!

 # save dialog
 my $types = [
['mpg files',        '.mpg',          ],         # MPEG-2 is a high quality video standard used for DVD discs and Digital TV broadcasts. 
['avi files',        '.avi',          ],               # The AVI file created by Microsoft is the primary audio/video format for Windows platforms and may be compressed with a variety of codecs.
['mov files',        '.mov',          ],           # MOV - Apple QuickTime 
['All Files',        '*',             ],
];
## need a proper list of well known ones that ffmpeg can save to...

my $sfile = $mw->getSaveFile( # could look better... # position it
                                          -defaultextension => ".mpg",
                                          -initialdir => "/home/frank/Perl/screencaps", # standardise...
                                          -initialfile => "ScreenCast01",
                                          -title => "ScreenCast Capture file",
                                          -filetypes => $types
                                          ); 

&do_saveFileWithType($sfile) if defined $sfile;

sub do_saveFileWithType {
  my @InboundParameters = @_;
    print "This is what was passed:\t$InboundParameters[0]\n";

   # fix the "if the file is already there problem
   # show the file-extention with the "filename" & tie the filetype/defaultextension to the "Files of type:" selection...
   # all files problem...
 }

# 1. just capture the desktop/selected window -- no sound
my $result2 = ffmpeg ("-f x11grab", "-s $window_width"."x"."$window_height", "-r 25", "-i :0.0+$abs_x,$abs_y", " -sameq", " $sfile"); 

最佳答案

X11::WMCtrl您可以通过编程方式提升特定窗口。

关于linux - xwininfo 将选定的窗口置于最前面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7660646/

相关文章:

linux - 在 AWK 语句中定义一个 perl 变量

循环中的 Perl 脚本在一次运行后结束

perl - 为什么inotify会丢失事件?

当操作系统滚动日志文件时,Node.js 日志记录会出现问题。

linux - 自定义 vi 命令 : How to insert current date with a variable's content?

linux - Linux 中的信号量错误 - :Invalid argument error no :22 (EINVAL)

Perl 浏览页面,替代 Win32::Ole

perl - Perl 的 inet_aton 线程安全吗?

c - 为什么此 ioctl 的 access_ok 失败

linux - 如何在不使用 adduser/useradd 的情况下在 linux 中管理用户