[AppleScript] Word in PDF umwandeln

Jan K.

Stechapfel
Registriert
17.05.15
Beiträge
158
Hallo Leute,

ich habe folgendes Szenario: Ich erstelle einen Haufen einzelne Worddateien und will diese am Ende in PDFs umwandeln. Ich habe mir angewöhnt, dies mit einem Script zu machen, welches ich mal im Netz gefunden habe. Ich jage also alle Word Dateien durch den Automator, welcher mit dann am Ende PDFs speichert.
Dieses Script funktioniert nun aber komischerweise nicht mehr und ich weiß nicht woran es liegt. Hat einer von euch eine Ahnung?

on run {input, parameters}
try
repeat with thisFile in input
set thisPOSIXPath to POSIX path of thisFile as string
if thisPOSIXPath contains ".docx" or thisPOSIXPath contains ".doc" then
tell application "Microsoft Word"
set thisDoc to open file name (thisFile as string)
set thisName to my removeExtension(name of thisDoc)
set thisPath to path of thisDoc
set thisPath to POSIX path of thisPath
save as thisDoc file name thisPath & "/" & thisName & ".pdf" file format format PDF
close thisDoc saving no
end tell
end if
end repeat
on error error_message
display alert "WordToPDF" message error_message as warning
end try
return input
end run

on removeExtension(thisItem)
if thisItem does not contain "." then
return thisItem
end if
set text item delimiters to "."
set thisItem to (text items 1 thru -2 of thisItem) as string
set text item delimiters to ""
return thisItem
end removeExtension

Es kommt immer die Fehlermelden: Word würde das Kommando save as nicht verstehen. Weiß jemand Rat?

Liebe Grüße Jan
 

Keef

Gestreifter Böhmischer Borsdorfer
Registriert
17.07.09
Beiträge
3.420
Und aus welchem Grund mit einem Script?

Word - oder auch entsprechende Open Source - erledigt die Konvertierung doch ebenfalls.
 

sedna

Galloway Pepping
Registriert
22.10.08
Beiträge
1.358
Hallo,

mein Rat: erstelle den Arbeitsablauf neu!

Gruß
 
  • Like
Reaktionen: dg2rbf

BalthasarBux

Galloway Pepping
Registriert
25.12.19
Beiträge
1.361
Weil ich die Antwort von sedna doof fand, hab ich mal kurz im Netz geguckt und bin dabei über dieses Script gestolpert. Das nutzt Pages, um PDFs zu kreiren. Ich habe nur noch hinzugefügt, dass sie nicht nur DOC, sondern auch DOCX akzeptiert. Habe es jetzt nur mit einem Dokument getestet und das hat geklappt. Vielleicht kannst du ja was damit anfangen:

on run
set theseItems to (choose file of type {"com.microsoft.word.doc", "org.openxmlformats.wordprocessingml.document"} with prompt "Choose the Word documents to export to PDF:" with multiple selections allowed)
open theseItems
end run

on open theseItems
set the filesToProcess to {}
repeat with i from 1 to the count of theseItems
set thisItem to item i of theseItems
set the end of the filesToProcess to thisItem
end repeat
my exportToPDF(filesToProcess)
end open

on exportToPDF(theseFiles)
repeat with i from 1 to the count of theseFiles
set thisFile to item i of theseFiles
set thisFilePOSIXPath to the POSIX path of thisFile
copy my deriveNewFilename(thisFilePOSIXPath, "pdf", "-", "") to {targetName, targetPOSIXpath}
set targetFileReference to targetPOSIXpath as POSIX file
tell application "Pages"
try
activate
with timeout of 1200 seconds
open thisFile
delay 1
with timeout of 1200 seconds
export front document to targetFileReference as PDF
end timeout
close front document saving no
end timeout
on error errorMessage number errorNumber
if errorNumber is not -128 then
display alert errorNumber message errorMessage
end if
error number -128
end try
end tell
end repeat
end exportToPDF

on deriveNewFilename(sourceItemPOSIXPath, newNameExtension, incrementSeparator, targetFolderPOSIXPath)
copy my itemInfoFor(sourceItemPOSIXPath) to {parentDirectoryPath, sourceItemName, sourceItemBaseName, sourceItemNameExtension}
if targetFolderPOSIXPath is "" then
set targetFolderPOSIXPath to parentDirectoryPath
else if targetFolderPOSIXPath contains "~" then
set targetFolderPOSIXPath to (do shell script "echo " & targetFolderPOSIXPath)
end if
if targetFolderPOSIXPath does not end with "/" then set targetFolderPOSIXPath to targetFolderPOSIXPath & "/"

if the sourceItemNameExtension is missing value then
set the sourceItemNameExtension to ""
if newNameExtension is "" then
set extensionSeparator to ""
else
set extensionSeparator to "."
end if
else
set extensionSeparator to "."
end if

if the newNameExtension is "" then
set targetName to sourceItemName
set targetExtension to sourceItemNameExtension
else
set targetExtension to newNameExtension
set targetName to (the sourceItemBaseName & extensionSeparator & targetExtension) as Unicode text
end if

set targetItemPOSIXPath to targetFolderPOSIXPath & targetName
set the fileExistenceStatus to (do shell script "[ -a " & (quoted form of targetItemPOSIXPath) & " ] && echo 'true' || echo 'false'") as boolean
if fileExistenceStatus is true then
set the nameIncrement to 1
repeat
set the newName to (the sourceItemBaseName & incrementSeparator & (nameIncrement as Unicode text) & extensionSeparator & targetExtension) as Unicode text
set targetItemPOSIXPath to targetFolderPOSIXPath & newName
set the fileExistenceStatus to (do shell script "[ -a " & (quoted form of targetItemPOSIXPath) & " ] && echo 'true' || echo 'false'") as boolean
if fileExistenceStatus is true then
set the nameIncrement to the nameIncrement + 1
else
set the targetPOSIXpath to (targetFolderPOSIXPath & newName)
return {newName, targetPOSIXpath}
end if
end repeat
else
set the targetPOSIXpath to (targetFolderPOSIXPath & targetName)
return {targetName, targetPOSIXpath}
end if
end deriveNewFilename

on itemInfoFor(sourceItemPOSIXPath)
set the parentDirectoryPath to (do shell script "dirname " & (the quoted form of sourceItemPOSIXPath))
if parentDirectoryPath does not end with "/" then set parentDirectoryPath to parentDirectoryPath & "/"
set the itemFileName to (do shell script "basename " & (quoted form of sourceItemPOSIXPath))
set the itemNameWithoutExtension to (do shell script "file=" & (quoted form of itemFileName) & ";echo ${file%.*}")
if itemFileName contains "." then
set the itemFileExtension to do shell script "file=" & (quoted form of itemFileName) & ";echo ${file##*.}"
else
set the itemFileExtension to missing value
end if
return {parentDirectoryPath, itemFileName, itemNameWithoutExtension, itemFileExtension}
end itemInfoFor

Quelle:
-- Based on droplet from iworkautomation.com
-- Editted by Vicente Ochoa Jr.
 

sedna

Galloway Pepping
Registriert
22.10.08
Beiträge
1.358
Hallo,

meine Antwort mag dir doof vorkommen, ist aber ernst gemeint!
Das in Beitrag #1 gepostete Skript ist ok.


Die gegebenen Infos hingegen sind dürftig: seit wann ist „nun“? Wann hat es das letzte mal funktioniert? Gestern noch oder letztes Jahr?
Um welches macOS und welche Version von Microsoft Word handelt es sich?
Sind (falls ein neueres macOS) die nötigen Berechtigungen gesetzt bzw. bestätigt?

Gruß
 

Jan K.

Stechapfel
Registriert
17.05.15
Beiträge
158
Dein Rat ist aber nunmal nicht sonderlich hilfreich. Der Arbeitsablauf ist schon so angepasst, dass er gut ist. Denn ich brauche mehrere Dokumente und diese müssen auch bearbeitbare bleiben.

Ich finde die Frage nach einer Möglichkeit mehrere Word Dokumente in PDFs umzuwandeln auch gar nicht so abwegig. Das ist ein Vorgang der häufig von Leuten genutzt wird.

Das Script funktionierte vor einigen Wochen noch auch mit Docx Dateien. Ich arbeite immer mit der aktuellen Word Version auf dem Mac und dem aktuellen MacOS. Nun bekomme ich aber eben immer diese Fehlermeldung und kann mir nicht erklären woran das liegen könnte, da ich nichts verändert habe.
Weil ich die Antwort von sedna doof fand, hab ich mal kurz im Netz geguckt und bin dabei über dieses Script gestolpert. Das nutzt Pages, um PDFs zu kreiren. Ich habe nur noch hinzugefügt, dass sie nicht nur DOC, sondern auch DOCX akzeptiert. Habe es jetzt nur mit einem Dokument getestet und das hat geklappt. Vielleicht kannst du ja was damit anfangen:

on run
set theseItems to (choose file of type {"com.microsoft.word.doc", "org.openxmlformats.wordprocessingml.document"} with prompt "Choose the Word documents to export to PDF:" with multiple selections allowed)
open theseItems
end run

on open theseItems
set the filesToProcess to {}
repeat with i from 1 to the count of theseItems
set thisItem to item i of theseItems
set the end of the filesToProcess to thisItem
end repeat
my exportToPDF(filesToProcess)
end open

on exportToPDF(theseFiles)
repeat with i from 1 to the count of theseFiles
set thisFile to item i of theseFiles
set thisFilePOSIXPath to the POSIX path of thisFile
copy my deriveNewFilename(thisFilePOSIXPath, "pdf", "-", "") to {targetName, targetPOSIXpath}
set targetFileReference to targetPOSIXpath as POSIX file
tell application "Pages"
try
activate
with timeout of 1200 seconds
open thisFile
delay 1
with timeout of 1200 seconds
export front document to targetFileReference as PDF
end timeout
close front document saving no
end timeout
on error errorMessage number errorNumber
if errorNumber is not -128 then
display alert errorNumber message errorMessage
end if
error number -128
end try
end tell
end repeat
end exportToPDF

on deriveNewFilename(sourceItemPOSIXPath, newNameExtension, incrementSeparator, targetFolderPOSIXPath)
copy my itemInfoFor(sourceItemPOSIXPath) to {parentDirectoryPath, sourceItemName, sourceItemBaseName, sourceItemNameExtension}
if targetFolderPOSIXPath is "" then
set targetFolderPOSIXPath to parentDirectoryPath
else if targetFolderPOSIXPath contains "~" then
set targetFolderPOSIXPath to (do shell script "echo " & targetFolderPOSIXPath)
end if
if targetFolderPOSIXPath does not end with "/" then set targetFolderPOSIXPath to targetFolderPOSIXPath & "/"

if the sourceItemNameExtension is missing value then
set the sourceItemNameExtension to ""
if newNameExtension is "" then
set extensionSeparator to ""
else
set extensionSeparator to "."
end if
else
set extensionSeparator to "."
end if

if the newNameExtension is "" then
set targetName to sourceItemName
set targetExtension to sourceItemNameExtension
else
set targetExtension to newNameExtension
set targetName to (the sourceItemBaseName & extensionSeparator & targetExtension) as Unicode text
end if

set targetItemPOSIXPath to targetFolderPOSIXPath & targetName
set the fileExistenceStatus to (do shell script "[ -a " & (quoted form of targetItemPOSIXPath) & " ] && echo 'true' || echo 'false'") as boolean
if fileExistenceStatus is true then
set the nameIncrement to 1
repeat
set the newName to (the sourceItemBaseName & incrementSeparator & (nameIncrement as Unicode text) & extensionSeparator & targetExtension) as Unicode text
set targetItemPOSIXPath to targetFolderPOSIXPath & newName
set the fileExistenceStatus to (do shell script "[ -a " & (quoted form of targetItemPOSIXPath) & " ] && echo 'true' || echo 'false'") as boolean
if fileExistenceStatus is true then
set the nameIncrement to the nameIncrement + 1
else
set the targetPOSIXpath to (targetFolderPOSIXPath & newName)
return {newName, targetPOSIXpath}
end if
end repeat
else
set the targetPOSIXpath to (targetFolderPOSIXPath & targetName)
return {targetName, targetPOSIXpath}
end if
end deriveNewFilename

on itemInfoFor(sourceItemPOSIXPath)
set the parentDirectoryPath to (do shell script "dirname " & (the quoted form of sourceItemPOSIXPath))
if parentDirectoryPath does not end with "/" then set parentDirectoryPath to parentDirectoryPath & "/"
set the itemFileName to (do shell script "basename " & (quoted form of sourceItemPOSIXPath))
set the itemNameWithoutExtension to (do shell script "file=" & (quoted form of itemFileName) & ";echo ${file%.*}")
if itemFileName contains "." then
set the itemFileExtension to do shell script "file=" & (quoted form of itemFileName) & ";echo ${file##*.}"
else
set the itemFileExtension to missing value
end if
return {parentDirectoryPath, itemFileName, itemNameWithoutExtension, itemFileExtension}
end itemInfoFor

Quelle:
-- Based on droplet from iworkautomation.com
-- Editted by Vicente Ochoa Jr.

Danke für die Hilfe, allerdings zerstört mir Pages einige Formatierungen in dem Dokument und daher kann ich das so nicht nutzen. Ganz auf Pages kann ich aber nicht umsteigen.
 

sedna

Galloway Pepping
Registriert
22.10.08
Beiträge
1.358
Hallo
Dein Rat ist aber nunmal nicht sonderlich hilfreich
Du hast ihn doch gar nicht befolgt!?
Ich habe übrigens nicht von Arbeitsablauf ändern oder anpassen geschrieben...


Da du jetzt allerdings die Info gegeben hast, dass du das neueste macOS und die neueste Version von Word benutzt, ist es sogar eher wahrscheinlich, dass der Fehler in den Berechtigungen liegt (nach denen ich übrigens gefragt hatte!)

Das Stichwort ist Sandbox

Hier eine einfache, aber tricky Lösung:


Gruß
 
Zuletzt bearbeitet:

Jan K.

Stechapfel
Registriert
17.05.15
Beiträge
158
Hallo

Du hast ihn doch gar nicht befolgt!?
Ich habe übrigens nicht von Arbeitsablauf ändern oder anpassen geschrieben...


Da du jetzt allerdings die Info gegeben hast, dass du das neueste macOS und die neueste Version von Word benutzt, ist es sogar eher wahrscheinlich, dass der Fehler in den Berechtigungen liegt (nach denen ich übrigens gefragt hatte!)

Das Stichwort ist Sandbox

Hier eine einfache, aber tricky Lösung:


Gruß
Dann habe ich nicht verstanden was du mit Arbeitsablauf meinst.

Allerdings hat mir dein Unverständnis doch weitergeholfen. Ich habe eine Sache geändert und zwar liegen die Dateien in OneDrive und anscheinend ist das der Fehler. Er öffnet zwar die Word Dokumente von dort, kann sie aber dann nicht als PDF abspeichern. Lege ich die Dateien beispielsweise auf den Schreibtisch macht er es ohne Probleme.
So gesehen hast du mir also doch geholfen. Allerdings weiß ich nicht wieso er rumzickt wenn es in OneDrive liegt.
 

sedna

Galloway Pepping
Registriert
22.10.08
Beiträge
1.358
Hallo,

Allerdings hat mir dein Unverständnis doch weitergeholfen.
Unverständnis?

Man kann nur Aussagen aufgrund der gegebenen Infos treffen.
Diese gibst du scheibchenweise und erst in deiner letzten Antwort die entscheidende, dass die Dateien in OneDrive liegen...

Ich behaupte, dass deine Aussage in #1 , dass das Skript "nun nicht mehr funktioniert", so nicht stimmen kann, sondern dass das Skript noch nie mit Dateien in OneDrive funktioniert hat. Es ist nämlich zwar ok, funktioniert allerdings nur, solange es sich um Pfade in deinem Dateisystem handelt.
Eine kleine Änderung im Skript , und es würde auch für OneDrive Dateien funktionieren.

Da du meine Ratschläge eh nicht befolgst 😜 , gebe ich am besten keine weiteren.

Gruß
 
Zuletzt bearbeitet:

BalthasarBux

Galloway Pepping
Registriert
25.12.19
Beiträge
1.361
Liegen OneDrive-Dateien denn nicht im Dateisystem, nämlich im Benutzerordner?
 

Jan K.

Stechapfel
Registriert
17.05.15
Beiträge
158
Unverständnis war ja da. Dies hatte keinerlei Bewertung deiner oder meiner Ausführungen zu Grunde. Es tut mir leid, wenn du die ganze Sache persönlich nimmst. Ist nicht meine Absicht. Und deine Antwort wurde ja nicht nur von mir als wenig hilfreich wahrgenommen.

Zudem könnte dein Rat ja auch für andere hilfreich sein. Aber es ist dein gutes Recht nun eingeschnappt zu sein.
Falls jemand die Lösung kennt es auch für OneDrive möglich zu machen, dann wäre ich sehr dankbar.

Liebe Grüße Jan
 

sedna

Galloway Pepping
Registriert
22.10.08
Beiträge
1.358
Hallo,

nee.... ich nehme Antworten in einem Forum nie persönlich. Sie sind es nämlich nicht wirklich!
Aber wenn man es so formulieren möchte: es ist meine/unsere Hilfe, die persönlich ankommt.

Zur Frage von @BalthasarBux:
Ja und nein. Cloud Dienste wie OneDrive greifen auf die Datei je nach Einstellung unterschiedlich zu.
In diesem Fall hier liegt sie zwar physikalisch auf dem Mac, es wird aber auf das Dokument in der Cloud verlinkt.
Und dieser Pfad ist ein URL-Link und kein für das Dateisystem benutzter POSIX- oder HFS-Pfad.

POSIX- und HFS-Pfade kann man in das entsprechende andere Format umwandeln. AppleScript kann das, stellt sich aber dumm, wenn es sich um andere Pfade wie z.B. einen URL-Link handelt.
POSIX Pfade sind ausschließlich durch einzelne Schrägstriche / getrennt, HFS-Pfade durch Doppelpunkte :
"/Volumes/Media/Temp/xyz.doc"
"Media:Temp:xyz.doc"

URL-Links sehen so aus
"https://www.apfeltalk.de/community/threads/word-in-pdf-umwandeln.554602/"

Sie enthalten einen Doppelpunkt und doppelte Schrägstriche. Wird wie hier versucht, das in einen POSIX-Pfad zu wandeln, kommt Kauderwelsch bei raus
"/https/::www.apfeltalk.de:community:threads:word-in-pdf-umwandeln.554602:"

Das hier erzeugt solch ein Kauderwelsch
set thisPath to POSIX path of thisPath

Ersetzen durch
Code:
        if thisPath starts with "https://" then
            set thisPath to thisPath
        else
            set thisPath to POSIX path of thisPath
        end if

Und nicht so schlimm, aber trotzdem eine leichte Dauerwelle erzeugend und besser so geschrieben
Code:
if (thisFile as string) contains ".docx" or (thisFile as string) contains ".doc" then

Gruß