changeset 8:37b9a5119474

add apple address book (Tiger 10.4)
author kono
date Sat, 07 May 2005 23:25:45 +0900
parents 13949e4d6f18
children 798ba47e8046
files Calcon.pm calcon.pl
diffstat 2 files changed, 192 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/Calcon.pm	Sun Mar 02 18:59:57 2003 +0900
+++ b/Calcon.pm	Sat May 07 23:25:45 2005 +0900
@@ -1954,12 +1954,21 @@
 	    my $d = $self->{'-script-name'}++;
 	    $self->close() if ( $self->{'-telling'} );
 	    $self->{'-telling'} = 0;
+            if (1) {
 	    if ($self->{'-file-out'}) {
 		open OUT,"> script-out/$d.script" or croak($!);
 	    } else {
 		print STDERR "doing $i\n";
 		open OUT,"| osascript " or cloak($!);
 	    }
+            } else {
+	    if ($self->{'-file-out'}) {
+		open OUT,"> script-out/$d.script" or croak($!);
+	    } else {
+		print STDERR "doing $i\n";
+		open OUT,"| osascript " or cloak($!);
+	    }
+            }
 	    select OUT;
 	}
     }
@@ -1974,6 +1983,7 @@
 	$data =~ s/\356\277/  /g;
 	$data =~ s/([^\200-\377])\\/$1\200/g;
 	# $data =~ s/\201/\/g;
+        $data =~ s/\\/\\\\/g;
 	print $data;
     }
 }
@@ -2042,7 +2052,7 @@
     return if(! defined $record{'name'});
     $tab .= '    ';
 
-    $self->print("with transaction\n");
+    # $self->print("with transaction\n");
     if(defined $record{'office'}) {
 	my $group = $record{'office'};
 	$self->print($tab,"if not exists some group whose name is ");
@@ -2127,14 +2137,14 @@
 	    $self->print($record{'office'},"\"\n");
 	    $tab =~ s/    $//;
 	    $self->print($tab,"end\n");
-	    $self->print("end transaction\n");
+	    # $self->print("end transaction\n");
 	    $self->{'-groups'}->{$record{'office'}} = 1;;
 	    return;
 	}
     }
     $tab =~ s/    $//;
     $self->print($tab,"end tell\n");
-    $self->print("end transaction\n");
+    # $self->print("end transaction\n");
 }
 
 sub check_2byte {
@@ -2210,9 +2220,9 @@
 	$self->print($tab,"end\n");
     }
     $self->print("close group\n");
-    $self->print("with transaction\n");
+    # $self->print("with transaction\n");
     $self->print("save addressbook\n");
-    $self->print("end transaction\n");
+    # $self->print("end transaction\n");
     $self->print("quit saving yes\n");
     $self->print("end tell\n");
 }
@@ -2355,7 +2365,7 @@
     my $data =  $record{'name'}; 
     @names = split(/ +/,$data);
 
-    $self->print("with transaction\n");
+    # $self->print("with transaction\n");
     $tab .= '    ';
 
 
@@ -2440,7 +2450,7 @@
     }
     $tab =~ s/    $//;
     $self->print($tab,"end tell\n");
-    $self->print("end transaction\n");
+    # $self->print("end transaction\n");
 }
 
 sub birth_date {
@@ -2978,10 +2988,10 @@
 	    $last = $first = $last_yomi = $first_yomi = '';
 	    ($last,$first) =  split(/ /,$record{'name'});
 	    ($last_yomi,$first_yomi) = split(/ /,$record{'name-yomi'}),
-	    print YOMI $last,"\n";
-	    print YOMI $last_yomi,"\n";
-	    print YOMI $first,"\n";
-	    print YOMI $first_yomi,"\n";
+#	    print YOMI $last,"\n";
+#	    print YOMI $last_yomi,"\n";
+#	    print YOMI $first,"\n";
+#	    print YOMI $first_yomi,"\n";
 	}
 
 	# print "fn:$data\n" if($data);
@@ -3032,7 +3042,7 @@
     } 
     if(defined $record{'birth'}) {
 	$data =  $record{'birth'};
-	print "bday:$data\n" if($data);
+	print "bday:".$data->date()."\n" if($data);
     }
     if(defined $record{'name-yomi'}) {
 	$data =  $record{'name-yomi'};
@@ -3048,6 +3058,168 @@
 
 #######################################################################/
 
+package Calcon::Vcard_Apple_write;
+use strict;
+# use warnings;
+use NKF;
+
+# VCARD ·Á¼°
+
+use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
+@ISA = ( 'Calcon::Vcard_write' );
+
+# Mac OS X 10.3 's Address Book requires utf-16
+# | nkf -w16
+#
+
+sub initialize {
+    my ($self) = @_;
+    $self->SUPER::initialize();
+}
+
+sub print {
+    my ($self,@data) = @_;
+    foreach (@data) {
+	chop;
+	s/\015/\\n/g;
+	s/\012//g;
+	print "$_\n";
+    }
+}
+
+sub vcard {
+    my ($self,$keys,$record) = @_;
+    my (%record) = %{$record};
+    my $data;
+    $self->{'item'} = 1;
+
+    if(defined($record{'office'})) {
+	$record{'office'} = 'etc' if(! $record{'office'}) ;
+    }
+    if(defined($record{'name-yomi'})) {
+	$record{'name-yomi'} =~ s/^ *//;
+    }
+    if(defined($record{'office-yomi'})) {
+	$record{'office-yomi'} =~ s/^ *//;
+    }
+    $self->print("begin:vcard\n");
+    $self->print("version:3.0\n");
+    if(defined $record{'name'}) {
+	$data =  $record{'name'};
+	$self->print("FN:$data\n") if($data);
+	if(0 && defined $record{'name-yomi'}) {
+	    $data = join(";",split(/ /,$record{'name-yomi'}));
+	    $self->print("N:$data\n") if($data);
+	} else {
+	    $data = join(";",split(/ /,$data));
+	    $self->print("N:$data\n") if($data);
+	}
+	if(defined $record{'name-yomi'}) {
+	    my ($last , $first , $last_yomi , $first_yomi );
+	    $last = $first = $last_yomi = $first_yomi = '';
+	    ($last,$first) =  split(/ /,$record{'name'});
+	    ($last_yomi,$first_yomi) = split(/ /,$record{'name-yomi'}),
+	    $self->print("X-PHONETIC-LAST-NAME:$last_yomi\n");
+	    $self->print("X-PHONETIC-FIRST-NAME:$first_yomi\n");
+	}
+
+	# print "fn:$data\n" if($data);
+	# if(defined $record{'office'}) {
+	#     $data = $data.";".$record{'office'};
+	# }
+	# print "n:$data\n" if($data);
+    }
+#    my $cat = 0;
+#    if(defined $record{'group'}) {
+#	 $data = "$record{'group'}";
+#	$self->print("CATEGORIES:$data\n") if($data);
+#    }
+    if(defined $record{'office'}) {
+	 $data = "$record{'office'}";
+	$self->print("org:$data\n") if($data);
+#	if ($cat==0) {
+#	    $self->print("CATEGORIES:$data\n") if($data);
+#	}
+    }
+    my $title;
+    if(defined $record{'section'}) {
+	$title = $record{'section'};
+    }
+    if(defined $record{'title'}) {
+	 $title .= ($title?'\n':'')."$record{'title'}";
+	$self->print("title:$data\n") if($data);
+    }
+    if(defined $record{'address'}) {
+	my $i = $self->{'item'}++;
+	my ($adr1 , $adr2 , $adr_state , $adr_zip , $adr_country ); 
+	$adr1 = $adr2 = $adr_state = $adr_zip = $adr_country = '';
+	$data =  $record{'address'};
+	$adr1 =  $record{'address'};
+# ADD:ÈÖÃÏ;;Į¼;²­Æì;903-0213;ÆüËÜ
+	if(defined $record{'zip'}) {
+	    $adr_zip = $record{'zip'};
+	}
+$self->print("item$i.ADR;type=work;type=pref:;;$adr1;$adr2;$adr_state;$adr_zip;$adr_country\n") if ($data);
+    }
+    if(defined $record{'tel'}) {
+	my $i = $self->{'item'}++;
+	$data =  $record{'tel'};
+	$self->print("item$i.TEL;type=work:$data\n") if($data);
+    }
+    if(defined $record{'home-tel'}) {
+	my $i = $self->{'item'}++;
+	$data =  $record{'tel'};
+	$self->print("item$i.TEL;type=home:$data\n") if($data);
+    }
+    if(defined $record{'tel2'}) {
+	my $i = $self->{'item'}++;
+	$data =  $record{'tel2'};
+	$self->print("item$i.tel;type=cell:$data\n") if($data);
+    }
+    if(defined $record{'fax'}) {
+	my $i = $self->{'item'}++;
+	$data =  $record{'fax'};
+	$self->print( "item$i.TEL;type=pref:$data\n") if($data);
+    }
+    if(defined $record{'home-address'}) {
+	my $i = $self->{'item'}++;
+	my ($adr1 , $adr2 , $adr_state , $adr_zip , $adr_country ); 
+	$adr1 = $adr2 = $adr_state = $adr_zip = $adr_country = '';
+	$data =  $record{'home-address'};
+	$adr1 =  $record{'home-address'};
+# ADD:ÈÖÃÏ;;Į¼;²­Æì;903-0213;ÆüËÜ
+	if(defined $record{'home-zip'}) {
+	    $adr_zip = $record{'home-zip'};
+	}
+$self->print("item$i.ADR;type=home;type=pref:;;$adr1;$adr2;$adr_state;$adr_zip;$adr_country\n") if ($data);
+    }
+    if(defined $record{'mail'}) {
+	my $i = $self->{'item'}++;
+	$data =  $record{'mail'};
+	$self->print("item$i.EMAIL;type=INTERNET;type=pref:$data\n") if($data);
+    } 
+    if(defined $record{'birth'}) {
+	$data =  $record{'birth'};
+	$self->print("bday:".$data->date()."\n") if($data);
+    }
+    if(defined $record{'memo'}) {
+	$data =  $record{'memo'};
+	$self->print("NOTE:$data\n") if($data);
+    }
+#    if(defined $record{'name-yomi'}) {
+#	$data =  $record{'name-yomi'};
+#	$self->print "x-custom1:$data\n" if($data);
+#    }
+#    if(defined $record{'office-yomi'}) {
+#	$data =  $record{'office-yomi'};
+#	$self->print "x-custom2:$data\n" if($data);
+#    }
+    $self->print("end:vcard\n");
+    $self->print("\n");
+}
+
+#######################################################################/
+
 package Calcon::File_read;
 use strict;
 # use warnings;
@@ -3108,6 +3280,7 @@
 	} else {
 	    $key = 'memo';
 	}
+	$key = 'memo' if ($key eq '');
         if ($key eq 'Subject') {
 	    $key = 'memo';
         }
@@ -3719,6 +3892,7 @@
 
     while(<F>) {
 if (/^begin:\s*vcalendar/i) {
+} elsif (/^begin:\s*vcad/i) {
 } elsif (/^adr(.*):\s*(.*)/i) { $self->items($record,'address',$1,$2);
 } elsif (/^bday:\s*(.*)/i) { $record->{'birth'} = $self->make_date($1);
 } elsif (/^begin:\s*vcard/i) {  $record = $self->make_record;
@@ -3739,14 +3913,18 @@
 } elsif (/^n:\s*(.*)/i) {  $self->name($record,split(/;/,$1));
 } elsif (/^org:\s*(.*)/i) { $record->{'office'} = $1;
 } elsif (/^sequence:\s*(.*)/i) { $record->{'sequence'} = $1;
+} elsif (/^categories:\s*(.*)/i) { $record->{'group'} = $1;
 } elsif (/^summary:\s*(.*)/i) { $record->{'summary'} = $1;
 } elsif (/^tel(.*):\s*(.*)/i) { $self->items($record,'tel',$1,$2);
 } elsif (/^title:\s*/i) { $record->{'title'} = $1;
 } elsif (/^version:\s*(.*)/i) { $record->{'version'} = $1;
+} elsif (/^X-PHONETIC-LAST-NAME:\s*(.*)/i) { $record->{'name-yomi'} .= "$1 ";
+} elsif (/^X-PHONETIC-FIRST-NAME:\s*(.*)/i) { $record->{'name-yomi'} .= "$1 ";
 } elsif (/^x-custom1:\s*(.*)/i) { $record->{'name-yomi'} = $1;
 } elsif (/^x-custom2:\s*(.*)/i) { $record->{'office-yomi'} = $1;
 } elsif (/^x-wr-calname.*:\s*(.*)/i) { $record->{'calendar'} = $1;
 } elsif (/^x-wr-timezone.*:\s*(.*)/i) { $record->{'timezone'} = $1;
+} elsif (/^item\d+(.*):\s*(.*)/i) { $self->items($record,'address',$1,$2);
 } else { $record->{'extra'} .= $_;
 }
     }
--- a/calcon.pl	Sun Mar 02 18:59:57 2003 +0900
+++ b/calcon.pl	Sat May 07 23:25:45 2005 +0900
@@ -24,6 +24,7 @@
     'SLA300'	=> 'Calcon::Sla300_write',
   'AppleScript'	=> 'Calcon::iApp_write',
     'iApp' 	=> 'Calcon::iApp_write',
+ 'Address Book' => 'Calcon::Vcard_Apple_write',
   'Entourage'	=> 'Calcon::Entourage_write',
     'Print'     => 'Calcon::Print_write',
     'File' 	=> 'Calcon::File_write',
@@ -116,6 +117,7 @@
     vCal/vCard
     Zaurus SLA-300
     Entourage via Applescript
+    Address Book  (Mac OS X 10.4 Address Book)
 
  -a addres only
  -c calendar only