Join all pdf's in a subfolder

I have had a request where I need to select a folder which contains subfolders, which inside, have either one or more pdf’s.
I have found a fantastic command line package (joinpdf) which is part of the itext project, which can join the pdf’s, but I am fairly green at this intense level of applescript.
The way I was tackling this problem was to set up a repeat command to query the subfolders, and then in this repeat, get the joinpdf command to create a pdf under the chosen folder with the name of the folder containing all the pdf’s i was trying to join. My stumbling block is getting the names of these files to come out in a order similar to this “/pathtochosenfolder/chosenfolder/subfolder1/1.pdf /pathtochosenfolder/chosenfolder/subfolder1/2.pdf, etc, etc” as the joinpdf command works like this joinpdf /pattochosenfolder/subfoldername.pdf /pathtochosenfolder/chosenfolder/subfolder1/1.pdf /pathtochosenfolder/chosenfolder/subfolder1/2.pdf, etc, etc" with spaces between the pdf’s being to separate arguments. So far, this is my applescript for getting information on the folders and files, but I cannot seem to get my head around how I can achieve my goal. Any help is more than appreciated.

on run
	global sourcefolder
	set sourcefolder to choose folder
	global sub_folders
	set sub_folders to {}
	set the_folder_list to list folder sourcefolder without invisibles
	set the_count to count of the_folder_list
	repeat with i from 1 to number of items in the_folder_list
		set this_items to item i of the_folder_list
		set folder_action to this_items
		set initial_source to sourcefolder & this_items & ":" as string
		set kachup to initial_source
		set sub_cat to this_items & ":" as string
		set inforec to info for file (initial_source)
		if (kind of inforec) is "folder" then set end of sub_folders to (name of inforec)
		set the_files to {}
		--tell application "Finder" to set folder_list to entire contents of folder folder_action & ":"
		tell application "Finder"
			set allFiles to {}
			set allFiles to (files of entire contents of alias kachup whose name extension is "pdf")
		end tell
		repeat with i from 1 to number of items in allFiles
			set name_step to {}
			set name_step to name of item i of allFiles
		end repeat
	end repeat
end run

Hi cs_field,

for a command line tool it’s necessary to coerce all paths to POSIX paths

this should do the job:

set sourcefolder to choose folder
tell application "Finder" to set sub_folders to folders of sourcefolder
repeat with one_folder in sub_folders
	tell application "Finder" to set {the_pdfs, folder_name} to {files of one_folder whose name extension is "pdf", name of one_folder}
	set pdfPathList to ""
	repeat with one_pdf in the_pdfs
		set pdfPathList to pdfPathList & space & quoted form of (POSIX path of (one_pdf as alias))
	end repeat
	set joinPDF to "joinpdf " & quoted form of ((POSIX path of sourcefolder) & folder_name & ".pdf") & pdfPathList
	-- do shell script joinPDF
end repeat

Notes: if you have only a run handler there is no need for on run - end run.
Global variables in a single handler are not necessary.
It’s also not necessary to declare the variable, set the value to 0 and then set the value to something else

thanks for that, I will give this a go. I knew I was going wrong :expressionless:

Works fine :D:D:D:D:D:D:D

What I need to do to be able to run this either by launching the script or by passing a folder name from another script. As I intend to try and integrate this with our filemaker database, and specify the chosen folder option from filemaker.

I changed one line to proceed only pdf files:

 tell application "Finder" to set {the_pdfs, folder_name} to {files of one_folder whose name extension is "pdf", name of one_folder}

Will this allow the script to be called by filemaker, and for filemaker to pass the top level foldername (ie replace choose folder) to the script?

I don’t use FileMaker but this is certainly possible.
Probably somebody else can help