Skip to content

Instantly share code, notes, and snippets.

@z448
Last active February 18, 2017 12:58
Show Gist options
  • Save z448/d1c088060205aa0fb04f14bf08bf8c74 to your computer and use it in GitHub Desktop.
Save z448/d1c088060205aa0fb04f14bf08bf8c74 to your computer and use it in GitHub Desktop.
Clipboard.pm
package Clipboard;
use Exporter 'import';
our @EXPORT_OK = qw( clip );
our $VERSION = '0.03';
use warnings;
use strict;
sub os {
open my $pipe,"-|",'uname -a';
while(<$pipe>){
if(/iPhone/){ return 'iPhone' }
else { return $^O }
}
}
my $os = os();
sub tool {
my $tool = {
linux => [ 'xsel', 'xclip' ],
iPhone => [ 'pbpaste', 'pbcopy' ],
darwin => [ 'pbpaste', 'pbcopy' ],
msys => [ '/dev/clipboard' ],
};
my @tool = ();
for( @{$tool->{$os}} ){ if(`which $_`){ push @tool, $_ } }
return \@tool;
}
sub clip {
my @tool = @{tool()};
my $clip = sub {
my $string = shift || undef;
unless( $string ){
if($os eq 'iPhone'){ return ios_clip() }
my $read = `$tool[0]`; chomp($read); return $read }
else {
return system("echo $string | $tool[1]") }
};
# return $clip;
};
sub ios_clip {
#my $write = sub {
#my $tool = tool();
#my $string = shift || undef;
my $load = eval {
require Mac::PropertyList;
Mac::PropertyList->import();
#Mac::PropertyList->import('parse_plist', 'as_perl');
1;
};
if($load){
my( $data ) = ();
my $c = '/private/var/mobile/Library/Caches/com.apple.UIKit.pboard/pasteboardDB';
{
local $/;
open(my $fh,"<",$c) || die "cant open $c: $!";
$data = <$fh>; close $fh;
}
my $plist = Mac::PropertyList::parse_plist( $data );
for(@{$plist}){
my $s = $_->as_perl;
unless($s eq 1){
if($s->{name} eq 'com.apple.UIKit.pboard.general'){
for(@{$s->{items}->{mobile}}){
return $_->{'public.utf8-plain-text'};
}
}
}
}
} else {
print "in pssss";
open my $psss, '>&', STDOUT;
push my @clip, `pbpaste`; pop @clip;
open STDOUT, '>&', $psss;
return sub { join('',@clip) }
}
}
1;
=head1 create clipboard
my $c = clip();
=head1 read from clipboard
print $c->();
=head1 write to clipboard
#print $c->('text')
=cut
use v5.10;
use strict;
use warnings;
my $clipboard = ' ';
### output goes to terminal but wont be in $clipboard
open my $p,'-|',"pbpaste";
while(<$p>){ $clipboard = $clipboard . $_ }
close $p;
print "[$clipboard]";
### redirect STDOUT into log.txt
open my $STDOLD, '>&', STDOUT;
open STDOUT, '>>', 'log.txt';
### goes only to log.txt
say 'This should be logged to log.txt.';
### using sh shell gives error: /usr/bin/pbpaste: /usr/bin/pbpaste: cannot execute binary file
`sh pbpaste`;
### on ios output goes only to terminal, not into log.txt
`bash -c pbpaste`;
### output goes to terminal but is logged into log.txt
open my $p,'-|',"pbpaste";
while(<$p>){ $clipboard .= $_ }
close $p;
### change output back to STDOUT
open STDOUT, '>&', $STDOLD;
say 'This should show in the terminal';
### todo add Mac::PropertyList + rip clipoard from clipboard file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment