The above steps failed to produce any output (after hanging on "Updating spreadsheet..." ~30 seconds) on 2,895 local contacts in Contacts 14.0 and Numbers 14.5 under macOS Golden Gate 27.2.1. GPT-6.2 Astra on medium effort one-shotted this AppleScript snippet to convert an exported VCF to CSV, taking less than a second on the same contacts:
-- Apple Contacts vCard 3.0 to UTF-8 CSV. Runs locally using the system Perl.
on run
set sourceFile to choose file with prompt "Choose the .vcf exported by Contacts"
set originalName to name of (info for sourceFile)
if originalName ends with ".vcf" and (length of originalName) > 4 then set originalName to text 1 thru -5 of originalName
set destinationFile to choose file name with prompt "Save the contacts CSV" default name (originalName & ".csv")
set cardCount to do shell script "/usr/bin/perl -e " & quoted form of (my converterSource()) & " -- " & quoted form of (POSIX path of sourceFile) & " " & quoted form of (POSIX path of destinationFile)
display dialog ("Exported " & cardCount & " contacts to:" & return & POSIX path of destinationFile) buttons {"OK"} default button 1
end run
on converterSource()
return "use strict;
use warnings;
use Encode qw(decode FB_CROAK);
use File::Basename qw(dirname);
use File::Temp;
my ($source, $destination) = @ARGV;
die \"Choose a destination ending in .csv.\\n\" unless $destination =~ /\\.csv\\z/i;
open my $input, '<:raw', $source or die \"Cannot read vCard: $!\\n\";
my @names = ('Last name', 'First name', 'Additional names', 'Prefix', 'Suffix');
my @address = ('PO box', 'Extended address', 'Street', 'City', 'Region', 'Postal code', 'Country');
my %title = (FN => 'Full name', NICKNAME => 'Nickname', ORG => 'Organization',
TITLE => 'Job title', TEL => 'Phone', EMAIL => 'Email', ADR => 'Address',
URL => 'Website', BDAY => 'Birthday', NOTE => 'Notes');
my @headers = ('Full name', @names);
my %seen = map { $_ => 1 } @headers;
my (@rows, @properties);
my $inside = 0;
sub unescape {
my ($s) = @_;
$s =~ s/\\\\([nN,;\\\\])/$1 =~ m{^[nN]$} ? \"\\n\" : $1/ge;
return $s;
}
sub parts {
my ($s) = @_;
return map { unescape($_) } $s =~ /((?:\\\\.|[^;\\\\])*)(?:;|\\z)/g;
}
sub finish_card {
my (%row, %count, %labels);
my $version = '';
for my $p (@properties) {
my ($group, $key, undef, $value) = @$p;
$version = $value if $key eq 'VERSION';
if ($key eq 'X-ABLABEL') {
$value = unescape($value);
$value =~ s/^_!<(.+)>!_\\z/$1/;
$labels{$group} = $value;
}
}
die \"Expected a vCard 3.0 export from Apple Contacts.\\n\" unless $version eq '3.0';
my $put = sub {
my ($field, $value) = @_;
push @headers, $field unless $seen{$field}++;
$row{$field} = $value;
};
for my $p (@properties) {
my ($group, $key, $params, $value) = @$p;
next if $key =~ /^(?:VERSION|PRODID|X-ABLABEL|PHOTO|LOGO|SOUND|KEY)$/;
die \"Unsupported encoding in $key. Export a UTF-8 vCard 3.0.\\n\"
if $params =~ /;ENCODING=(?:QUOTED-PRINTABLE|B|BASE64)(?:;|$)/i;
my $i = ++$count{$key};
my $suffix = $i == 1 ? '' : \" $i\";
my $field = ($title{$key} // $key) . $suffix;
if ($key eq 'N' || $key eq 'ADR') {
my @values = parts($value);
my @fields = $key eq 'N' ? @names : @address;
for my $j (0 .. $#fields) {
next unless defined $values[$j] && length $values[$j];
my $name = $key eq 'N' ? $fields[$j].$suffix : \"$field $fields[$j]\";
$put->($name, $values[$j]);
}
} elsif ($key eq 'ORG') {
$put->($field, join(' / ', grep { length } parts($value)));
} else {
$put->($field, unescape($value));
}
my @types = $params =~ /;TYPE=(\"[^\"]*\"|[^;]+)/ig;
s/^\"|\"$//g for @types;
$put->(\"$field type\", join(',', @types)) if @types;
$put->(\"$field label\", $labels{$group}) if exists $labels{$group};
}
push @rows, \\%row;
}
sub line {
my ($bytes) = @_;
return unless length $bytes;
my $s = decode('UTF-8', $bytes, FB_CROAK);
if (uc($s) eq 'BEGIN:VCARD') {
die \"Nested or unfinished vCard.\\n\" if $inside;
@properties = ();
$inside = 1;
} elsif (uc($s) eq 'END:VCARD') {
die \"Unexpected END:VCARD.\\n\" unless $inside;
finish_card();
$inside = 0;
} else {
die \"Content outside a vCard.\\n\" unless $inside;
my ($group, $key, $params, $value) =
$s =~ /^(?:([\\w-]+)\\.)?([\\w-]+)((?:;(?:\"[^\"]*\"|[^\":])*)?):(.*)\\z/s;
die \"Malformed property in card \" . (1 + @rows) . \".\\n\" unless defined $key;
push @properties, [lc($group // ''), uc($key), $params, $value];
}
}
my $pending = '';
while (defined(my $line = <$input>)) {
$line =~ s/^\\xEF\\xBB\\xBF// if $. == 1;
$line =~ s/\\r?\\n\\z//;
if ($line =~ s/^[ \\t]//) {
$pending .= $line;
} else {
line($pending);
$pending = $line;
}
}
line($pending);
close $input or die \"Cannot finish reading vCard: $!\\n\";
die \"Truncated or empty vCard file.\\n\" if $inside || !@rows;
my $output = File::Temp->new(DIR => dirname($destination), UNLINK => 1);
binmode $output, ':encoding(UTF-8)';
sub write_record {
my @cells = map { my $v = $_ // ''; $v =~ s/\"/\"\"/g; qq{\"$v\"} } @_;
print {$output} join(',', @cells), \"\\r\\n\" or die \"CSV write failed: $!\\n\";
}
write_record(@headers);
for my $row (@rows) {
write_record(@{$row}{@headers});
}
close $output or die \"Could not finish CSV: $!\\n\";
rename $output->filename, $destination or die \"Could not save CSV: $!\\n\";
print scalar(@rows);
"
end converterSource
/mac | Oct 16, 2017