| 1 | 1 | ▶ | use 5.20.0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | package App::Addex::AddressBook::AppleScript 0.009; | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use parent qw(App::Addex::AddressBook); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use experimental 'postderef'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | # ABSTRACT: Mac::Glue-less Addex adapter for Apple Address Book and Addex | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | #pod =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 11 | #pod | ||||||||||||||||||||||||||||
| 12 | #pod This module implements the L<App::Addex::AddressBook> interface for Mac OS X's | ||||||||||||||||||||||||||||
| 13 | #pod Address Book application, using I<a horrible hack> to get entries from the | ||||||||||||||||||||||||||||
| 14 | #pod address book. | ||||||||||||||||||||||||||||
| 15 | #pod | ||||||||||||||||||||||||||||
| 16 | #pod A much cleaner interface would be to use L<App::Addex::AddressBook::Apple>, | ||||||||||||||||||||||||||||
| 17 | #pod which uses L<Mac::Glue> to access the address book. Unfortunately, Mac::Glue | ||||||||||||||||||||||||||||
| 18 | #pod does not work in many recent builds of Perl, and will cease to work as the | ||||||||||||||||||||||||||||
| 19 | #pod Carbon API is killed off. | ||||||||||||||||||||||||||||
| 20 | #pod | ||||||||||||||||||||||||||||
| 21 | #pod The AppleScript adapter builds an AppleScript program that prints out a dump of | ||||||||||||||||||||||||||||
| 22 | #pod relevant address book entries, then runs it, then parses its output. The | ||||||||||||||||||||||||||||
| 23 | #pod format of the intermediate form may change for all kinds of crazy reasons. | ||||||||||||||||||||||||||||
| 24 | #pod | ||||||||||||||||||||||||||||
| 25 | #pod =cut | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 1 | ▶ | use App::Addex::Entry; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 1 | ▶ | use App::Addex::Entry::EmailAddress; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 1 | ▶ | use Encode (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 1 | ▶ | use File::Temp (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 0 | ▶ | sub _produce_applescript { | ||||||||||||||||||||||||||
|
Subroutine
_produce_applescript: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 34 | 0 | my @fields = ( | |||||||||||||||||||||||||||
| 35 | 'company', # true / false | ||||||||||||||||||||||||||||
| 36 | 'organization', | ||||||||||||||||||||||||||||
| 37 | 'first name', | ||||||||||||||||||||||||||||
| 38 | 'middle name', | ||||||||||||||||||||||||||||
| 39 | 'last name', | ||||||||||||||||||||||||||||
| 40 | 'nickname', | ||||||||||||||||||||||||||||
| 41 | 'suffix', | ||||||||||||||||||||||||||||
| 42 | 'note', | ||||||||||||||||||||||||||||
| 43 | ); | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 0 | my $dumper = ''; | |||||||||||||||||||||||||||
| 46 | 0 | for my $field (@fields) { | |||||||||||||||||||||||||||
| 47 | 0 | $dumper .= <<"END_FIELD_DUMPER"; | |||||||||||||||||||||||||||
| 48 | set _this to get $field of _person | ||||||||||||||||||||||||||||
| 49 | if $field of _person is not missing value then | ||||||||||||||||||||||||||||
| 50 | set _answer to _answer & "- BEGIN $field\n" | ||||||||||||||||||||||||||||
| 51 | set _answer to _answer & ($field of _person) & "\n" | ||||||||||||||||||||||||||||
| 52 | set _answer to _answer & "- END $field\n" | ||||||||||||||||||||||||||||
| 53 | end if | ||||||||||||||||||||||||||||
| 54 | END_FIELD_DUMPER | ||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 0 | my $osascript = <<'END_APPLESCRIPT'; | |||||||||||||||||||||||||||
| 58 | tell application "Address Book" | ||||||||||||||||||||||||||||
| 59 | set _people to (get people) | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | set _answer to "" | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | repeat with _person in _people | ||||||||||||||||||||||||||||
| 64 | repeat 1 times | ||||||||||||||||||||||||||||
| 65 | if count of email of _person = 0 then | ||||||||||||||||||||||||||||
| 66 | exit repeat | ||||||||||||||||||||||||||||
| 67 | end if | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | set _answer to _answer & "--- BEGIN " & id of _person & "\n" | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | $dumper | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | set _answer to _answer & "- BEGIN email\n" | ||||||||||||||||||||||||||||
| 74 | repeat with _email in (get email of _person) | ||||||||||||||||||||||||||||
| 75 | set _answer to _answer & (label of _email) & "\n" | ||||||||||||||||||||||||||||
| 76 | set _answer to _answer & (value of _email) & "\n" | ||||||||||||||||||||||||||||
| 77 | end repeat | ||||||||||||||||||||||||||||
| 78 | set _answer to _answer & "- END email\n" | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | set _answer to _answer & "--- END " & id of _person & "\n\n" | ||||||||||||||||||||||||||||
| 81 | end repeat | ||||||||||||||||||||||||||||
| 82 | end repeat | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | _answer | ||||||||||||||||||||||||||||
| 85 | end tell | ||||||||||||||||||||||||||||
| 86 | END_APPLESCRIPT | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 0 | $osascript =~ s/\$dumper/$dumper/; | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 0 | return $osascript; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 0 | ▶ | sub _produce_scriptfile { | ||||||||||||||||||||||||||
|
Subroutine
_produce_scriptfile: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 94 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 0 | my $osascript = $self->_produce_applescript; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 0 | my ($fh, $filename) = File::Temp::tempfile(UNLINK => 1); | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 0 | $fh->print($osascript); | |||||||||||||||||||||||||||
| 101 | 0 | ▶ | $fh->close or die "can't close $filename: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 0 | return $filename; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 0 | ▶ | sub entries { | ||||||||||||||||||||||||||
|
Subroutine
entries: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||||||
| 107 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 0 | my $script = $self->_produce_scriptfile; | |||||||||||||||||||||||||||
| 110 | 0 | my @output = `/usr/bin/osascript $script`; | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 0 | @output = map {; Encode::decode('utf-8', $_) } @output; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 0 | my @people; | |||||||||||||||||||||||||||
| 115 | my $this; | ||||||||||||||||||||||||||||
| 116 | 0 | ▶ | LINE: while (my $line = shift @output) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 0 | ▶ | unless ($this) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 0 | ▶ | next LINE unless $line =~ /\A--- BEGIN (.+)\Z/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 0 | $this = { id => $1 }; | |||||||||||||||||||||||||||
| 120 | 0 | push @people, $this; | |||||||||||||||||||||||||||
| 121 | 0 | next LINE; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 0 | my @input; | |||||||||||||||||||||||||||
| 125 | 0 | ▶ | if ($line =~ /\A- BEGIN (.+)\Z/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 0 | my $field = $1; | |||||||||||||||||||||||||||
| 127 | 0 | ▶ | push @input, shift @output until @input and $input[-1] =~ /\A- END $1\Z/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@input and $input[-1] =~ /\A- END $1\Z/u
MC/DC0%0/2
@input and $input[-1] =~ /\A- END $1\Z/u
@input
$input[-1] =~ /\A- END $1\Z/u
Truth table0%0/3
A and B
A= @inputB= $input[-1] =~ /\A- END $1\Z/u
| |||||||||||||||||||||||||||||
| 128 | 0 | pop @input; | |||||||||||||||||||||||||||
| 129 | 0 | $this->{ $field } = join q{}, @input; | |||||||||||||||||||||||||||
| 130 | 0 | chomp $this->{ $field }; | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 0 | ▶ | if ($field eq 'email') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | 0 | $this->{emails} = [ split /\n/, delete $this->{email} ]; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 0 | ▶ | if ($line =~ /\A--- END \Q$this->{id}\E\Z/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 0 | undef $this; | |||||||||||||||||||||||||||
| 139 | 0 | next LINE; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 0 | my @entries = map {; $self->_entrify($_) } @people; | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 0 | return @entries; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 0 | ▶ | sub _entrify { | ||||||||||||||||||||||||||
|
Subroutine
_entrify: not called CC=24 SCAR=64.0
| |||||||||||||||||||||||||||||
| 149 | 0 | my ($self, $person) = @_; | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 0 | my %fields; | |||||||||||||||||||||||||||
| 152 | 0 | ▶ | if (my $note = $person->{note} // '') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$person->{"note"} // ""
MC/DC0%0/1
$person->{"note"} // ""
$person->{"note"}
Truth table0%0/2
A
A= $person->{"note"}
| |||||||||||||||||||||||||||||
| 153 | 0 | my @lines = grep { length } split /\R/, $note; | |||||||||||||||||||||||||||
| 154 | 0 | for my $line (@lines) { | |||||||||||||||||||||||||||
| 155 | 0 | ▶ | next if $line =~ /^--/; # comment | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | my $tmpname | ||||||||||||||||||||||||||||
| 158 | = join q{ }, grep $_, | ||||||||||||||||||||||||||||
| 159 | 0 | $person->@{'first name', 'middle name', 'last name', 'suffix'}; | |||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 0 | ▶ | warn("bogus line in notes on $tmpname: $line\n"), next | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | unless $line =~ /\A([^:]+):\s*(.+?)\Z/; | ||||||||||||||||||||||||||||
| 163 | 0 | $fields{$1} = $2; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 0 | ▶ | my $fname = $person->{'first name'} // ''; | ||||||||||||||||||||||||||
|
Condition0%0/2
$person->{"first name"} // ""
MC/DC0%0/1
$person->{"first name"} // ""
$person->{"first name"}
Truth table0%0/2
A
A= $person->{"first name"}
| |||||||||||||||||||||||||||||
| 168 | 0 | ▶ | my $mname = $person->{'middle name'} // ''; | ||||||||||||||||||||||||||
|
Condition0%0/2
$person->{"middle name"} // ""
MC/DC0%0/1
$person->{"middle name"} // ""
$person->{"middle name"}
Truth table0%0/2
A
A= $person->{"middle name"}
| |||||||||||||||||||||||||||||
| 169 | 0 | ▶ | my $lname = $person->{'last name'} // ''; | ||||||||||||||||||||||||||
|
Condition0%0/2
$person->{"last name"} // ""
MC/DC0%0/1
$person->{"last name"} // ""
$person->{"last name"}
Truth table0%0/2
A
A= $person->{"last name"}
| |||||||||||||||||||||||||||||
| 170 | 0 | ▶ | my $suffix = $person->{suffix} // ''; | ||||||||||||||||||||||||||
|
Condition0%0/2
$person->{"suffix"} // ""
MC/DC0%0/1
$person->{"suffix"} // ""
$person->{"suffix"}
Truth table0%0/2
A
A= $person->{"suffix"}
| |||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 0 | ▶ | $mname = '' unless $fields{'use middle'} // 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$fields{"use middle"} // 1
MC/DC0%0/1
$fields{"use middle"} // 1
$fields{"use middle"}
Truth table0%0/2
A
A= $fields{"use middle"}
| |||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 0 | my $name; | |||||||||||||||||||||||||||
| 175 | 0 | ▶ | if ($person->{company} eq 'true') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | 0 | $name = $person->{organization}; | |||||||||||||||||||||||||||
| 177 | } else { | ||||||||||||||||||||||||||||
| 178 | 0 | ▶ | $name = $fname | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 179 | . (length $mname ? " $mname" : '') | ||||||||||||||||||||||||||||
| 180 | . (length $lname ? " $lname" : '') | ||||||||||||||||||||||||||||
| 181 | . (length $suffix ? " $suffix" : ''); | ||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 0 | ▶ | unless (length $name) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 185 | 0 | warn "couldn't figure out a name for this entry\n"; | |||||||||||||||||||||||||||
| 186 | 0 | return; | |||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 0 | my @emails; | |||||||||||||||||||||||||||
| 190 | 0 | my @kv = @{ $person->{emails} }; | |||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 0 | ▶ | for (my $i = 0; $i < $#kv; $i += 2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 193 | 0 | push @emails, App::Addex::Entry::EmailAddress->new({ | |||||||||||||||||||||||||||
| 194 | address => $kv[ $i + 1 ], | ||||||||||||||||||||||||||||
| 195 | label => $kv[ $i ], | ||||||||||||||||||||||||||||
| 196 | }); | ||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | CHECK_DEFAULT: { | ||||||||||||||||||||||||||||
| 200 | 0 | ▶ | if (@emails > 1 and my $default = $fields{default_email}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@emails > 1 and my $default = $fields{"default_email"}
MC/DC0%0/2
@emails > 1 and my $default = $fields{"default_email"}
@emails > 1
my $default = $fields{"default_email"}
Truth table0%0/3
A and B
A= @emails > 1B= my $default = $fields{"default_email"}
| |||||||||||||||||||||||||||||
| 201 | 0 | my $check; | |||||||||||||||||||||||||||
| 202 | 0 | ▶ | if ($default =~ m{\A/(.+)/\z}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 203 | 0 | $default = qr/$1/; | |||||||||||||||||||||||||||
| 204 | 0 | ▶ | $check = sub { $_[0]->address =~ $default }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 205 | } else { | ||||||||||||||||||||||||||||
| 206 | 0 | ▶ | $check = sub { $_[0]->label eq $default }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | 0 | for my $i (0 .. $#emails) { | |||||||||||||||||||||||||||
| 210 | 0 | ▶ | if ($check->($emails[$i])) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 211 | 0 | ▶ | unshift @emails, splice @emails, $i, 1 if $i != 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 212 | 0 | last CHECK_DEFAULT; | |||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | 0 | warn "no email found for $name matching $fields{default_email}\n"; | |||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | my $arg = { | ||||||||||||||||||||||||||||
| 221 | name => $name, | ||||||||||||||||||||||||||||
| 222 | nick => $person->{nickname}, | ||||||||||||||||||||||||||||
| 223 | 0 | emails => \@emails, | |||||||||||||||||||||||||||
| 224 | fields => \%fields, | ||||||||||||||||||||||||||||
| 225 | }; | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | 0 | return App::Addex::Entry->new($arg); | |||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | 1 | 1; | |||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | __END__ | ||||||||||||||||||||||||||||