Parsing plain text from clipboard to .VCF

I’m looking for some guidance on how I could go about creating a script that would allow me to Parse plain clipboard text to a VCF format, on the fly.

This script would allow me to Copy google map entries to the Clipboard, Export them as a VCF, and easily import them using that file. I only need it to parse one entry. Here is an example of the copied data from maps.google.com

I don’t know if there is an easier method of accomplishing what I’m looking for… but I could not seem to find anything upon checking macscripter forums, nor searching the web.

From Actual Map

From Side Menu

Hello.

Please post a VCard file that is acceptable to whatever program you are going to import it into, preferable with the data from the example above, and I’ll see what I can do. (Open the vcard file by rightclicking, then select TextEdit or whatever editor you use, and edit/paste it from there.) Please also check that you got the example file imported and everything is correct. So that the fields works for you. There are miscellanous Vcard formats out there.

I have added one of the examples below to the program I am using. Exported it, deleted it from program, and imported it with all fields match exactly. Here is the exported CSV file. As you can see, when I copy the text from maps.google.com it adds “more infoŽ” to the end of the Company Name.

Ex. Unity Telecommore infoŽ
Unity Telecommore infoŽ

I omitted the more info when I entered the contact info.

Hello.

Try the script below It looks rather ugly, but the file produced are taken for a vcard file as long as you remember to add the vcf extension to its name.

on run
    main()
end run
on main()
    set withoutHomepage to false
    try
        set VcardData to paragraphs of (get the clipboard)
        if item 1 of VcardData is "" then set VcardData to items 2 thru -1 of VcardData
        set lineCount to (count VcardData)
        if lineCount < 5 then
            if lineCount = 4 then
                set withoutHomepage to true
            else
                display alert "To few lines on clipboard to generate VCard file with..."
                error number -128
            end if
        end if
    on error
        display alert "To few lines on clipboard to generate VCard file with..."
        error number -128
    end try
    set preamble to "BEGIN:VCARD
VERSION:3.0
N:;;;;
"
    
    set companyName to item 1 of VcardData as text
    set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "more info"}
    set companyName to text item 1 of companyName as text
    set AppleScript's text item delimiters to tids
    set namefield to "FN:" & companyName & "
ORG:" & companyName & "
"
    
    set phoneNumberdata to item 4 of VcardData
    set AppleScript's text item delimiters to {"(", ")"}
    set phoneNumberdata to text items of phoneNumberdata
    set AppleScript's text item delimiters to " "
    set phoneNumberdata to phoneNumberdata as text
    set phoneNumberdata to text items of phoneNumberdata
    set phoneNumber to ""
    repeat with i from 1 to (count phoneNumberdata)
        if item i of phoneNumberdata ≠ "" then
            set phoneNumber to phoneNumber & item i of phoneNumberdata & "-"
        end if
    end repeat
    set AppleScript's text item delimiters to tids
    set phoneNumber to "TEL;type=WORK;type=VOICE;type=pref:" & text 1 thru -2 of phoneNumber & "
"
    set address to "ADR;type=WORK;type=pref:;;" & item 2 of VcardData & ";"
    set AppleScript's text item delimiters to ","
    set stateaddress to text items of (item 3 of VcardData)
    set AppleScript's text item delimiters to " "
    set stateaddress to text items of (stateaddress as text)
    set address2 to ""
    repeat with i from 1 to (count stateaddress)
        if item i of stateaddress ≠ "" then
            set address2 to address2 & item i of stateaddress & ";"
        end if
    end repeat
    set address2 to text 1 thru -2 of address2
    set AppleScript's text item delimiters to tids
    
    set address to address & address2 & "
"
    if not withoutHomepage then
        set homepage to "item1.URL;type=pref:" & item 5 of VcardData & "
item1.X-ABLabel:_$!<HomePage>!$_
X-ABShowAs:COMPANY
END:VCARD"
    else
        set homepage to "X-ABShowAs:COMPANY
END:VCARD"
    end if
    set vcardText to preamble & namefield & phoneNumber & address & homepage
    set vcardText to paragraphs of (text returned of (display dialog "Hit enter to save the vcf to disk with this contents:" default answer vcardText))
    set AppleScript's text item delimiters to linefeed
    set vcardText to vcardText as text
    set AppleScript's text item delimiters to tids
    set thef to choose file name with prompt "create Vcard-file where?" default location path to desktop folder from user domain
    set f to (open for access thef with write permission)
    write vcardText to f
    close access f
    --display alert "Saved Vcard-file to " & thef
end main


It procuces this output, I gamble on that whatever app you are importing into will add UID fields that are valid.
By the way, it won’t work with the data from the sidebar of google maps.

BEGIN:VCARD VERSION:3.0 N:;;;; FN:Unity Telecom ORG:Unity Telecom TEL;type=WORK;type=VOICE;type=pref:480-607-1234 ADR;type=WORK;type=pref:;;6402 Superstition Springs Boulevard;Mesa;AZ;85206 item1.URL;type=pref:unitytelecom.net item1.X-ABLabel:_$!<HomePage>!$_ X-ABShowAs:COMPANY END:VCARD

Attempt #1 With:

Attempt #2 With:

Also, you are correct about the UID fields, this is not necessary for import.

Hello.

I have updated the script.

Please try the attempt #2. The first attempt fails due to a lack of a homepage, I didn’t anticipate that, nor the second.

I’ll deal with the lacking of homepage tomorrow.

Hello.

I have now fixed the script in post #4 to handle cases without a homepage, your attempt #1.

It returns output like this:

BEGIN:VCARD VERSION:3.0 N:;;;; FN:Paetec Communications-Robert Maguire ORG:Paetec Communications-Robert Maguire TEL;type=WORK;type=VOICE;type=pref:602-283-2378 ADR;type=WORK;type=pref:;;3230 East Broadway Road #210;Phoenix;AZ;85040 X-ABShowAs:COMPANY END:VCARD
As you can see there is person info in here, as Robert Maguire is mentioned. I don’t know how you want to resolve this, and this is not a case that the script can handle easily, for cases like this, I recommend that you past the clipboard into TextEdit, edit it, and copy it back, before rerunning the script. I’ll show what is going to be output in the script, before saving it to a file.

Edit
You may now edit the text in the dialogbox slightly if you wish.

Works Great!! Thank you SO MUCH!