How do I compare a list that might contain "duplicates" in other list?

Hello all:
Probably asking incorrectly but any help is appreciated.

Trying to export PDF pages from an InDesign document that has several layers. The issue is I have a static list of needed “Versions” but those letters or group of letters “repeat” within the Layer names. I am trying to turn on all layers that have a certain letter or letter group within them. I have a static version named AB form my know list but I have a unknown layers that contain ABA and ABQ and another that contains AB. How do I select the AB layer only but not the ABA or the ABQ? I am using a “if xxxx contains” instead “if xxxx is” because the layer name may be multiple different sets of letters.

I know how to get the PDF to create just having a problem with the lists.

The script so far:

use AppleScript version “2.4” – Yosemite (10.10) or later
use scripting additions
set myVersionList to {“LC”, “LR”, “BW”, “AC”, “AR”, “ABA”, “SE”, “AB”, “BR”, “S”, “W”, “SY”, “V”, “Q”, “G”, “C”, “MO”, “SA”, “NM”, “AG”, “ABQ”, “DFX”}
set myLayerList to {}
tell application “Adobe InDesign CC 2018”
set user interaction level of script preferences to never interact
set myDoc to active document
set myCommonLayer to “WTX Common All”
set myDefaultLayer to “Layer 1”
tell myDoc
set visible of layers to false
set visible of layer myDefaultLayer to false
–delete layer myDefaultLayer
set visible of layer myCommonLayer to true
set mylayerCount to count of layers
repeat with i from 1 to mylayerCount
set theLayerName to name of layer i
if theLayerName is not “” then
copy theLayerName to end of myLayerList
end if
repeat with a from 1 to length of myLayerList
set theCurrentlayer to item a of myLayerList
repeat with x from 1 to length of myVersionList
set myCurrentVersion to item x of myVersionList
if theCurrentlayer contains myCurrentVersion then
set visible of layer theCurrentlayer to true
end if
end repeat
end repeat
end repeat
end tell
end tell

My layers within InDesign:
-NM,AG,ABQ
-BW,BR,W,SY,V,Q,G
-LC,LR,AC,AR,ABA,SE,AB,S,C,MO,SA
-BW,ABA,AB,BR,S,W,SY,V,Q,G,MO,SA,DFX
-LC,LR,AC,AR,SE,C,NM,AG,ABQ
-WTX Common All
-DFX
-SA,ABQ
-LC,LR,BW,AC,AR,ABA,SE,AB,BR,S,W,SY,V,Q,G,C,MO,NM,AG

Thank you very much for your help.

Model: MacBook Pro
AppleScript: 2.4
Browser: Safari 537.36
Operating System: macOS 10.12

Hi. Welcome to MacScripter.

Sorry you haven’t had a reply before now. I don’t have InDesign, but the easy AppleScript way to check if a name is in a list is either …

if (theLayerName is in myVersionList) then

… or …

if (myVersionList contains theLayerName) then

Thanks!! I will give that a try!!

First, a hint on getting help here: Indesign is completely irrelevant to your question - you’re trying to match terms, the question doesn’t actually relate to Indesign, you just happen to be using it. The first time I saw your post I read to “Trying to export PDF pages from an InDesign document,” thought “well, I don’t use Indesign,” and moved on to the next post. It doesn’t hurt to mention exactly what you’re trying to do - the whole Indesign layers thing - later on in the post. But I’d start with the problem as pure Applescript problem, which it is.

I’m not 100% clear what you’re trying to do, but I think it’s this:

Indesign reports the layer name as a string, and those strings look like these:

You have a list of “versions,” these:

and need to know whether or not certain versions appear in a layer name string.

Neither “contains” nor “is” is working for you to perform the comparisons.

Example:
You have two layers, names are
Layer1: “SA,ABQ”
Layer2: “SA, AB”
Version you want to know if it contains: “AB”

If you use “is” to compare, it compares the whole string, so then both will return false, while Layer2 should return true because it contains “AB”.

If you use “contains,” then both return “True,” where Layer1 should return false because it does not contain the code “AB,” it is just finding that as a subset of “ABQ.”

If I have correctly summarized the issue, the solution is to convert the comma delimited text that is returned for the layer names into Applescript lists of the elements, at which point you can easily perform exact matches on each item in the list.


use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

set theLayerNames to {"SA, ABQ", "AS, AB"}
set findVersion to "AB"

-- remove spaces and convert to text to list
set layerNamesLists to {}
repeat with aLayerName in theLayerNames
	copy text_to_list(replace_chars(aLayerName, " ", ""), ",") to the end of layerNamesLists
end repeat

--this repeat can just set your Indesign layer visibility, but I didn't do it that way because I don't have Indesign and wanted to be able to compile my code to test.
set theResults to {}
repeat with aNameList in layerNamesLists
	if aNameList contains findVersion then
		set theResults to theResults & true
	else
		set theResults to theResults & false
	end if
end repeat


on text_to_list(theText, theDelimiter)
	set {delimitHolder, AppleScript's text item delimiters} to {AppleScript's text item delimiters, theDelimiter}
	set theList to the text items of theText
	set AppleScript's text item delimiters to delimitHolder
	return theList
end text_to_list

on replace_chars(this_text, search_string, replacement_string)
	set AppleScript's text item delimiters to the search_string
	set the item_list to text items of this_text
	set AppleScript's text item delimiters to the replacement_string
	set this_text to the item_list as text
	set AppleScript's text item delimiters to ""
	return this_text
end replace_chars

I’m making some assumptions as to the core issue. If this isn’t what you’re trying to do, some clarification is needed:

set mergedList to {}
set traversalPast to {}

set list1 to {"NM", "AG", "ABQ"}
set list2 to {"LC", "LR", "AC", "AR", "SE", "C", "NM", "AG", "ABQ"}

set megaList to list1 & list2 --& list3, etc.
set mergedList's end to megaList's item 1
set traversalPast's end to megaList's item 1

repeat with this in megaList
	if this's contents is not in traversalPast then set mergedList's end to this's contents
	set traversalPast's end to this's contents
end repeat

log mergedList -->{"NM", "AG", "ABQ", "LC", "LR", "AC", "AR", "SE", "C"}

tell application "Adobe InDesign CS3"'s document 1
	set layers's visible to false
	repeat with this in mergedList --assumes each item actually exists
		tell ((layers whose name is this)'s item 1)
			set visible to true
			-- insert additional commands for the variable this's object reference
		end tell
	end repeat
end tell