(* show_mail_templates.applescript – Version 1.4.1 Rolle dieses Scripts -------------------- Listet alle verfügbaren Mail-Vorlagen aus /mail_vorlagen auf und lässt den User eine davon auswählen. Property standaloneMode: - true (default): Script läuft alleine, User-freundlich - false: Aufgerufen von _belegversand, minimal intrusive Behavior: - 0 Vorlagen (standalone): Dialog "mail_vorlagen ist leer" - 0 Vorlagen (nicht standalone): leerer String - 1 Vorlage (standalone): Dialog mit Vorlage-Name - 1 Vorlage (nicht standalone): direkt zurückgeben - >1 Vorlagen: choose from list Dialog (immer) *) property standaloneMode : true tell application id "DNtp" set sel to selection if sel is {} then error "Bitte zuerst ein Objekt in DEVONthink auswählen." end if set selectedRecord to item 1 of sel end tell on getTopParentGroup(theRecord) tell application id "DNtp" set currentRecord to theRecord repeat try set p to parents of currentRecord if p is {} then return currentRecord set currentRecord to item 1 of p on error return currentRecord end try end repeat end tell end getTopParentGroup on findChildByName(parentRecord, childName) tell application id "DNtp" try set kids to children of parentRecord repeat with i from 1 to count of kids set c to item i of kids if (name of c as rich text) is childName then return c end repeat end try end tell return missing value end findChildByName on selectTemplate() -- Im Standalone-Modus: Erste DB nutzen statt Selection if standaloneMode then tell application id "DNtp" set allDatabases to databases if allDatabases is {} then error "Keine DEVONthink-Datenbank offen." end if set selectedRecord to item 1 of allDatabases end tell else -- Von _belegversand aufgerufen: aktuelle Selection nutzen tell application id "DNtp" set sel to selection if sel is {} then error "Keine Objekte ausgewählt." end if set selectedRecord to item 1 of sel end tell end if set dbRoot to getTopParentGroup(selectedRecord) set configGroup to findChildByName(dbRoot, "_config") if configGroup is missing value then error "Keine _config-Gruppe gefunden." end if set mailTemplatesGroup to findChildByName(configGroup, "mail_vorlagen") if mailTemplatesGroup is missing value then error "Keine mail_vorlagen-Gruppe gefunden." end if tell application id "DNtp" set templateChildren to children of mailTemplatesGroup end tell set templateNames to {} repeat with eachTemplate in templateChildren set templateName to (eachTemplate's name) & "" set end of templateNames to templateName end repeat -- EDGE CASE: 0 Vorlagen if templateNames is {} then if standaloneMode then display dialog "Die DEVONthink-Gruppe /mail_vorlagen ist leer." buttons {"OK"} default button "OK" end if return "" end if -- EDGE CASE: 1 Vorlage if (count of templateNames) is 1 then if standaloneMode then display dialog "Folgende Mail-Vorlage gefunden:" & return & return & (item 1 of templateNames) buttons {"OK"} default button "OK" end if return item 1 of templateNames end if -- NORMAL: >1 Vorlagen set chosenTemplate to choose from list templateNames with title "Mail-Vorlage(n)..." with prompt "Folgende Vorlagen gefunden:" if chosenTemplate is false then return "" else return item 1 of chosenTemplate end if end selectTemplate -- Wenn standalone aufgerufen selectTemplate()