Public Access
Aktualisiert auf Version 1.5 Doppelte Pfadwurzel eliminiert. Funktionalität unverändert, verweist auf aktualisierte _belegversand*-Scripts. Alle Pfade von Version 1.4 unverändert übernommen!
267 lines
8.7 KiB
AppleScript
267 lines
8.7 KiB
AppleScript
(*
|
||
START.scpt - Version 1.5
|
||
Rolle dieses Scripts
|
||
--------------------
|
||
Dieses Script ist der sichtbare Einstiegspunkt für Anwender:innen.
|
||
Es selbst verschickt keine Belege und führt auch keine Einrichtung durch,
|
||
sondern übernimmt nur zwei Aufgaben:
|
||
1. Prüfen, ob relativ zur aktuellen DEVONthink-Auswahl eine Konfigurationsdatei
|
||
für den Belegversand existiert.
|
||
2. Falls in dieser Konfiguration SHOW_README=true gesetzt ist,
|
||
einen kurzen Willkommen-/Hinweisdialog anzeigen.
|
||
3. Danach das Setup-Script _belegversand_setup.scpt starten.
|
||
Das Setup-Script entscheidet anschließend selbst:
|
||
- ob bereits eine gültige Einrichtung vorliegt und direkt das Hauptscript
|
||
gestartet werden kann,
|
||
- oder ob zunächst die Konfiguration eingerichtet werden muss.
|
||
Wichtig:
|
||
--------
|
||
START.scpt ist bewusst klein gehalten.
|
||
Die eigentliche Fachlogik liegt in:
|
||
- _belegversand_setup.scpt
|
||
- _belegversand.scpt
|
||
Version 1.5:
|
||
-----------
|
||
- Fix: Pfade zu Helpers/scripts werden zentral in helperScriptPath()
|
||
gebaut. Vorher überschrieb showWelcomeDialogIfNeeded() die Property
|
||
basePath mit einem absoluten Pfad – danach entstand beim Start des
|
||
Setup-Scripts ein doppelter Home-Pfad (/Users/x//Users/x/Library/...).
|
||
Aufgefallen ist das erst, seit das Setup SHOW_README=true schreibt.
|
||
- basePath jetzt ohne führenden Schrägstrich, wie in den anderen Scripts
|
||
- doppelten Kommentarblock entfernt
|
||
*)
|
||
|
||
-- Name des Setup-Scripts, das im Unterordner "Helpers/scripts" liegt.
|
||
-- Dieses Script wird von START.scpt immer am Ende aufgerufen.
|
||
property setupScriptName : "_belegversand_setup.scpt"
|
||
-- Name des Scripts mit dem Text für den Willkommensdialog.
|
||
property aboutScriptName : "_text_about.scpt"
|
||
-- Name der DEVONthink-Gruppe, in der Konfigurationsdateien liegen.
|
||
property configGroupName : "_config"
|
||
-- Name der eigentlichen Konfigurationsdatei in DEVONthink.
|
||
property configRecordName : "belegversand.conf"
|
||
property appVersion : "1.5"
|
||
-- Relativ zum Home-Ordner. Wird NIE zur Laufzeit verändert.
|
||
property basePath : "Library/Application Scripts/com.devon-technologies.think/Menu/Belegversand"
|
||
property windowTitle : "Belegversand, Vers. " & appVersion
|
||
|
||
(*
|
||
helperScriptPath(scriptFileName)
|
||
--------------------------------
|
||
Liefert den absoluten Pfad eines Scripts in Helpers/scripts, z. B.
|
||
/Users/<name>/Library/Application Scripts/.../Belegversand/Helpers/scripts/_belegversand_setup.scpt
|
||
Einzige Stelle, an der dieser Pfad zusammengebaut wird.
|
||
*)
|
||
on helperScriptPath(scriptFileName)
|
||
-- POSIX path of (path to home folder) endet bereits mit "/"
|
||
return (POSIX path of (path to home folder)) & basePath & "/Helpers/scripts/" & scriptFileName
|
||
end helperScriptPath
|
||
|
||
(*
|
||
runSiblingScript(scriptFileName)
|
||
--------------------------------
|
||
Lädt und startet ein weiteres AppleScript aus dem Unterordner "Helpers/scripts".
|
||
Fehlerbehandlung:
|
||
- wenn die Datei nicht gefunden wird -> Hinweisdialog
|
||
- wenn das Laden oder Starten fehlschlägt -> Hinweisdialog mit Fehlermeldung
|
||
*)
|
||
on runSiblingScript(scriptFileName)
|
||
set targetPath to my helperScriptPath(scriptFileName)
|
||
|
||
try
|
||
set targetAlias to POSIX file targetPath as alias
|
||
on error
|
||
display dialog "Hinweis: Scriptdatei nicht gefunden:" & return & targetPath buttons {"OK"} default button "OK"
|
||
return
|
||
end try
|
||
|
||
try
|
||
set loadedScript to load script targetAlias
|
||
run loadedScript
|
||
on error errMsg number errNum
|
||
display dialog "Hinweis: Konnte Script nicht starten:" & return & scriptFileName & return & return & errMsg buttons {"OK"} default button "OK"
|
||
end try
|
||
end runSiblingScript
|
||
|
||
(*
|
||
recordText(theRecord)
|
||
---------------------
|
||
Liest den Textinhalt eines DEVONthink-Records.
|
||
- zunächst plain text versuchen
|
||
- wenn das fehlschlägt, auf content ausweichen
|
||
- wenn beides fehlschlägt, leeren String zurückgeben
|
||
*)
|
||
on recordText(theRecord)
|
||
tell application id "DNtp"
|
||
try
|
||
set t to plain text of theRecord
|
||
if t is missing value then set t to ""
|
||
return t as rich text
|
||
on error
|
||
try
|
||
set t to content of theRecord
|
||
if t is missing value then set t to ""
|
||
return t as rich text
|
||
on error
|
||
return ""
|
||
end try
|
||
end try
|
||
end tell
|
||
end recordText
|
||
|
||
(*
|
||
findChildByName(parentRecord, childName)
|
||
----------------------------------------
|
||
Sucht innerhalb einer DEVONthink-Gruppe ein direktes Kind mit bestimmtem Namen.
|
||
Rückgabe: den gefundenen Record oder missing value.
|
||
*)
|
||
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
|
||
|
||
(*
|
||
getTopParentGroup(theRecord)
|
||
----------------------------
|
||
Ermittelt die oberste Gruppe einer DEVONthink-Hierarchie ausgehend von einem Record.
|
||
START.scpt kennt die gewünschte Datenbank nicht direkt, sondern geht von
|
||
der aktuellen Auswahl über die Parent-Kette bis zur Wurzel hoch.
|
||
*)
|
||
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
|
||
|
||
(*
|
||
configValue(configText, keyName)
|
||
--------------------------------
|
||
Liest aus einem key=value-Konfigurationstext den Wert zu einem Schlüssel.
|
||
Rückgabe: Textwert oder missing value.
|
||
*)
|
||
on configValue(configText, keyName)
|
||
set keyPrefix to keyName & "="
|
||
repeat with oneLine in paragraphs of configText
|
||
set lineText to oneLine as text
|
||
if lineText starts with keyPrefix then
|
||
if (length of lineText) is equal to (length of keyPrefix) then
|
||
return ""
|
||
else
|
||
set val to text ((length of keyPrefix) + 1) thru -1 of lineText
|
||
return val as text
|
||
end if
|
||
end if
|
||
end repeat
|
||
return missing value
|
||
end configValue
|
||
|
||
(*
|
||
findConfigInCurrentDB()
|
||
-----------------------
|
||
Sucht belegversand.conf relativ zur aktuellen DEVONthink-Auswahl
|
||
(DB-Wurzel → _config → belegversand.conf).
|
||
Bewusst nicht global in allen Datenbanken.
|
||
*)
|
||
on findConfigInCurrentDB()
|
||
tell application id "DNtp"
|
||
set sel to selection
|
||
if sel is {} then return missing value
|
||
set selectedRecord to item 1 of sel
|
||
end tell
|
||
|
||
set dbRootRecord to my getTopParentGroup(selectedRecord)
|
||
set configGroupRecord to my findChildByName(dbRootRecord, configGroupName)
|
||
if configGroupRecord is missing value then return missing value
|
||
|
||
set configRecord to my findChildByName(configGroupRecord, configRecordName)
|
||
return configRecord
|
||
end findConfigInCurrentDB
|
||
|
||
(*
|
||
showWelcomeDialogIfNeeded()
|
||
---------------------------
|
||
Zeigt den Willkommensdialog beim Start an, falls konfiguriert (SHOW_README=true).
|
||
1. Config-Datei relativ zur aktuellen Auswahl suchen
|
||
2. Wert SHOW_README auslesen
|
||
3. falls SHOW_README=true -> Dialog mit "Nicht mehr anzeigen"-Option anzeigen
|
||
4. falls gewählt: SHOW_README=false in die Config zurückschreiben
|
||
Der Dialog-Text wird aus Helpers/scripts/_text_about.scpt geladen.
|
||
*)
|
||
on showWelcomeDialogIfNeeded()
|
||
set configRecord to my findConfigInCurrentDB()
|
||
if configRecord is missing value then return
|
||
|
||
set cfgText to my recordText(configRecord)
|
||
set showReadmeVal to my configValue(cfgText, "SHOW_README")
|
||
if showReadmeVal is missing value then return
|
||
|
||
set showReadmeLower to do shell script "printf %s " & quoted form of showReadmeVal & " | tr '[:upper:]' '[:lower:]'"
|
||
if showReadmeLower is not "true" then return
|
||
|
||
set textScriptPath to my helperScriptPath(aboutScriptName)
|
||
|
||
try
|
||
set loadedScript to load script (POSIX file textScriptPath as alias)
|
||
run loadedScript
|
||
tell loadedScript
|
||
set msgText to buildAboutText()
|
||
end tell
|
||
on error errMsg
|
||
display dialog "Fehler beim Laden des About-Textes:" & return & textScriptPath & return & return & errMsg buttons {"OK"}
|
||
return
|
||
end try
|
||
|
||
set choice to button returned of (display dialog msgText buttons {"Diesen Dialog nicht mehr anzeigen", "Weiter"} default button "Weiter" with title windowTitle)
|
||
|
||
if choice is "Diesen Dialog nicht mehr anzeigen" then
|
||
set newCfgText to ""
|
||
repeat with oneLine in paragraphs of cfgText
|
||
set lineText to oneLine as text
|
||
if lineText starts with "SHOW_README=" then
|
||
set newCfgText to newCfgText & "SHOW_README=false" & return
|
||
else
|
||
set newCfgText to newCfgText & lineText & return
|
||
end if
|
||
end repeat
|
||
|
||
tell application id "DNtp"
|
||
try
|
||
set plain text of configRecord to newCfgText
|
||
on error
|
||
set content of configRecord to newCfgText
|
||
end try
|
||
end tell
|
||
end if
|
||
end showWelcomeDialogIfNeeded
|
||
|
||
(*
|
||
run
|
||
---
|
||
Haupteinstiegspunkt des START-Scripts.
|
||
1. optionalen Startdialog anzeigen
|
||
2. Setup-Script starten (entscheidet: Versand oder Einrichtung)
|
||
*)
|
||
on run
|
||
with timeout of 3600 seconds
|
||
my showWelcomeDialogIfNeeded()
|
||
my runSiblingScript(setupScriptName)
|
||
end timeout
|
||
end run |