Wenn man im Apple Adressbuch Kontakte als vCard exportieren möchte, erstellt das Programm bei mehreren Kontakten anscheinend immer nur eine Datei, in der alle Informationen stehen… FALSCH, mit gedrückter alt-Taste erstellt das Programm auf Wunsch auch separate VCF-Dateien für jeden Kontakt (siehe Kommentare)…. somit kann man sich das Weiterlesen eigentlich sparen…Nun, dieses Skript (quick&dirty) exportiert die Kontakte in einzelne .vcf Dateien

--hubionmac.com 06.02.2011
--quick&dirty script for exporting contacts as single vCards
--exisiting vCards will not be overwritten by the script, instead new files will 
--get an index like "John Smith 1.vcf"

set doThese to get_item_2_process(true)
tell application "Contacts"
    set destination_path to choose folder with prompt "Destination folder for export:"
    repeat with doThis in doThese
        if first name of doThis is missing value then
            set firstname to ""
        else
            set firstname to first name of doThis
        end if
        if last name of doThis is missing value then
            set lastname to ""
        else
            set lastname to last name of doThis
        end if
        if (lastname & firstname) as text = "" then
            set namestring to "nobody.vcf"
        else
            set namestring to (first name of doThis & " " & last name of doThis & ".vcf") as text
        end if
        set namestring to my checkname_with_pdf_suffix(namestring, destination_path, false)
        my writeToFile((destination_path as text) & namestring as text, (get vcard of doThis), false)
    end repeat
    my display_message(((count of doThese) & " contact(s) exported") as text, 3)
end tell
on writeToFile(MacFilePathTxt, txt, add2eof)
    --lastedit 18.01.2011
    if add2eof is false then
        try
            do shell script "rm " & quoted form of POSIX path of (MacFilePathTxt as alias)
        end try
    end if
    set RefNum to (open for access file MacFilePathTxt with write permission)
    try
        if add2eof is false then
            write txt to RefNum
        else
            write txt to RefNum starting at ((get eof RefNum) + 1)
        end if
        close access RefNum
        return true
    on error
        close access RefNum
        return false
    end try
end writeToFile

to get_item_2_process(askwhat)
    try
        tell application "Contacts"
            activate
            tell application "System Events"
                keystroke "0" using command down --show address-window not matter what…
            end tell
            if askwhat is true then
                set theaction to button returned of (display dialog "Process all or just selection?" buttons {"All", "Selection", "cancel"} default button {"Selection"})
                if theaction = "Selection" then
                    set a to every person whose selected is true
                else
                    set a to people
                end if
            else
                set a to people
            end if
        end tell
    on error msg
        error "error on get_item_2_process" & return & msg
    end try
end get_item_2_process

to checkname_with_pdf_suffix(n, D, looped)
    --check if filename exists in D
    --so if "A File.pdf" exists it names it "A File 1.pdf","A File 2.pdf",…
    tell application "Finder"
        set thefiles to name of every item of (D as alias)
    end tell
    if thefiles contains n then
        if looped = false then
            set n to ((characters 1 through -5 of n) & " 1" & (characters -4 through -1 of n)) as text
            my checkname_with_pdf_suffix(n, D, true)
        else
            set tmp to (last word of ((characters 1 through -5 of n) as text) as integer)
            set tmpcount to (count of characters of (tmp as text)) + 5
            set tmp to tmp + 1
            set n to ((characters 1 through (-1 * tmpcount) of n) & tmp & (characters -4 through -1 of n)) as text
            my checkname_with_pdf_suffix(n, D, true)
        end if
    else
        return n
    end if

end checkname_with_pdf_suffix
on display_message(msgTXT, msgTimeout)
   tell application "System Events"
        set isRunning to ¬
            (count of (every process whose name is "GrowlHelperApp")) > 0
    end tell
    if isRunning = true then
        tell application "GrowlHelperApp"
            — Make a list of all the notification types 

            — that this script will ever send:

            set the allNotificationsList to ¬

                {"Status"}

            — Make a list of the notifications 

            — that will be enabled by default.      

            — Those not enabled by default can be enabled later 

            — in the ‘Applications’ tab of the growl prefpane.

            set the enabledNotificationsList to ¬

                {"Status"}

            — Register our script with growl.

            — You can optionally (as here) set a default icon 

            — for this script’s notifications.

            register as application ¬

                "Address Book" all notifications allNotificationsList ¬

                default notifications enabledNotificationsList ¬

                icon of application "Address Book"

            --Send a Notification…

            notify with name ¬

                "Status" title ¬

                "Status" description ¬

                msgTXT application name ¬

                "Address Book"

            return true

        end tell

    else

        activate

        display dialog msgTXT giving up after msgTimeout

    end if

end display_message

Add a comment

Next Post Previous Post