zusammengehörende pdf-Dateien finden und zusammenfügen

J

jaypee

Registriert
Thread Starter
Dabei seit
11.10.2003
Beiträge
1
Reaktionspunkte
0
Hallo,

in einem Ordner laufen automatisch generierte pdf-Dateien auf. Diese weisen als Dateinamen eine automatisch generierte 5-stellige Zahl (=Jobnummer) auf
sowie einen Zusatz, der die Seitenzahl darstellt.

also z.B:

11111-1
22222-1
33333-1
44444-1
55555-1
55555-2
55555-3
55555-4
55555-5

66666-1
77777-1
88888-1
99999-1

Nun möchte alle mehrseitigen Jobs (hier 55555) automatisch finden, sortieren und zu einer einzigen pdf zusammenfügen lassen.
Hat hierfür jemand zufällig ein Applescript oder bash-script parat ?

Vielen Dank.

Gruß,
Jörg
 
Hallo,

probier mal folgendes Skript aus. Das musst du als Programm speichern und dann die PDF Dateien per Drag&Drop draufziehen. Wichtig ist, dass die Dateien in dem Ordner nach Name geordnet sind, sonst kann es sein, dass die Reihenfolge der Seiten nicht stimmt.

Code:
property join_py : quoted form of "/System/Library/Automator/Combine PDF Pages.action/Contents/Resources/join.py"


on open dropfiles
	tell application "Finder"
		
		repeat with thisFile in dropfiles
			set outFile to (container of (thisFile as alias) as string) & word 1 of (name of (thisFile) as string) & ".pdf"
			if my getFile(outFile) is false then
				set filefound to false
				set sameFiles to quoted form of (POSIX path of (thisFile as alias))
				
				
				repeat with thatFile in dropfiles
					if ((word 1 of (name of thisFile as string)) = (word 1 of (name of thatFile as string))) & ((name of thisFile as string) ≠ (name of thatFile as string)) is {true, true} then
						set sameFiles to sameFiles & " " & quoted form of (POSIX path of (thatFile as alias))
						
						set filefound to true
					end if
				end repeat
				
				if filefound is true then
					set joinpdf to "python " & join_py & " -o " & quoted form of (POSIX path of outFile) & " " & sameFiles
					do shell script joinpdf
				end if
				get joinpdf
			end if
		end repeat
	end tell
end open

on getFile(filepath)
	try
		(filepath as alias)
		return true
	on error
		return false
	end try
end getFile

Das Skript ist noch nicht so wirklich performant, probier einfach mal aus, ob es für deine Zwecke reicht.

Viele Grüße,

Pill
 
Und schon folgt Version 1.1 (ja, ich habe nichts zu tun):

Code:
property join_py : quoted form of "/System/Library/Automator/Combine PDF Pages.action/Contents/Resources/join.py"

on open dropfiles
	
	set dropnames to {}
	
	tell application "Finder"
		set dropfolder to POSIX path of (container of (item 1 of dropfiles) as alias) as string
		repeat with i in dropfiles
			set end of dropnames to name of i
		end repeat
	end tell
	
	set AppleScript's text item delimiters to "
"
	set sorted to "echo '" & (dropnames as string) & "' | sort"
	set AppleScript's text item delimiters to ""
	set fileNames to every paragraph of (do shell script sorted)
	
	set dropnamesSorted to my ListSortedGroup(fileNames)
	
	repeat with listGroup in dropnamesSorted
		if (count of listGroup) > 1 then
			set inputFiles to ""
			set outputFile to "'" & dropfolder & word 1 of item 1 of listGroup & ".pdf' "
			repeat with dropname in listGroup
				set inputFiles to inputFiles & "'" & dropfolder & dropname & "' "
			end repeat
			set joinpdf to "python " & join_py & " -o " & outputFile & inputFiles
			do shell script joinpdf
		end if
	end repeat
	
end open

on ListSortedGroup(listSorted)
	
	set listGroups to {}
	set n to 0
	
	repeat with i from 1 to (count of listSorted)
		set searchString to word 1 of item (n + 1) of listSorted
		set AppleScript's text item delimiters to "
"
		set sed to ("echo '" & listSorted as string) & "' | sed -n /^" & searchString & "/p"
		set AppleScript's text item delimiters to ""
		set end of listGroups to every paragraph of (do shell script sed)
		set n to n + (count of item i of listGroups)
		if n = (count of listSorted) then exit repeat
	end repeat
	
	return listGroups

end ListSortedGroup

Neu in Version 1.1:

~ Stark verbesserte Performance
~ Die Reihenfolge der Dateien ist nun egal
 
Zuletzt bearbeitet:
  • Gefällt mir
Reaktionen: EmilS
Na wenn du schon nichts zu tun hast und dir die Zeit genommen hast dem TE so eine umfangreiche Lösung zu bieten, dann wäre es nur ehrenhaft von ihm dich dafür auch zu entlohnen. :cake:
 
Zuletzt bearbeitet:
Zurück
Oben Unten