Transfer Phone Numbers from gammu to the Mac Adressbook

Tue, 31. Mar 2009

Categories: en sysadmin Tags: 6610 Addressbook gammu Nokia OS X Ruby vCard

ok, make yourself comfortable, fasten your seatbelt and stop smoking. This will be quite a journey:

  1. Get gammu and backup the phone data:
    $ gammu backup phonedata.txt -yes
  2. open phonedata.txt and save it in „MacRoman“ encoding with Unix line endings
  3. run it through this ruby script:
 1#!/usr/bin/ruby -w -- 
 2
 3# http://www.rubycentral.com/pickaxe/
 4# http://homepage.mac.com/phrogz/CSV2vCard_v2.html
 5
 6records = {}
 7fields = {}
 8
 9eor = /^[ \t]*$/
10bor = /^\[([^0-9\]]+)([0-9]*)\]$/
11etype = /^Entry([0-9]+)Type\s+=\s+(\S+)$/
12etext = /^Entry([0-9]+)Text\s+=\s+"(.*)"/
13
14record_type = nil
15entry_type = nil
16tmp = nil
17
18$stdin.each do |l|
19  if (eor =~ l) == 0
20    records[record_type] = [] if records[record_type].nil?
21    records[record_type] << tmp if tmp
22#   puts '-- EOR --'
23    record_type = entry_type = tmp = nil
24    next
25  end
26  m = bor.match l
27  if m
28#   puts "#{m[1]} - #{m[2]}"
29    record_type = "#{m[1]}"
30    tmp = {}
31    next
32  end
33  if 'PhonePBK' == record_type
34    m = etype.match l
35#   raise "Parse Error: #{l}" if entry_type.nil? && m.nil?
36    if m
37      entry_type = "#{m[2]}"
38    else
39      m = etext.match l
40      if m
41        raise 'Parse Error' if entry_type.nil?
42        if 'Name' == entry_type
43          name = "#{m[2]}".split ' '
44          tmp[entry_type = 'LastName'] = "#{name[name.length-1]}"
45          fields[entry_type] = entry_type
46          name[name.length-1] = nil
47          name.compact!
48
49          count = 0
50          name.each do |tn|
51            tmp[entry_type = "Name_#{count}"] = tn
52            fields[entry_type] = entry_type
53            count += 1
54          end
55        else
56          fields[entry_type] = entry_type
57#         puts "#{entry_type} = #{m[2]}"
58          tmp[entry_type] = "#{m[2]}"
59        end
60        entry_type = nil
61      end
62    end
63  end
64end
65
66keys = fields.keys.sort
67sep = ','
68puts "\##{keys.join(sep)}"
69records['PhonePBK'].each do |item|
70  tmp = ''
71  keys.each do |key|
72    v = item[key]
73    v = '' if v.nil?
74    v.gsub! "'", "''"
75    tmp = "#{tmp}'#{v}'#{sep}"
76  end
77  puts tmp
78end
  1. run the result through a csv to vcard converter,
  2. remove all single quotes ' from the vcards,
  3. import the vcards into the osx addressbook.

And guess what โ€“ that really works! The script is quite a hack but does the job. I thought about rewriting it using closures prior publishing but left it as a homework to you.

[Update] Maybe iSync would have done the trick, too.