change part of file name & create pdf for each version

The following script will activate/deactivate layers based on the letter {A,E,D, F} chosen from the list.
The problem I have I don’t know how to rename the pdf based on the letter chosen from the list.
If file name is ABC01_A100.indd & the letter chosen from the list is E then final pdf should be ABC01E100.pdf. Basically what is changing whatever is between the underscores ?
If the letter chosen from the list is D then final pdf is ABC
D_100.pdf. I can see the results on the events window. How can I capture the results of whatever is chosen from list? I try return statement but no success at all.
Text item delimiters have been always a very difficult concept for me to grasp. I always try to understand it but as simple as that I can’t.

¢Then if i use choose from list with multiple selection allowed in which I can select more letters: A", “E”, “D”, “F” how can I output different PDFs with whatever is chosen from the list (with the correct layers activated/deactivated) & file names; ABC01_E100.pdf, ABC01F100.pdf, ABC01F_100.pdf. I know it is not difficult but after spending 4 days trying to figure it out by myself reading, researching I have to throw the towel & ask for help at this forum.

property myPDFpreset : missing value
set myPDFpreset to "PressQuality"


tell application "Adobe InDesign CS4"
	if not (exists document 1) then
		display alert "Please open an InDesign document"
		error number -128
	end if
end tell


set versionLayers to {"A", "E", "D", "F"}
set the_choice to (choose from list versionLayers with prompt "What version do you need?" without multiple selections allowed) as text
set chosenVersionLayer to item 1 of the_choice


-->Activate versions Layers
if the_choice is false then error number -128
if the_choice is "A" then
	myAB()
else if the_choice is "E" then
	myE()
else if the_choice is "D" then
	myD()
else if the_choice is "F" then
	myF()
	
end if


on myA()
	tell application "Adobe InDesign CS4"
		--set myDocument to active document
		tell active document
			--Activate Layers
			set properties of layer "Tive" to {visible:true}
			set properties of layer "Logo" to {visible:true}
			set properties of layer "Search" to {visible:true}
			set properties of layer "Z1" to {visible:true}
			--Hide Layers
			set properties of layer "Z2" to {visible:false}
			set properties of layer "Z3" to {visible:false}
			set properties of layer "GS" to {visible:false}
			set properties of layer "BD" to {visible:false}
		end tell
		display alert "Version AB activated" giving up after 6
	end tell
end myA

on myE()
	tell application "Adobe InDesign CS4"
		--set myDocument to active document
		tell active document
			--Activate Layers
			set properties of layer "Tive" to {visible:true}
			set properties of layer "Logo" to {visible:true}
			set properties of layer "Search" to {visible:true}
			set properties of layer "Z2" to {visible:true}
			--Hide Layers
			set properties of layer "Z1" to {visible:false}
			set properties of layer "Z3" to {visible:false}
			set properties of layer "GS" to {visible:false}
			set properties of layer "BD" to {visible:false}
		end tell
		display alert "Version E activated" giving up after 6
		
	end tell
end myE

on myD()
	tell application "Adobe InDesign CS4"
		--set myDocument to active document
		tell active document
			--Activate Layers
			set properties of layer "Tive" to {visible:true}
			set properties of layer "Logo" to {visible:true}
			set properties of layer "Z3" to {visible:true}
			--Hide Layers
			set properties of layer "Z1" to {visible:false}
			set properties of layer "Z2" to {visible:false}
			set properties of layer "GS" to {visible:false}
			set properties of layer "BD" to {visible:false}
			set properties of layer "Search" to {visible:false}
			
		end tell
		display alert "Version D activated" giving up after 6
		
	end tell
end myD


on myF()
	tell application "Adobe InDesign CS4"
		--set myDocument to active document
		tell active document
			--Activate Layers
			set properties of layer "Tive" to {visible:true}
			set properties of layer "Logo" to {visible:true}
			set properties of layer "Z3" to {visible:true}
			set properties of layer "GS" to {visible:true}
			--Hide Layers
			set properties of layer "Z1" to {visible:false}
			set properties of layer "Z2" to {visible:false}
			set properties of layer "BD" to {visible:false}
			set properties of layer "Search" to {visible:false}
		end tell
		display alert "Version F activated" giving up after 6
		
	end tell
end myF


---======================

tell application "Adobe InDesign CS4"
	activate
	
	set source_folder to file path of active document --Put the PDFs same folder as ID file
	
	set theName to name of active document
	set text item delimiters of AppleScript to {"_"}
	set theShortName to text 1 thru text item -2 of theName
	--text 1 thru text item -2 is every character from the first character to the second to last text item
	
	
	set text item delimiters of AppleScript to ""
	
	tell application "Finder"
		if (exists folder "PDFs" of folder source_folder) is false then
			make folder at source_folder with properties {name:"PDFs"}
		end if
	end tell
	
	repeat with x from 1 to count pages of active document
		set thePageName to name of page x of active document
		set page range of PDF export preferences to thePageName
		
		--section-page number M-U-S-T have 3 digits
		set threeDigitPageName to text -3 thru -1 of ("00" & thePageName)
		(* text 1 thru 3 are the first 3 characters 
					text -3 thru -1 are the last 3 characters*)
		set theFilePath to source_folder & "PDFs:" & theShortName & "_" & threeDigitPageName & ".pdf" as string
		----Export PDF as single pages
		tell active document
			export format PDF type to theFilePath using myPDFpreset without showing options
		end tell
	end repeat
	
end tell

set versionLayers to {"A", "E", "D", "F"}
set the_choice to (choose from list versionLayers with prompt "What version do you need?" with multiple selections allowed) as text
set chosenVersionLayer to item 1 of the_choice
--export PDFS with items selected from list

Hi there,
Hopefully the example below will help you understand text item delimiters a little better. I’ve expanded things to hopefully make things a bit clearer.

set versionLayers to {"A", "E", "D", "F"}
set the_choice to (choose from list versionLayers with prompt "What version do you need?" with multiple selections allowed) as text

set theFilename to "ABC01_A_100.indd" as text -- Sample filename

set {atid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "."}

set theFilenameWithoutINDD to text item 1 of theFilename

set AppleScript's text item delimiters to "_"

set part1 to text item 1 of theFilenameWithoutINDD
set part2 to text item 3 of theFilenameWithoutINDD

set AppleScript's text item delimiters to atid

set thePdfName to part1 & "_" & the_choice & "_" & part2 & ".pdf"

display dialog "The new filename is: " & thePdfName

Whenever I have anything to do with text item delimiters I specify them as below. Stefan showed me this method and I’ve used it ever since.

set {atid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "the_delimiter_required"}  -- sets the delimiter required and also remembers the current one

-- manipulate your data/text here...

set AppleScript's text item delimiters to atid -- sets the delimiter back to what it was

As for the multiple files query, you simply need to loop through the process you have for the versions returned from the list selection. You’re pretty much there.

Thank you TecNik :D:D
That really help. I THINK I UNDERSTAND IT THIS TIME. I have a question:

	set AppleScript's text item delimiters to atid

and

	set text item delimiters of AppleScript to ""

are the same?

Hi nellbern,

Glad to hear your understanding of the text item delimiters thing is clearer.

As for the question regarding the lines being the same, there is a slight difference.

I’m sure someone will correct me if wrong, but as I understand it, set AppleScript’s text item delimiters to atid sets the text item delimiters to the one you had specified prior to changing it.
This line set {atid, AppleScript’s text item delimiters} to {AppleScript’s text item delimiters, “.”} remembers the current text item delimiter and sets the new one “.” .

The line set text item delimiters of AppleScript to “” sets the text item delimiters back to the default delimiter.

HTH

Wonderful explanation. I’m going to keep asking you because I want to learn.

I just realized in this line:

if the_choice is false then error number -128

It doesn’t Cancel or Stop the script. Just continuos w the export & the resulting pdf have the name
ABC01_false_100.pdf.

choose from list returns either a list of chosen items even without multiple selections or false if the user presses Cancel

change these three lines


.
set the_choice to (choose from list versionLayers with prompt "What version do you need?"
if the_choice is false then error number -128



-->Activate versions Layers
set the_choice to item 1 of the_choice
.

Stefan,

set the_choice to item 1 of the_choice

choose from list returns a list of chosen items or false. I don’t understand how “item 1 of the_choice” will stop/cancel from continuing. What is “item 1” in this case.

this expression flattens the list.
When the user doesn’t select anything and presses Cancel, choose from list returns boolean false,
otherwise returns always a list containing one item (assuming multiple selections is also false)

Now with that comes the question:

set the_choice to (choose from list versionLayers with prompt "What versions do you need?" with multiple selections allowed)

I want multiple selections for example. I choose A,E,D
returns → {“A”, “E”, “D”}

Currently it creates a pdf of the 1st selected item on list instead of all selected. How do I trap that result {A,E,F} and activate-deactivate & export 3 different pdfs. I’m trying to loop thru the list. I tried this:

set versionLayers to {"AB", "E", "D", "F"}
set the_choice to (choose from list versionLayers with prompt "What version do you need?" with multiple selections allowed)
if the_choice is false then error number -128


-->Activate versions Layers
set the_choice to item 1 of the_choice

repeat with i in the_choice
if the_choice is "AB" then
	myAB()
else if the_choice is "E" then
	myE()
else if the_choice is "D" then
	myD()
else if the_choice is "F" then
	myF()
end if
end repeat

cut the line to flatten the list and use the index variable in the loop


set versionLayers to {"AB", "E", "D", "F"}
set the_choice to (choose from list versionLayers with prompt "What version do you need?" with multiple selections allowed)
if the_choice is false then error number -128

-->Activate versions Layers

repeat with aChoice in the_choice
	if aChoice is "AB" then
		myAB()
	else if aChoice is "E" then
		myE()
	else if aChoice is "D" then
		myD()
	else if aChoice is "F" then
		myF()
	end if
end repeat


I tried:

set versionLayers to {"AB", "E", "D", "F"}
set the_choice to (choose from list versionLayers with prompt "What version do you need?" with multiple selections allowed)
if the_choice is false then error number -128

-->Activate versions Layers
repeat with aChoice in the_choice
   if aChoice is "AB" then
       myAB()
   else if aChoice is "E" then
       myE()
   else if aChoice is "D" then
       myD()
   else if aChoice is "F" then
       myF()
   end if
end repeat

but I’m getting 1 pdf ( ABC01_AED_100.pd) with whatever I selected. instead of ABC01_A_100.pdf, ABC01_E_100.pdf, ABC01_D_100.pdf

that’s the dereferencing trap, add contents of to all appropriate lines


.
if contents of aChoice is "AB" then
.