[GastForen Programmierung/Entwicklung AppleScript PDFs in Ordner suchen und zu PDF kombinieren

  • Suche
  • Hilfe
  • Lesezeichen
  • Benutzerliste
Themen
Beiträge
Moderatoren
Letzter Beitrag

PDFs in Ordner suchen und zu PDF kombinieren

mb_new
Beiträge gesamt: 123

28. Feb 2017, 17:22
Beitrag # 1 von 14
Bewertung:
(9909 mal gelesen)
URL zum Beitrag
Beitrag als Lesezeichen
Hallo zusammen!

Ich habe in einem Ordner PDFs liegen, welche immer folgende Dateinamen haben:
zB 001-Dateinamexxy, 044-Dateinamezzz, 005-AX_Dateiname

Nun möchte ich mittels Script alle Dateien finden, welche nach de Numemr und Bindestrich mit "AX_" beginnen.
Dieses Pdf möchte ich dann zusammen mit der vorigen Nummer und dem Dokument mit 001 beginnend zu einem Pdf zusammenfügen.
Das sollte so oft funktionieren, so viele Dokumente mit "AX_" vorhanden sind.

Vielleicht ein Beispiel:
001-Dok1
002-Dok2
003-Dok3
004-AX_Dok4
005-Dok5
006-AX_Dok6

Resultat:
PDF 1 mit 001, 003, 004
PDF 2 mit 001, 005, 006

Ich hoffe ich hab mich halbwegs verständlich ausgedrückt.
Ich hab es auch schon mit dem Automator probiert, bin aber noch auf keinen grünen Zweig gekommen.

Danek
Martin
X

PDFs in Ordner suchen und zu PDF kombinieren

mb_new
Beiträge gesamt: 123

2. Mär 2017, 13:53
Beitrag # 2 von 14
Beitrag ID: #555999
Bewertung:
(9837 mal gelesen)
URL zum Beitrag
Beitrag als Lesezeichen
Hallo,
ich bin schon ein wenig weiter und habe mit diversen Schnipseln undmeinen bescheidenen Kenntnissen auch schon ein Skript beisammen, welches mir tatsächlich ausführt, was ich haben möchte.

ABER, GROSSES ABER: das funktioniert nur für eine Datei!
Was ich brauche ist, dass das Skript so lange läuft, bis alle AD_ Dateien gefunden sind.

Wahrscheinlich denke ich viel zu kompliziert, doch es funktioniert.
Code
-- SUCHE FÜR COVER DOKUMENT, beginnend mit 001 
set pdfFolder to POSIX path of ((path to desktop as text) & "LowResPdf") -- Assuming this really has to be a POSIX path.

set searchstrings to {"001"}
set pdfFolderPath to (POSIX file pdfFolder) as text -- Get the POSIX path as an HFS path.

tell application "Finder" to set filenames to name of every file of folder pdfFolderPath
repeat with i from 1 to (count filenames)
set thisName to item i of filenames
set matched to false
-- Check each file name against each search string in turn.
repeat with j from 1 to (count searchstrings)
set matched to (thisName contains item j of searchstrings)
-- Give up immediately on any mismatch.
if (not matched) then exit repeat
end repeat
if (matched) then
set coverFile to thisName
end if
end repeat

-- SUCHE FÜR ADVERTISING DOKUMENT, beinhaltet "AD_" im Namen, zB "025-AD_Dokumentenname.pdf"
set searchstrings to {"AD_"}
set pdfFolderPath to (POSIX file pdfFolder) as text -- Get the POSIX path as an HFS path.

tell application "Finder" to set filenames to name of every file of folder pdfFolderPath
repeat with i from 1 to (count filenames)
set thisName to item i of filenames
set matched to false
-- Check each file name against each search string in turn.
repeat with j from 1 to (count searchstrings)
set matched to (thisName contains item j of searchstrings)
-- Give up immediately on any mismatch.
if (not matched) then exit repeat
end repeat
if (matched) then
set adFile to thisName
end if
end repeat

-- BERECHNET DIE SEITENNUMMER VOR DEM ADVERTISING DOKUMENT
set Var1 to ((characters 1 thru 3 of adFile) as string)
set Var1 to Var1
set Var2 to 1
set Var3 to Var1 - Var2
set Var3 to addLeadingZerosToNumber(Var3, 2)

-- SUCHE FÜR DOKUMENT mit Seitenzahl VOR dem "AD_ Dokument"
set searchstrings to {Var3}
set pdfFolderPath to (POSIX file pdfFolder) as text -- Get the POSIX path as an HFS path.

tell application "Finder" to set filenames to name of every file of folder pdfFolderPath
repeat with i from 1 to (count filenames)
set thisName to item i of filenames
set matched to false
-- Check each file name against each search string in turn.
repeat with j from 1 to (count searchstrings)
set matched to (thisName contains item j of searchstrings)
-- Give up immediately on any mismatch.
if (not matched) then exit repeat
end repeat
if (matched) then
set preadFile to thisName
end if
end repeat


set coverFilePath to (POSIX file coverFile) as text -- Get the POSIX path as an HFS path.
set coverFilenew to pdfFolderPath & coverFilePath as text

set preadFilePath to (POSIX file preadFile) as text -- Get the POSIX path as an HFS path.
set preadFilenew to pdfFolderPath & preadFilePath as text

set adFilePath to (POSIX file adFile) as text -- Get the POSIX path as an HFS path.
set adFilenew to pdfFolderPath & adFilePath as text

-- HIER WIRD DAS PDF DER 3 DAZTEIEN KOMBINIERT
use scripting additions
use framework "Foundation"
use framework "Quartz" -- required for PDF stuff

set inFiles to {coverFilenew, preadFilenew, adFilenew}

set destPosixPath to POSIX path of (choose file name default name "EBEL.pdf" with prompt "Save new PDF to:")
its combineFiles:inFiles savingTo:destPosixPath

on combineFiles:inFiles savingTo:destPosixPath
-- make URL of the first PDF
set inNSURL to current application's class "NSURL"'s fileURLWithPath:(POSIX path of item 1 of inFiles)
-- make PDF document from the URL
set theDoc to current application's PDFDocument's alloc()'s initWithURL:inNSURL
-- loop through the rest
set oldDocCount to theDoc's pageCount()
set inFiles to rest of inFiles
repeat with aFile in inFiles
-- make URL of the next PDF
set inNSURL to (current application's class "NSURL"'s fileURLWithPath:(POSIX path of aFile))
-- make PDF document from the URL
set newDoc to (current application's PDFDocument's alloc()'s initWithURL:inNSURL)
-- loop through, moving pages
set newDocCount to newDoc's pageCount()
repeat with i from 1 to newDocCount
-- get page of old PDF
set thePDFPage to (newDoc's pageAtIndex:(i - 1)) -- zero-based indexes
-- insert the page
(theDoc's insertPage:thePDFPage atIndex:oldDocCount)
set oldDocCount to oldDocCount + 1
end repeat
end repeat
set outNSURL to current application's class "NSURL"'s fileURLWithPath:destPosixPath
-- save the new PDF
(theDoc's writeToURL:outNSURL)
end combineFiles:savingTo:

-- HANDLER FÜR FÜHRENDE NULLEN
on addLeadingZerosToNumber(theNumber, theMaxLeadingZeroCount)
-- Determine if the number is negative
set isNegative to theNumber is less than 0
-- Determine when the maximum number of digits will be reached
set theThreshold to (10 ^ theMaxLeadingZeroCount) as integer
-- If the number is shorter than the maximum number of digits
if theNumber is less than theThreshold then
-- If the number is negative, convert it to positive
if isNegative = true then set theNumber to -theNumber
-- Add the zeros to the number
set theLeadingZeros to ""
set theDigitCount to length of ((theNumber div 1) as string)
set theCharacterCount to (theMaxLeadingZeroCount + 1) - theDigitCount
repeat theCharacterCount times
set theLeadingZeros to (theLeadingZeros & "0") as string
end repeat
-- Make the number negative, if it was previously negative
if isNegative = true then set theLeadingZeros to "-" & theLeadingZeros
-- Return the prefixed number
return (theLeadingZeros & (theNumber as text)) as string
-- If the number is greater than or equal to the maximum number of digits
else
-- Return the original number
return theNumber as text
end if
end addLeadingZerosToNumber



als Antwort auf: [#555938]

PDFs in Ordner suchen und zu PDF kombinieren

mb_new
Beiträge gesamt: 123

2. Mär 2017, 14:20
Beitrag # 3 von 14
Beitrag ID: #556000
Bewertung:
(9835 mal gelesen)
URL zum Beitrag
Beitrag als Lesezeichen
Hier mein Ansatz um zu probieren, dass das Skript alle Dateien, welche "AD_" beinhalten findet und zusammensetzt.

Code ist absichtlich nicht vollständig, damit es nicht zu umfangreich wird.

Doch es scheitert schon daran, dass die Schleife nicht weiterläuft, wenn der "searchstring" nicht passt.

Bitte die Experten um Hilfe und Ideen.

Danke
Martin
Code
-- SUCHE FÜR ADVERTISING DOKUMENT, beinhaltet "AD_" im Namen, zB "025-AD_Dokumentenname.pdf" 
set pdfFolder to POSIX path of ((path to desktop as text) & "LowResPdf") -- Assuming this really has to be a POSIX path.

-- Liste des Inhalts erstellen
tell application "System Events"
set contentsList to name of every file of folder (pdfFolder as Unicode text)
end tell

-- Wiederholschleife, welche alle Dateien im Ordner zum Handler 'processSuche' schickt
repeat with curItem in contentsList
set filePath to ((pdfFolder as Unicode text) & "/" & curItem)
-- unsichtbare Dateien ausschliessen
if not (curItem starts with ".") then
processSuche(curItem)
end if
end repeat

-- HANDLER processSuche
on processSuche(curItem)
set searchstring to {"AD_"}

repeat with i from 1 to (count curItem)
if curItem contains searchstring then
set adfile to curItem
end if
end repeat
-- BERECHNET DIE SEITENNUMMER VOR DEM ADVERTISING DOKUMENT
set Var1 to ((characters 1 thru 3 of adfile) as string)

set Var1 to Var1
set Var2 to 1
set Var3 to Var1 - Var2
set Var3 to addLeadingZerosToNumber(Var3, 2)

end processSuche


-- HANDLER FÜR FÜHRENDE NULLEN
on addLeadingZerosToNumber(theNumber, theMaxLeadingZeroCount)
-- Determine if the number is negative
set isNegative to theNumber is less than 0

-- Determine when the maximum number of digits will be reached
set theThreshold to (10 ^ theMaxLeadingZeroCount) as integer

-- If the number is shorter than the maximum number of digits
if theNumber is less than theThreshold then
-- If the number is negative, convert it to positive
if isNegative = true then set theNumber to -theNumber

-- Add the zeros to the number
set theLeadingZeros to ""
set theDigitCount to length of ((theNumber div 1) as string)
set theCharacterCount to (theMaxLeadingZeroCount + 1) - theDigitCount
repeat theCharacterCount times
set theLeadingZeros to (theLeadingZeros & "0") as string
end repeat

-- Make the number negative, if it was previously negative
if isNegative = true then set theLeadingZeros to "-" & theLeadingZeros

-- Return the prefixed number
return (theLeadingZeros & (theNumber as text)) as string

-- If the number is greater than or equal to the maximum number of digits
else
-- Return the original number
return theNumber as text
end if
end addLeadingZerosToNumber



als Antwort auf: [#555999]

PDFs in Ordner suchen und zu PDF kombinieren

Hans Haesler
  
Beiträge gesamt: 5826

2. Mär 2017, 16:53
Beitrag # 4 von 14
Beitrag ID: #556006
Bewertung:
(9797 mal gelesen)
URL zum Beitrag
Beitrag als Lesezeichen
Hallo Martin,

ich habe eine Lösung, mit welcher in einer Schleife bei jedem Durchgang die "AX"-Datei, die davorliegende und die "001" in einen Ordner dupliziert.

Doch das Zusammenhängen will noch nicht gelingen.

Jetzt bin ich unterwegs. Aber im Laufe des Abends werde ich Deine Lösungen anschauen.

Gruss, Hans


als Antwort auf: [#556000]

PDFs in Ordner suchen und zu PDF kombinieren

Hans Haesler
  
Beiträge gesamt: 5826

2. Mär 2017, 21:03
Beitrag # 5 von 14
Beitrag ID: #556011
Bewertung:
(9751 mal gelesen)
URL zum Beitrag
Beitrag als Lesezeichen
Hallo Martin,

aus Zeitmangel habe ich Deine Lösungen nicht ausprobiert.

Doch hier meine Lösung, welche je drei Dateien in separate Ordner auf dem Desktop dupliziert.
Die Zeilen, welche die PDFs vereinen sollen, habe ich entfernt, weil es nicht klappte.

Aber vielleicht kannst Du mit meinem Ansatz schon etwas anfangen.

Code
-- den Quellordner auswählen lassen  
set sourceFolder to (choose folder) as Unicode text

-- die Liste des Inhalts wird erstellt
tell application "System Events"
set contentsList to name of every file of folder sourceFolder
end tell
set nItems to count of contentsList

-- das Start-Objekt feststellen und die "AD_" zählen
set startObject to ""
set ctr to 0
repeat with curItem in contentsList
-- unsichtbare Dateien ausschliessen
if not (curItem starts with ".") then
set curNum to (text 1 thru 3 of curItem)
if curNum is "001" then
set startObject to curItem
else
if curItem contains "AD_" then
set ctr to ctr + 1
end if
end if
end if
end repeat

-- prüfen, ob das Startobjekt gefunden worden ist ...
if startObject is "" then
display dialog "Das 001-PDF nicht gefunden." buttons "OK" default button 1 with icon 2
error number -128
end if
-- ... und ob "AD_"-Objekte vorhanden sind
if ctr is 0 then
display dialog "Kein \"AD_\"-PDF vorhanden." buttons "OK" default button 1 with icon 2
error number -128
end if

-- eine Rückwärts-Schleife durch die Liste der Namen
repeat with n from nItems to 1 by -1
set curItem to item n of contentsList
-- unsichtbare Dateien ausschliessen
if not (curItem starts with ".") then
if curItem contains "AD_" then
try
set preItem to item (n - 1) of contentsList
set objList to {startObject, preItem, curItem}
my combinePDF(sourceFolder, objList, ctr)
set ctr to ctr - 1
end
end if
end if
end repeat

-- den Anwender informieren
activate me
display dialog "Fertig." buttons "OK" default button 1 with icon 1 giving up after 1

-- der Handler, welcher die PDFs vereint
on combinePDF(folPath, objList, ctr)

-- die Pfade der drei Dateien
set fileOne to (folPath & (item 1 of objList)) as string
set fileTwo to (folPath & (item 2 of objList)) as string
set fileThr to (folPath & (item 3 of objList)) as string

-- die drei aktuellen Dateien duplizieren
tell application "Finder"
try
set curFolderPath to (make new folder at (path to desktop) with properties {name:"CombinePDF_" & ctr})
duplicate file fileOne to curFolderPath
duplicate file fileTwo to curFolderPath
duplicate file fileThr to curFolderPath
end try
end tell

end combinePDF

Gruss, Hans


als Antwort auf: [#556006]

PDFs in Ordner suchen und zu PDF kombinieren

-hans-
Beiträge gesamt: 748

3. Mär 2017, 09:23
Beitrag # 6 von 14
Beitrag ID: #556017
Bewertung:
(9655 mal gelesen)
URL zum Beitrag
Beitrag als Lesezeichen
Hallo,

beide in diesem Thread angebotenen Lösungen zum Zusammenfügen von PDF-Dateien scheinen zu funktionieren (OSX 10.11.5): http://macscripter.net/viewtopic.php?id=44250


als Antwort auf: [#556000]

PDFs in Ordner suchen und zu PDF kombinieren

Hans Haesler
  
Beiträge gesamt: 5826

3. Mär 2017, 17:04
Beitrag # 7 von 14
Beitrag ID: #556047
Bewertung:
(9627 mal gelesen)
URL zum Beitrag
Beitrag als Lesezeichen
Hallo Hans-Gerd,

besten Dank für den Hinweis. Das Script für Mavericks funktioniert. Aber für die vorliegende Aufgabe ist es nicht geeignet: Die Dateien werden nicht ausgewählt (sondern der Quellordner) und pro Arbeitsgang sollten mehrere kombinierte PDF-Dateien entstehen.

Zunächst habe ich erfolglos versucht, die wichtigen Zeilen in das schon gepostete Script einzufügen.

Doch mit der folgenden, vereinfachten Version klappt es. Die Dateien werden nicht mehr in einen Ordner kopiert.

Code
(*   
PDF_kombinieren_01d.scpt
Dieses Script kombiniert PDF-Dateien
© 03.03.2017 / Hans Haesler, Châtelard 52, CH-1018 Lausanne

Credits: Die 'joinPy'-Variable und den do-shell-script-Befehl einem Beitrag
von kerflooey im MacScripter-Forum entnommen ( http://macscripter.net/viewtopic.php?id=44250 )
*)

property joinPy : quoted form of "/System/Library/Automator/Combine PDF Pages.action/Contents/Resources/join.py"
property deskPath : path to desktop

-- den Quellordner auswählen lassen
set sourceFolder to (choose folder) as Unicode text

-- die Liste des Inhalts wird erstellt
tell application "System Events"
set contentsList to name of every file of folder sourceFolder
end tell
set nItems to count of contentsList

-- das Start-Objekt feststellen und die "AD_" zählen
set startObject to ""
set ctr to 0
repeat with curItem in contentsList
-- unsichtbare Dateien ausschliessen
if not (curItem starts with ".") then
set curNum to (text 1 thru 3 of curItem)
if curNum is "001" then
set startObject to curItem
else
if curItem contains "AD_" then
set ctr to ctr + 1
end if
end if
end if
end repeat

-- prüfen, ob das Startobjekt gefunden worden ist ...
if startObject is "" then
display dialog "Das 001-PDF nicht gefunden." buttons "OK" default button 1 with icon 2
error number -128
end if
-- ... und ob "AD_"-Objekte vorhanden sind
if ctr is 0 then
display dialog "Kein \"AD_\"-PDF vorhanden." buttons "OK" default button 1 with icon 2
error number -128
end if

-- eine Rückwärts-Schleife durch die Liste der Namen
repeat with n from nItems to 1 by -1
set curItem to item n of contentsList
-- unsichtbare Dateien ausschliessen
if not (curItem starts with ".") then
if curItem contains "AD_" then
try
-- die Datei vor jener mit "AD_"
set preItem to item (n - 1) of contentsList
-- die Objektliste
set objList to {startObject, preItem, curItem}
-- die aktuelle Ausgabe-Datei
set outputPdf to POSIX path of ((deskPath & "finalPDF_" & ctr & ".pdf") as string)

-- die Pfade der drei Dateien der Variablen 'addPdfs' übergeben
set addPdfs to ""
repeat with anItem in objList
set curPath to (sourceFolder & anItem) as string
set addPdfs to addPdfs & space & quoted form of (POSIX path of (curPath as alias))
end repeat

-- die drei PDFs vereinen
do shell script "python " & joinPy & " -o " & (quoted form of outputPdf) & addPdfs

-- den Zählerwert ändern
set ctr to ctr - 1

end try
end if
end if

end repeat

-- den Anwender informieren
delay 0.5
activate me
display dialog "Fertig." buttons "OK" default button 1 with icon 1 giving up after 1

Allerdings: Der Anwender muss die erzeugten Dateien verschieben. Sonst werden sie bei einer nächsten Ausführung überschrieben.

Gruss, Hans


als Antwort auf: [#556017]
(Dieser Beitrag wurde von Hans Haesler am 3. Mär 2017, 17:05 geändert)

PDFs in Ordner suchen und zu PDF kombinieren

Kalle85
Beiträge gesamt: 2

6. Mär 2017, 11:46
Beitrag # 8 von 14
Beitrag ID: #556064
Bewertung:
(9502 mal gelesen)
URL zum Beitrag
Beitrag als Lesezeichen
Das ist mal ein hammer Script. Vielen Dank!


als Antwort auf: [#556047]

PDFs in Ordner suchen und zu PDF kombinieren

Hans Haesler
  
Beiträge gesamt: 5826

6. Mär 2017, 12:15
Beitrag # 9 von 14
Beitrag ID: #556065
Bewertung:
(9496 mal gelesen)
URL zum Beitrag
Beitrag als Lesezeichen
Hallo Kalle,

herzlich willkommen im AppleScript-Forum auf HilfDirSelbst! :-)

Bitte sehr. Danke für das Lob. Allerdings: Das oben publizierte Script ist eine Sonderausgabe, um die Aufgabe von Martin zu lösen. Und genau diese wird kaum jemand anders haben.

Wenn alle PDF-Dateien eines Ordners vereint werden sollten, müsste das Scrlipt geändert werden.

Aber zur Zeit bin ich im Skiurlaub in Villars-sur-Ollon und werde gleich mit den Langlaufskiern loslegen. Trotz des anhaltende Schneefalls. :-)

Also entweder bis Samstag gedulden. Oder eines der Scripts, auf welche Hans-Gerd hingewiesen hat, verwenden.

Gruss, Hans


als Antwort auf: [#556064]

PDFs in Ordner suchen und zu PDF kombinieren

mb_new
Beiträge gesamt: 123

7. Mär 2017, 12:42
Beitrag # 10 von 14
Beitrag ID: #556108
Bewertung:
(9387 mal gelesen)
URL zum Beitrag
Beitrag als Lesezeichen
Ein großes Danke an Hans!
Es funktioniert.

Allerdings habe ich dein Skript zu spät gesehen.

Ich habe am Wochenende an meinem Ansatz weitergebastelt und hab es auch hingebracht.
Wahrscheinlich dreht es einem Profiskripter, wie dir Hans, den Magen um, welche Wege ich darin gehe.
Aber: es funktioniert einwandfrei und schnell.
Noch dazu habe ich mich gespielt, dass die kombinierten Pdfs mit dem Namen der Ursprungsdatei ergänzt werden.

Das Skript hab ich noch in ein Automator-Service gepackt und der Arbeitstag ist wieder um einiges leichter! :-)

Danke Hans und noch schönen Urlaub!

Code
-- PARAMETER NÖTIG ZUR PDF KOMBINATION 
use scripting additions
use framework "Foundation"
use framework "Quartz" -- required for PDF stuff

-- Ausgangsordner wählen
set pdfFolder to POSIX path of (choose folder)

-- Liste des Inhalts erstellen
set pdfFolderPath to (POSIX file pdfFolder) as text -- Get the POSIX path as an HFS path.
tell application "Finder" to set filenames to name of every file of folder pdfFolderPath

-- SUCHPARAMENTER
set searchstring to {"AD_"}
-- Wiederholschleife, welche alle Dateien im Ordner zum Handler 'processSuche' schickt
repeat with curItem in filenames
if curItem contains searchstring then
processSuche(curItem, pdfFolder, pdfFolderPath, filenames)
end if
end repeat

-- HANDLER processSuche
on processSuche(curItem, pdfFolder, pdfFolderPath, filenames)

-- Doppelpunkt am Ende des Pfades entfernen, sonst funktioniert es nicht, warum auch immer?
set pdfFolderPath to ((characters 1 thru -2 of pdfFolderPath) as string)

--Neuer Ordner für PDFs
set targetFolder to pdfFolderPath
set newName to "E-Belege"
set newFolder to targetFolder & newName

-- testen, ob vorhanden ...
tell application "Finder"
-- wenn nicht vorhanden, den Ordner erzeugen ...
if not (newFolder exists) then
try
make new folder at targetFolder with properties {name:newName}
end try
end if
end tell

-- SUCHPARAMENTER für Cover Dokument
set searchstring to {"001-"}

-- Cover Dokument finden
repeat with coverItem in filenames
if coverItem contains searchstring then
set coverFile to coverItem
end if
end repeat

repeat with i from 1 to (count curItem)
set adFile to curItem

-- BERECHNET DIE SEITENNUMMER VOR DEM ADVERTISING DOKUMENT
set Var1 to ((characters 1 thru 3 of adFile) as string)

set Var1 to Var1
set Var2 to 1
set Var3 to Var1 - Var2
set Var3 to addLeadingZerosToNumber(Var3, 2)

-- SUCHPARAMENTER für preAD Dokument
set searchstring to {Var3}

-- preAD Dokument finden
repeat with preadItem in filenames
if preadItem contains searchstring then
set preadFile to preadItem
end if
end repeat
end repeat


--PFADE ZUR PDF KOMBINATION
set coverFilePath to (POSIX file coverFile) as text
set coverFilenew to pdfFolderPath & coverFilePath as text

set preadFilePath to (POSIX file preadFile) as text
set preadFilenew to pdfFolderPath & preadFilePath as text

set adFilePath to (POSIX file adFile) as text
set adFilenew to pdfFolderPath & adFilePath as text

-- Name kürzen für PDF Namen
set filename to adFile
set newfilename to ((characters 1 thru -5 of filename) as string)
set newerfilename to ((characters 8 thru -1 of newfilename) as string)

-- START ZUM PDF KOMBINIEREN
set inFiles to {coverFilenew, preadFilenew, adFilenew}

-- neuer Pfad für PDF Speicherung
set newPDFfilePath to pdfFolderPath & ":" & newName & ":E-Beleg_" & newerfilename & ".pdf"
set destPosixPath to POSIX path of newPDFfilePath -- (choose file name default name newPDFfilePath)
its combineFiles:inFiles savingTo:destPosixPath

end processSuche

-- HANDLER ZUM PDF KOMBINIEREN
on combineFiles:inFiles savingTo:destPosixPath
-- make URL of the first PDF
set inNSURL to current application's class "NSURL"'s fileURLWithPath:(POSIX path of item 1 of inFiles)
-- make PDF document from the URL
set theDoc to current application's PDFDocument's alloc()'s initWithURL:inNSURL
-- loop through the rest
set oldDocCount to theDoc's pageCount()
set inFiles to rest of inFiles
repeat with aFile in inFiles
-- make URL of the next PDF
set inNSURL to (current application's class "NSURL"'s fileURLWithPath:(POSIX path of aFile))
-- make PDF document from the URL
set newDoc to (current application's PDFDocument's alloc()'s initWithURL:inNSURL)
-- loop through, moving pages
set newDocCount to newDoc's pageCount()
repeat with i from 1 to newDocCount
-- get page of old PDF
set thePDFPage to (newDoc's pageAtIndex:(i - 1)) -- zero-based indexes
-- insert the page
(theDoc's insertPage:thePDFPage atIndex:oldDocCount)
set oldDocCount to oldDocCount + 1
end repeat
end repeat
set outNSURL to current application's class "NSURL"'s fileURLWithPath:destPosixPath
-- save the new PDF
(theDoc's writeToURL:outNSURL)
end combineFiles:savingTo:


-- HANDLER FÜR FÜHRENDE NULLEN
on addLeadingZerosToNumber(theNumber, theMaxLeadingZeroCount)
-- Determine if the number is negative
set isNegative to theNumber is less than 0

-- Determine when the maximum number of digits will be reached
set theThreshold to (10 ^ theMaxLeadingZeroCount) as integer

-- If the number is shorter than the maximum number of digits
if theNumber is less than theThreshold then
-- If the number is negative, convert it to positive
if isNegative = true then set theNumber to -theNumber

-- Add the zeros to the number
set theLeadingZeros to ""
set theDigitCount to length of ((theNumber div 1) as string)
set theCharacterCount to (theMaxLeadingZeroCount + 1) - theDigitCount
repeat theCharacterCount times
set theLeadingZeros to (theLeadingZeros & "0") as string
end repeat

-- Make the number negative, if it was previously negative
if isNegative = true then set theLeadingZeros to "-" & theLeadingZeros

-- Return the prefixed number
return (theLeadingZeros & (theNumber as text)) as string

-- If the number is greater than or equal to the maximum number of digits
else
-- Return the original number
return theNumber as text
end if
end addLeadingZerosToNumber



als Antwort auf: [#556065]

PDFs in Ordner suchen und zu PDF kombinieren

mb_new
Beiträge gesamt: 123

7. Mär 2017, 12:44
Beitrag # 11 von 14
Beitrag ID: #556110
Bewertung:
(9386 mal gelesen)
URL zum Beitrag
Beitrag als Lesezeichen
Und hier noch ein Skript, welches ich benutze um PDFs zusammenzufügen.

Ich weiß allerdings nicht mehr, wo das her ist.
Vielleicht kann es ja wer brauchen.

Schöne Grüße
Martin
Code
use scripting additions 
use framework "Foundation"
use framework "Quartz" -- required for PDF stuff


set inFiles to (choose file of type {"pdf"} with prompt "Choose your PDF files:" with multiple selections allowed)
set destPosixPath to POSIX path of (choose file name default name "Combined.pdf" with prompt "Save new PDF to:")
its combineFiles:inFiles savingTo:destPosixPath

on combineFiles:inFiles savingTo:destPosixPath
-- make URL of the first PDF
set inNSURL to current application's class "NSURL"'s fileURLWithPath:(POSIX path of item 1 of inFiles)
-- make PDF document from the URL
set theDoc to current application's PDFDocument's alloc()'s initWithURL:inNSURL
-- loop through the rest
set oldDocCount to theDoc's pageCount()
set inFiles to rest of inFiles
repeat with aFile in inFiles
-- make URL of the next PDF
set inNSURL to (current application's class "NSURL"'s fileURLWithPath:(POSIX path of aFile))
-- make PDF document from the URL
set newDoc to (current application's PDFDocument's alloc()'s initWithURL:inNSURL)
-- loop through, moving pages
set newDocCount to newDoc's pageCount()
repeat with i from 1 to newDocCount
-- get page of old PDF
set thePDFPage to (newDoc's pageAtIndex:(i - 1)) -- zero-based indexes
-- insert the page
(theDoc's insertPage:thePDFPage atIndex:oldDocCount)
set oldDocCount to oldDocCount + 1
end repeat
end repeat
set outNSURL to current application's class "NSURL"'s fileURLWithPath:destPosixPath
-- save the new PDF
(theDoc's writeToURL:outNSURL)
end combineFiles:savingTo:



als Antwort auf: [#556108]

PDFs in Ordner suchen und zu PDF kombinieren

Kalle85
Beiträge gesamt: 2

7. Mär 2017, 18:03
Beitrag # 12 von 14
Beitrag ID: #556148
Bewertung:
(9345 mal gelesen)
URL zum Beitrag
Beitrag als Lesezeichen
Antwort auf [ Hans Haesler ] Hallo Kalle,

herzlich willkommen im AppleScript-Forum auf HilfDirSelbst! :-)

Bitte sehr. Danke für das Lob. Allerdings: Das oben publizierte Script ist eine Sonderausgabe, um die Aufgabe von Martin zu lösen. Und genau diese wird kaum jemand anders haben.

Wenn alle PDF-Dateien eines Ordners vereint werden sollten, müsste das Scrlipt geändert werden.

Aber zur Zeit bin ich im Skiurlaub in Villars-sur-Ollon und werde gleich mit den Langlaufskiern loslegen. Trotz des anhaltende Schneefalls. :-)

Also entweder bis Samstag gedulden. Oder eines der Scripts, auf welche Hans-Gerd hingewiesen hat, verwenden.

Gruss, Hans


Danke für das nette Willkommen und viel Spaß auf der Piste, Hans!

Ich nutze jetzt das letzte gepostete Script. Es funktioniert und ist eher was allgemeines.
Seither habe ich meine PDFs immer bei http://www.pdf-zusammenfuegen.com/ kombiniert... allerdings habe ich da immer ein bisschen Bauchweh, sensible Daten auf einen fremden Server zu laden.
Von daher kommen mir die Scripte wie gerufen.


als Antwort auf: [#556065]

PDFs in Ordner suchen und zu PDF kombinieren

Hans Haesler
  
Beiträge gesamt: 5826

7. Mär 2017, 18:42
Beitrag # 13 von 14
Beitrag ID: #556152
Bewertung:
(9335 mal gelesen)
URL zum Beitrag
Beitrag als Lesezeichen
Hallo Kalle,

bitte sehr. Und ja, auch heute hatte ich Spass in der Loipe. :-)

Eine Bitte: Nicht den gesamten Text des Vorredners zitieren.

Wenn ich es richtig verstanden habe, benutzest Du das von Martin gepostete Script zum Zusammenfügen. Das scheint mir wesentlich geeigneter als meines, welches Dateien mit "AD_" und "001" im Namen erwartet.

Gruss, Hans


als Antwort auf: [#556148]

PDFs in Ordner suchen und zu PDF kombinieren

Hans Haesler
  
Beiträge gesamt: 5826

7. Mär 2017, 20:46
Beitrag # 14 von 14
Beitrag ID: #556154
Bewertung:
(9308 mal gelesen)
URL zum Beitrag
Beitrag als Lesezeichen
Hallo Martin,

gut, dass Du Dir selber hast helfen können.

In meiner Lösung habe ich die Seitennumer nicht berücksichtigt, weil keine Informationen vorlagen.

Nach meinem Urlaub werde ich versuchen, ein allgemeines Script zu schreiben, welches unter "Mavericks" funktioniert.

Gruss, Hans


als Antwort auf: [#556110]
X