Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Take screenshot of all active i3 workspaces
#!/usr/bin/env perl
use strict;
use warnings;
use 5.010;
use JSON qw( decode_json ); # isn't default module, must be installed via CPAN or your system package manager
use Time::HiRes qw ( usleep ); # for simple delays
my $outputfile = shift; # getting output filename from command-line params
my $command = 'i3-msg -t get_workspaces'; # console command used to get the list of active workspaces
my $fileprefix = '/tmp/i3shot-'; # prefix for temporary screenshots
my $json = `$command`;
my $decoded = decode_json($json);
foreach my $workspace (@{$decoded}) {
# get the name of current workspace
my $current = $workspace->{'name'};
# move there
`i3-msg workspace $current`;
# a short delay — some windows couldn't be drawn in a moment, and we get a rubbish
usleep(200000);
# and take screenshot of all the display and save it to temporary file
`import -window root ${fileprefix}${current}.png`;
}
# montage — an utility from ImageMagick to combine multiple images to a single one
# parameters' values: -geometry — we don't need neither resizes nor shifts; -tile — put all images in a single row
`montage ${fileprefix}*.png -geometry +0+0 -tile x1 ${outputfile}`;
@Hi-Angel
Copy link

Hi-Angel commented Oct 6, 2017

Broken for me. ./i3shot.pl output.png produces files:

  1. i3shot-1, i3shot-2, i3shot-6, and i3shot-7.png, whereas active are workspaces are (counting from 1) 1,2,3, 6, 7.
  2. output.png which is the shot of the 7-th workspace.

@Hi-Angel
Copy link

Hi-Angel commented Oct 6, 2017

I tried debugging, and I tnink it have something to do with unicode. I changed the code to

#!/usr/bin/env perl

use strict;
use warnings;
use 5.010;
use Encode;

use JSON qw( decode_json );     # isn't default module, must be installed via CPAN or your system package manager
use Time::HiRes qw ( usleep );  # for simple delays

my $outputfile = shift;                       # getting output filename from command-line params
my $command    = 'i3-msg -t get_workspaces';  # console command used to get the list of active workspaces
my $fileprefix = '/tmp/i3shot-';              # prefix for temporary screenshots

my $json = `$command`;
my $decoded = decode_json($json);

foreach my $workspace (@{$decoded}) {
	# get the name of current workspace
	my $current = $workspace->{'name'};
	decode('UTF-8', print "$current");
}

And I'm getting

λ ./i3shot.pl output.png
Wide character in print at ./i3shot.pl line 22.
1 2 3 6 7

Something odd is going on, because when I'm printing the unicode characters directly, being copied from gucharmap into the print — I have no the error.

I almost never worked with perl though, so I'm not sure of its corner cases. For the record, my workspaces code in i3/config is:

set $tag1 "1 "
set $tag2 "2 "
set $tag3 "3 "
set $tag4 "4 "
set $tag5 "5 "
set $tag6 "6 "

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment