InDesign: Export PDF with name of linked image

Hello I am new to MacScripter so please let me know if there’s anything I should be doing differently.

I am working on modifying an existing script for InDesign. Currently, the script exports each page of the open doc as a PDF with specific naming structure, and it runs a find/replace to get rid of any illegal characters. So now, the department we make the PDFs for would like the name of a linked image (it’s a barcode) to be added to the end of the PDF name. It is possible that the document could be 10 pages long and have a different barcode per page. It is also possible that the document could be 2 pages long, but only have a barcode on the first page“in this case I would want the second page to have something like “NoBarcode” added to the end of the PDF name. The barcode (if there is one) is on it’s own layer for every page. If there is no barcode, then the barcode layer would still exist even though it would be empty.

I’ve been playing around with it for about a week now and can’t get it to look at the link on EACH page’s barcode. I’ve been googling non stop to find new language to try but I am stumped. I’m by no means a pro scripter, and still learning as much as I can.

Any help is GREATLY appreciated. I am really impressed with the community here. Many thanks to anyone with any ideas.

Here is what I have so far (this is just the piece of the script that makes the PDFs). I am using a “Yes” or “No” system just to test if it worked. Eventually I would replace that with the name of the link:

[code]tell app “Adobe InDesign CC 2014”
repeat with myCounter from 1 to (count pages in myDocument)
set myPageName to name of page myCounter of myDocument

				set page range of PDF export preferences to name of page myCounter of myDocument
				
				set myBarcodeKey to "Barcode"
				set theDocLinks to all graphics of myDocument
				
				repeat with i in theDocLinks
					set theLinksLayer to item layer of i
					if theLinksLayer contains myBarcodeKey then
						set barcodeLayer to layer myBarcodeKey of myDocument
						set barcodeList to all graphics of barcodeLayer
						repeat with z in barcodeList
							set thePage to parent page of z
							if thePage is myCounter then
								set myBarcode to "Yes"
							else
								set myBarcode to "No"
							end if
						end repeat
					end if
				end repeat
				end repeat
			end tell[/code]

Problem solved. I needed to use more references, and also add “of myDocument” after pointing to pages.

Here is the code that worked:

[code]if exists layer “Barcode” then
set myBarcodeKey to “Barcode”
set theDocLinks to all graphics of myDocument
repeat with i in theDocLinks
set theLinksLayer to name of item layer of i
if theLinksLayer contains myBarcodeKey then
set barcodeLayer to layer myBarcodeKey of myDocument
set barcodeList to all graphics of barcodeLayer as list
repeat with z in barcodeList

								if z's parent page is page myCounter of myDocument then
									set myLink to item link of z
									set myLinkName to name of myLink
									set myBarcode to "_" & characters 3 thru -5 of myLinkName as string
								else
									set myBarcode to "_NoUPC"
								end if
							end repeat
						end if
					end repeat
				else
					set myBarcode to ""
				end if[/code]

Hey mnight,

Please use the {AppleScript} button when posting AppleScript code.

It will be formatted and more readable.