Skript für Einfügen von Kontakten in pages

N

nilsaushd

Mitglied
Thread Starter
Dabei seit
10.10.2016
Beiträge
7
Reaktionspunkte
0
Hallo zusammen
Ich habe dieses schöne Skript entdeckt, mit dem man von seinen Kontakten aus eine pages-Vorlage mit dem entsprechendem Adressfeld öffnen kann.
Praktisch eine Serienbrieffunktion durch die Hintertür.
Dummerweise übernimmt es aber nur den Vor- und Nachnamen und die Adresse.
Für meinen Zweck bräuchte ich noch Firma und Notizen.
Ich kenne mich leider überhaupt nicht mit AppleSkript aus und meine Versuche haben nur Fehlermeldungen erzeugt.
Weiß jemand, wie das Skript verändert werden müsste, damit es auch diese Informationen einfügt?
Vielen Dank, Gruß Nils

Hier das Skript
Code:
-- Properties festlegen

property recipientFirstNamePlaceholder : "RECIPIENTFIRSTNAME"
property recipientLastNamePlaceholder : "RECIPIENTLASTNAME"
property recipientFullAddressPlaceholder : "RECIPIENTFULLADDRESS"

-- Plugin definieren

using terms from application "Contacts"
    on action property
        return "address"
    end action property
  
    on should enable action for thePerson with theEntry
        if theEntry = missing value then
            return false
        else
            return true
        end if
    end should enable action
  
    on action title for thePerson with theEntry
        return "Brief mit Pages schreiben"
    end action title
  
    on perform action for thePerson with theEntry
      
        --Schleife für Kontakte
      
        tell application "Contacts"
          
            -- Auswahl für Auslesen vorbereiten  
            set thisPerson to selection
            set thisPerson to item 1 of thisPerson
          
          
            -- Kontaktdaten in Variablen auslesen
            tell thisPerson
                set recipientFirstName to first name
                set recipientLastName to last name
                set recipientFullAddress to the formatted address of the first address of thisPerson
            end tell
          
        end tell
      
        -- Ende der Schleife für Kontakte
      
      
        -- Schleife für Pages
      
        tell the application "Pages"
          
            activate
          
            -- Liste der Pages-Vorlagen aufrufen und eine wählen
            set userTemplateNames to the name of every template whose id of it begins with "User/"
            if userTemplateNames is {} then error number 10001
          
            set the chosenTemplateName to ¬
                (choose from list userTemplateNames with prompt ¬
                    "Bitte eine Vorlage wählen:" default items (item 1 of userTemplateNames))
            if chosenTemplateName is false then error number -128
            set chosenTemplateName to chosenTemplateName as string
          
          
            -- Neues Dokument aus der Auswahl öffnen  
            set thisDocument to ¬
                make new document with properties ¬
                    {document template:template chosenTemplateName}
          
            tell body text of thisDocument
                set recipientFullAddressPlaceholderCount to ¬
                    the count of (every word where it is recipientFullAddressPlaceholder)
            end tell
          
            tell thisDocument
              
              
                -- Platzhaltertext mit Empfängerdaten ersetzen
                set placeholderWordReplacementStringPairings to {{recipientFirstNamePlaceholder, recipientFirstName}, {recipientLastNamePlaceholder, recipientLastName}, {recipientFullAddressPlaceholder, recipientFullAddress}}
                tell body text
                    with timeout of 600 seconds -- Bis zu 10 minuten gestatten, falls sehr lange Dokumente nach Platzhaltertexten durchsucht werden müssen
                        repeat with i from 1 to the count of placeholderWordReplacementStringPairings
                            copy item i of placeholderWordReplacementStringPairings to ¬
                                {placeholderWord, replacementString}
                            my replaceWordWithStringInBodyText(placeholderWord, replacementString)
                        end repeat
                    end timeout
                end tell
            end tell
        end tell
    end perform action
  
end using terms from

on replaceWordWithStringInBodyText(searchWord, replacementString)
    tell application "Pages"
        activate
        tell the front document
            tell body text
                -- Schleife mit Ersetzungen
                repeat with i from the (count of paragraphs) to 1 by -1
                    tell paragraph i
                        repeat
                            try
                                if exists searchWord then
                                    set (last word where it is searchWord) to replacementString
                                else
                                    exit repeat
                                end if
                            on error errorMessage
                                exit repeat
                            end try
                        end repeat
                    end tell
                end repeat
            end tell
        end tell
        return true
    end tell
end replaceWordWithStringInBodyText
 
Zuletzt bearbeitet:
evt. auch mal die Quelle angeben damit jeder verseht, was dieses Script macht, wie es funktioniert und nicht raten muß ;)

RECIPIENTCOMPANY evt.
 
Stimmt, danke für die Ergänzung.
 
Kannst Du das bitte nochmal in Code-Tags einstellen/abändern. Sonst extrem unübersichtlich! :)

code.png
 
Also wenn ich davon ausgehe, dass Du in Deinem Template RECIPIENTORGANIZATION und
RECIPIENTNOTE benutzt, kannst Du dem existierenden Skript z.B. Folgendes hinzufügen.

Ganz zu Beginn hinzufügen

Code:
property recipientOrganizationPlaceholder : "RECIPIENTORGANIZATION"
property recipientNotePlaceholder : "RECIPIENTNOTE"

Im "tell thisPerson"-Block:

Code:
set recipientOrganization to organization
set recipientNote to note

Und schließlich an der Stelle an der placeholderWordReplacementStringPairings gesetzt wird ("set placeholderWordReplacementStringPairings to ...") , die existierende Zeile ersetzen durch:

Code:
set placeholderWordReplacementStringPairings to {{recipientFirstNamePlaceholder, recipientFirstName}, {recipientLastNamePlaceholder, recipientLastName}, {recipientFullAddressPlaceholder, recipientFullAddress}, {recipientOrganizationPlaceholder, recipientOrganization}, {recipientNotePlaceholder, recipientNote}}
 
Vielen Dank für die Hilfe,
pages ersetzt meine Platzhalter RECIPIENTORGANIZATION
und RECIPIENTNOTE aber leider nicht.
Wie sehe ich denn, ob die entsprechenden Felder in den Kontakten tatsächlich so heißen?
 
Vielen Dank für die Hilfe,
pages ersetzt meine Platzhalter RECIPIENTORGANIZATION
und RECIPIENTNOTE aber leider nicht.
Wie sehe ich denn, ob die entsprechenden Felder in den Kontakten tatsächlich so heißen?
Die Platzhalter sind erstmal unabhängig von den Kontaktfeldern und kannst Du Dir als Autor des Skripts (und der Vorlage) frei definieren. In den Kontakten gibt es die folgenden Felder. Hier habe ich beschrieben wie Du das auch nachschauen kannst (in dem Fall für OCRKit statt der Kontakte).

Code:
person n, pl people [inh. entry] : A person in the address book database.

elements
contains urls, addresses, phones, groups, custom dates, instant messages, social profiles, related names, emails; contained by application, groups.

properties
nickname (text or missing value) : The Nickname of this person.
organization (text or missing value) : Organization that employs this person.
maiden name (text or missing value) : The Maiden name of this person.
suffix (text or missing value) : The Suffix of this person.
vcard (text or missing value, r/o) : Person information in vCard format, this always returns a card in version 3.0 format.
home page (text or missing value) : The home page of this person.
birth date (date or missing value) : The birth date of this person.
phonetic last name (text or missing value) : The phonetic version of the Last name of this person.
title (text or missing value) : The title of this person.
phonetic middle name (text or missing value) : The Phonetic version of the Middle name of this person.
department (text or missing value) : Department that this person works for.
image (TIFF picture or missing value) : Image for person.
name (text, r/o) : First/Last name of the person, uses the name display order preference setting in Address Book.
note (text or missing value) : Notes for this person.
company (boolean) : Is the current record a company or a person.
middle name (text or missing value) : The Middle name of this person.
phonetic first name (text or missing value) : The phonetic version of the First name of this person.
job title (text or missing value) : The job title of this person.
last name (text or missing value) : The Last name of this person.
first name (text or missing value) : The First name of this person.

Zum testen z.B.:

Code:
tell application "Contacts" to get organization of item 1 of (get selection)
 
Ah, ok. Demnach müsste es doch so funktionieren, wenn ich in der Vorlage einen Platzhaltertext RECIPIENTORGANIZATION und RECIPIENTNOTE einfüge.
Hmm..
 
Schade eigentlich. Ich habe das Script seit über einem Jahr genutzt und meine Pages Templates gefüllt. Leider funktioniert es seit dem letzten Betriebssystem Update auf 10.14 nicht mehr. Sowohl bei meinem iMac als auch bei meinem MacBook Pro wird die Auswahl "Brief mit Pages schreiben" nicht mehr angezeigt. Das Script ist noch in dem gleichen Verzeichnis.
Gruß aus Köln, Bernhard
 
Zurück
Oben Unten