Save AI files as PDF in subfolders

Hello,

I need to export in PDF many AI files which are in many folders. I’m trying to write a script that allow me find required AI files, open these in Adobe Illustrator, and save as PDF with use specific preset.
So this is what I have:


set sourceFolder to choose folder with prompt "Source folder"
tell application "Finder" to set fileList to (every file of entire contents of folder sourceFolder whose name contains "box" and name extension is "ai") as alias list
set destFolder to sourceFolder
on SavePDF(fileList, destFolder)
	set destPath to destFolder as Unicode text
	repeat with aFile in fileList
		tell application "Finder" to set fileName to name of aFile
		set newFilePath to destPath & fileName
		tell application "Adobe Illustrator"
			open aFile
			save current document in file newFilePath as pdf with options {class:PDF save options, PDF preset:"my_preset"}
			close current document saving no
		end tell
	end repeat
end SaveFilesAsPDF
SavePDF(fileList, destFolder)

All of that work nice, besides saving PDFs in right places.
AI files are in many subfolders and I have no idea how should look this line: “set destFolder to sourceFolder” to make individual PDFs will be save in the same folders where are original AI files.

I hope that someone can help me solve this problem.
Thanks in advance and Regards

I think something like this is what you are after:

on SavePDFs(fileList)
	repeat with aFile in fileList
		set filePath to aFile as text
		set newFilePath to (text 1 thru -3 of filePath) & "pdf"
		tell application "Adobe Illustrator"
			open aFile
			save current document in file newFilePath as pdf with options {class:PDF save options, PDF preset:"my_preset"}
			close current document saving no
		end tell
	end repeat
end SavePDFs

That’s it! Actually only this one line is like enlightenment for me: “set newFilePath to (text 1 thru -3 of filePath) & "pdf”". Thanks a lot and regards! :slight_smile: