G
gandalf12
Aktives Mitglied
Thread Starter
- Dabei seit
- 23.03.2011
- Beiträge
- 472
- Reaktionspunkte
- 62
Hallo,
ich habe ein Script welches Dokumente verschiedenster Art mit Hilfe von Pages automatisch in ein PDF konvertieren kann.
Das Script war ursprünglich komplett auf französisch und ich habe es nun dort hingehend optimiert, das die Schriftausgabe auf deutsch ist.
Es funktioniert einwandfrei...ABER...
es ist wohl noch einiges an unnützen Code vorhanden oder recht umständlich gecoded.
Ich bin sicher, das die Scripter unter euch den vorhandenen Code effizienter gestalten könnten und das Script um einiges "verschlankt" werden kann.
Also...
jemand hier der den Code optimieren kann ohne die Funktion des Scriptes einzuschränken ?
Danke
ich habe ein Script welches Dokumente verschiedenster Art mit Hilfe von Pages automatisch in ein PDF konvertieren kann.
Das Script war ursprünglich komplett auf französisch und ich habe es nun dort hingehend optimiert, das die Schriftausgabe auf deutsch ist.
Es funktioniert einwandfrei...ABER...
es ist wohl noch einiges an unnützen Code vorhanden oder recht umständlich gecoded.
Ich bin sicher, das die Scripter unter euch den vorhandenen Code effizienter gestalten könnten und das Script um einiges "verschlankt" werden kann.
Also...
jemand hier der den Code optimieren kann ohne die Funktion des Scriptes einzuschränken ?
Danke
Code:
property theApp : "Pages"
property theExt : "pages"
property Stockage : "PDF to OCR"
property nomDuRapport : "report_Doc2Pdf.txt"
property srcTypeId : "com.microsoft.word.doc"
property srcTypeIdx : "org.openxmlformats.wordprocessingml.document"
property storeOnDesktop : false
property keepPagesDoc : false
property withSeconds : true
property msg1 : "" -- globale
property msg3 : "" -- globale
property msg90 : "" -- globale
property msg91 : "" -- globale
property msg92 : "" -- globale
property msg94 : "" -- globale
property msg95 : "" -- globale
property msg96 : "" -- globale
property msg98 : "" -- globale
property msg99 : "" -- globale
property rapport : "" -- globale
property dossierDeStockage : "" -- globale
property newExt : ""
property newType : ""
property types : {{"doc", "SLDocumentTypeMSWord"}, {"pdf", "SLDocumentTypePDF"}, {"txt", "SLDocumentTypePlainText"}, {"rtf", "SLDocumentTypeRichText"}, {"rtfd", "SLDocumentTypeRichTextBundle"}}
property typeNum : 2 (* 1 = WORD, 2 = PDF, 3 = Txt, 4 = rtf, 5 = rtfd *)
on run
tell application "System Events" to set dossier to choose folder
my commun({dossier})
end run
on open (sel)
my commun(sel)
end open
on commun(liste)
local elem, p2d, p2r
set {newExt, newType} to item typeNum of types (*
item 1 = {"doc", "SLDocumentTypeMSWord"},
item 2 = {"pdf", "SLDocumentTypePDF"}
item 3 = {"txt", "SLDocumentTypePlainText"}
item 4 = {"rtf", "SLDocumentTypeRichText"}
item 5 = {"rtfd", "SLDocumentTypeRichTextBundle"}
*)
my prepareMessages()
my afficheLeMessage(msg1)
set rapport to ""
set dossierDeStockage to my creeDossierDeStockage()
tell application theApp to activate
try
repeat with elem in liste
try
my exploreTraite(elem as Unicode text, "")
end try
end repeat
if rapport = "" then set rapport to msg90
-- crée un fichier texte sur le Bureau
set p2d to path to desktop
set p2r to (p2d as Unicode text) & nomDuRapport
tell application "System Events"
if exists (file p2r) then delete (file p2r)
make new file at end of p2d with properties {name:nomDuRapport}
end tell
set rapport to rapport as text
write rapport to (p2r as alias)
on error MsgErr number NroErr
if NroErr is not -128 then
beep 2
tell application (path to frontmost application as string) to ¬
display dialog "" & NroErr & " : " & MsgErr with icon 0 buttons {msg99} giving up after 20
end if -- NroErr is…
return
end try
my nettoie()
my afficheLeMessage(msg3)
end commun
on nettoie()
set dossierDeStockage to ""
set rapport to ""
end nettoie
on afficheLeMessage(m)
beep 1
tell application (path to frontmost application as string) to display dialog m buttons {msg98} default button 1 giving up after 10
end afficheLeMessage
on creeDossierDeStockage()
local dd, dds
if storeOnDesktop is true then
set dd to path to desktop as Unicode text
else
set dd to path to documents folder as Unicode text
end if
if Stockage ends with ":" then
set dds to dd & Stockage
else
set dds to dd & Stockage & ":"
end if
tell application "System Events"
if not (exists item dds) then make new folder at end of folder dd with properties {name:Stockage}
end tell -- to System Events
return dds as Unicode text
end creeDossierDeStockage
on exploreTraite(elem, ptree)
local cl_, typeId_
tell application "System Events" to tell disk item elem
set cl_ to class
if cl_ is folder then
set typeId_ to ""
else
set typeId_ to type identifier
end if
end tell
set cl_ to cl_ as Unicode text
if typeId_ is in {srcTypeId, srcTypeIdx} then
my TraiteUnDocument(elem, typeId_)
else if cl_ is in {"file package", "«class cpkg»"} then
set rapport to rapport & msg91 & elem & return
else if cl_ is in {"folder", "«class cfol»"} then
my ExploreUnDossier(elem, ptree)
else
set rapport to rapport & msg92 & elem & return
end if -- cl_ is …
end exploreTraite
on ExploreUnDossier(dossier, ptree)
local nomElement, cheminElement, C
repeat with nomElement in list folder dossier without invisibles
tell application "System Events" to set C to name of (dossier as alias)
my exploreTraite((dossier & nomElement), ptree & C & ":")
end repeat
end ExploreUnDossier
on TraiteUnDocument(leCheminOriginal_UniText, typId)
local sourceExt, dossierSource, nomSource, nouveauNom, nouveauChemin0, nouveauChemin
if typId is srcTypeId then
set sourceExt to "doc"
else
set sourceExt to "docx"
end if
if leCheminOriginal_UniText ends with ":" then set leCheminOriginal_UniText to (text 1 thru -2 of leCheminOriginal_UniText)
tell application "System Events" to tell disk item leCheminOriginal_UniText
set dossierSource to (path of container) as Unicode text
set nomSource to name as Unicode text
end tell -- System Events
if nomSource ends with sourceExt then set nomSource to (text 1 thru -(2 + (length of sourceExt)) of nomSource)
set nouveauChemin0 to dossierDeStockage & nomSource & "." & theExt
set nouveauChemin to dossierDeStockage & nomSource & "." & newExt
tell application "System Events"
if exists file nouveauChemin0 then set name of file nouveauChemin0 to nomSource & my horoDateur(modification date of file nouveauChemin0) & "." & theExt
if exists file nouveauChemin then set name of file nouveauChemin to nomSource & my horoDateur(modification date of file nouveauChemin) & "." & newExt
end tell
try
tell application theApp
open leCheminOriginal_UniText as alias
(*_*)
save front document in nouveauChemin0
save front document as newType in nouveauChemin
close front document saving no
end tell -- Pages
if keepPagesDoc is false then tell application "System Events" to delete file nouveauChemin0
on error MsgErr number NroErr
set rapport to rapport & msg95 & leCheminOriginal_UniText & return
end try
end TraiteUnDocument
on horoDateur(dt)
local annee, mois, jour, lHeure, lesSecondes, lesMinutes
set annee to year of dt
set mois to month of dt as number
set jour to day of dt
set lHeure to time of dt
set lesSecondes to (lHeure mod 60)
set lHeure to round (lHeure div 60)
set lesMinutes to (lHeure mod 60)
set lHeure to round (lHeure div 60)
if withSeconds is true then
return "_" & annee & text -2 thru -1 of ("00" & mois) & text -2 thru -1 of ("00" & jour) & "-" & text -2 thru -1 of ("00" & lHeure) & text -2 thru -1 of ("00" & lesMinutes) & text -2 thru -1 of ("00" & lesSecondes)
else
return "_" & annee & text -2 thru -1 of ("00" & mois) & text -2 thru -1 of ("00" & jour) & "-" & text -2 thru -1 of ("00" & lHeure) & text -2 thru -1 of ("00" & lesMinutes)
end if
end horoDateur
on parleAnglais()
local z
try
tell application theApp to set z to localized string "Cancel"
on error
set z to "Cancel"
end try
return (z = "Cancel")
end parleAnglais
on prepareMessages()
if my parleAnglais() is false then
set msg1 to "Bitte nicht Klicken wenn das Script läuft" & return & "außer Sie werden natürlich dazu aufgefordert"
set msg3 to "Export durchgeführt"
set msg90 to "Keine Probleme während des Export Vorganges"
set msg91 to "Package"
set msg92 to "Kein .doc Dokument"
set msg94 to "Pages kann es nicht lesen"
set msg95 to "Nicht geöffnet"
set msg96 to "Nicht kopiert"
set msg98 to " OK "
set msg99 to " Ooops "
else
set msg1 to "Don’t click when the script is running." & return & "Except, of course, if it ask for."
set msg3 to "Export done."
set msg90 to "No problem during the export process."
set msg91 to "Package"
set msg92 to "Not a .doc document"
set msg94 to "Pages can't read it"
set msg95 to "Not open"
set msg96 to "Not copied."
set msg98 to " OK "
set msg99 to "Oops"
end if
set msg91 to "### " & msg91 & " ### "
set msg92 to "### " & msg92 & " ### "
--
set msg94 to "### " & msg94 & " ### "
set msg95 to "### " & msg95 & " ### "
set msg96 to "### " & msg96 & " ### "
end prepareMessages