1 |
#! perl -w |
2 |
|
3 |
# http://triplefusion.net/system/macosx-clipboard |
4 |
|
5 |
# ---------------------------------------------------------------------- |
6 |
# File: macosx-clipboard |
7 |
# ---------------------------------------------------------------------- |
8 |
# |
9 |
# All portions of code are copyright by their respective author/s. |
10 |
# Copyright (c) 2006 Samuel Ljungkvist <salj@triplefusion.net> |
11 |
# |
12 |
# This program is free software; you can redistribute it and/or modify |
13 |
# it under the terms of the GNU General Public License as published by |
14 |
# the Free Software Foundation; either version 2 of the License, or |
15 |
# (at your option) any later version. |
16 |
# |
17 |
# This program is distributed in the hope that it will be useful, |
18 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
19 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
20 |
# GNU General Public License for more details. |
21 |
# |
22 |
# You should have received a copy of the GNU General Public License |
23 |
# along with this program; if not, write to the Free Software |
24 |
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
25 |
# ---------------------------------------------------------------------- |
26 |
|
27 |
# Usage: |
28 |
|
29 |
# URxvt.perl-ext-common: macosx-clipboard |
30 |
# URxvt.keysym.M-c: perl:macosx-clipboard:copy |
31 |
# URxvt.keysym.M-v: perl:macosx-clipboard:paste |
32 |
|
33 |
use Fcntl (); |
34 |
use Foundation; |
35 |
use locale; |
36 |
use utf8; |
37 |
|
38 |
our($appkit, $pasteboard); |
39 |
|
40 |
$appkit = NSBundle->alloc->init->initWithPath_('/System/Library/Frameworks/AppKit.framework'); |
41 |
$appkit->load if $appkit; |
42 |
|
43 |
if ($appkit->isLoaded) { |
44 |
no strict 'refs'; |
45 |
for my $class (qw(NSPasteboard)) { |
46 |
@{$class . '::ISA'} = 'PerlObjCBridge'; |
47 |
} |
48 |
} else { |
49 |
undef $appkit; |
50 |
} |
51 |
|
52 |
$pasteboard = NSPasteboard->generalPasteboard; |
53 |
|
54 |
sub copy { |
55 |
my ($self) = @_; |
56 |
|
57 |
$pasteboard->declareTypes_owner_(NSArray->arrayWithObject_('NSStringPboardType'), undef); |
58 |
$pasteboard->setString_forType_($self->selection, 'NSStringPboardType'); |
59 |
() |
60 |
} |
61 |
|
62 |
sub paste { |
63 |
my ($self) = @_; |
64 |
my ($type, $str); |
65 |
|
66 |
$type = $pasteboard->availableTypeFromArray_(NSArray->arrayWithObject_('NSStringPboardType')); |
67 |
if ($type->isEqual_('NSStringPboardType')){ |
68 |
$str = $pasteboard->stringForType_($type)->UTF8String; |
69 |
$str =~ tr/\n/\r/; |
70 |
utf8::decode($str); |
71 |
$self->tt_write($self->locale_encode($str)); |
72 |
} |
73 |
() |
74 |
} |
75 |
|
76 |
sub on_user_command { |
77 |
my ($self, $cmd) = @_; |
78 |
|
79 |
if ($cmd eq "macosx-clipboard:copy") { |
80 |
$self->copy; |
81 |
} |
82 |
|
83 |
if ($cmd eq "macosx-clipboard:paste") { |
84 |
$self->paste; |
85 |
} |
86 |
() |
87 |
} |
88 |
|