Find and select words that follow after a known word

This would be nice if this could be done.

I cannot figure out how to obtain the words that follow after a particular word that appears somewhere on an InDesign CS2 document. For example, the words that follow the word "expires " (not including the space). It gets so darn confusing with object references.

For example:

“This Advertisement Expires 00/00/00”

I am trying to find the “00/00/00” and then attach a variable to it.

That way, I could assign that value “??/??/??” to a variable which would become the new search criteria.

Any thoughts?

tell application "Adobe InDesign CS2"
	activate
	
	set foundExpire to {}
	set theExpire to "expires"
	tell document 1
		set searchResults to search every line of every story for theExpire
		set textFrameResults to count of text frames with theExpire
		repeat with X from 1 to count of searchResults
			repeat with Y from 1 to count of item X of searchResults
				set selection to item Y of item X of searchResults
				set theLine to line 1 of selection
				repeat with i in theLine
					--set ???????????
				end repeat
			end repeat
		end repeat
	end tell
end tell

I don’t know anything about your app, by why not applescript’s text item delimiters?

I may try that. But I won’t ever know the characters that follow the word “Expires”.

This is what I have so far, and it is getting close. Yet, I don’t think I will use the character list?

property charList : {"e", "x", "p", "i", "r", "e", "s", " "}

tell application "Adobe InDesign CS2"
	activate
	
	set foundExpire to {}
	set theExpire to "Expires"
	tell document 1
		set searchResults to search every line of every story for theExpire
		set textFrameResults to count of text frames with theExpire
		repeat with X from 1 to count of searchResults
			repeat with Y from 1 to count of item X of searchResults
				set selection to item Y of item X of searchResults
				set theLine to line 1 of selection
				set theSelection to text in theLine
				set objRef to object reference of every word of line 1 of selection
				repeat with wordCount in objRef
					set wordCount to count of words in line 1 of selection
					set expireWordCount to every word in line 1 of selection is equal to theExpire
					set dateRange to word 2 of line 1 of selection
				end repeat
			end repeat
		end repeat
	end tell
end tell

If you set the text item delimiters to expires, and then reassemble the first three words following it with “/” as the delimiter, you’ll get the date.

set tText to "This document expires 09/05/07 and will be superceded by bletch."
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "expires"
set Pieces to words 1 thru 3 of item 2 of text items of tText
set AppleScript's text item delimiters to "/"
set tDate to Pieces as string
set AppleScript's text item delimiters to tid
tDate --> "09/05/07"

Jeff,

Do you actually want to go about this by finding the word after your search terms, or is your goal just to find the string of numbers? If the latter, then you should try using the number wildcard. If you have a legitimate reason to do the former, then I’d obtain refs using a whose clause and repeat through that list to obtain “the word after” the current ref. I recommend reading the Indesign Scripting Guide on Adobe’s site to better understand it.

search for "^9^9/^9^9/^9^9"

It will always be a string of numbers, or else I will error out the script. But there are times when it may be 1/2/08 or 11/2/2008, etc. But as you can see, it will always be the word after Expires.

I played around prior to seeing these last two posts. I came up with this goofy but seemingly effective workaround. Does my logic make sense?

I would like to condense this if at all possible. I have a feeling I will be using text item delimitirs. Thank you both.

set exNum to 1

tell application "Adobe InDesign CS2"
	activate
	set foundExpire to {}
	set theExpire to "expires"
	tell document 1
		try
			set searchResults to search every story for theExpire
			set textFrameResults to count of text frames with theExpire
			repeat with X from 1 to count of searchResults
				repeat with Y from 1 to count of item X of searchResults
					try
						set selection to item Y of item X of searchResults
					end try
					try
						set end of foundExpire to text of selection
					end try
				end repeat
			end repeat
			set finalExpireCount to (count of foundExpire) as string
			---Above this line will end up with an "Epires" selected on the document
			
			set theSelection to selection
			set theLine to line 1 of selection
			set objRef to object reference of every word of line 1 of selection
			set expireWords to every word in line 1 of selection
			repeat with xExp in expireWords
				if xExp does not contain "Expires" then
					set exNum to exNum + 1
					if xExp contains "Expires" then exit repeat
				end if
			end repeat
			set theNumber to word exNum of line 1 of selection
			display dialog theNumber
		end try
	end tell
end tell

Hi jeffkr

Had a little play around with this one with some good results on my machine and indesign.
Can’t say if it will work for you.
Just had one doc with one text frame with a load of text in “expires 00/00/00” etc. plus other words.
so you would have to alter for multiple text frames, pages etc…
But like i say this worked for me!!

tell application "Adobe InDesign CS3"
	tell document 1
		tell text frame 1
			set b to count words
			repeat with i from 1 to b
				if "expires" is in word i then
					set y to i + 1
					set theexpdate to text returned of (display dialog (word y as string) default answer "??/??/??")
					set word y to theexpdate
				end if
			end repeat
		end tell
	end tell
end tell

Apologies in advance if its a load of tosh!!

Pidge,
Your solution was exactly what I was looking for”once again! THANK YOU. FWIW, I have it using the line rather than the text frame, since a selection will always be on the word “expires” before I run this script. No, I didn’t realize that before I posted this.

I don’t know why, but I think I had to change a line or two? e.g.,
if word i is equal to “expires” then

-Or am I just tired and stupid?

Funny thing is I thought my previous solution was working. Boy was I wrong!!! Here’s what I have that seems to work great.

tell application "Adobe InDesign CS2"
	tell document 1
		--tell text frame 1
		tell line 1 of selection
			set b to count words
			repeat with i from 1 to b
				try
					if word i is equal to "expires" then
						set Y to i + 1
						--set theexpdate to word Y as string
						--display dialog Y as string
						set theNumber to word Y
						--display dialog theNumber
					end if
				end try
			end repeat
		end tell
	end tell
end tell

Marc, (or anyone willing to help)
The wildcard search is more beneficial than I originally thought. I learned a bunch of good stuff from you all!!!

Here’s my present dilemma. Using two searches, I can obtain all 7 variations the dates may exist in a document. For example:

0/0/00

0/00/00

00/00/00

0/00/0000

00/00/0000

00/0/0000

0/0/0000

That’s fantastic, but somehow I need to figure out a way to assign a variable to the dates the script finds as it repeats through the search. This is probably done by a handler? I looked through my books but I don’t see where or how I could capture the dates (as a variable). Maybe I can log the results and attach a variable that way? There must be a simple solution I am overlooking?

tell application "Adobe InDesign CS2"
	activate
	tell document 1
		try
			set theExpire1 to "^9/^9/^9"
			set searchResults1 to search every story for theExpire1
			repeat with X from 1 to count of searchResults1
				repeat with Y from 1 to count of item X of searchResults1
					try
						set selection to item Y of item X of searchResults1
						set theSelDate1 to the word 1 of selection
						display dialog theSelDate1
					end try
				end repeat
			end repeat
		end try
		try
			set theExpire2 to "^9/^9^9/^9"
			set searchResults2 to search every story for theExpire2
			repeat with X from 1 to count of searchResults2
				repeat with Y from 1 to count of item X of searchResults2
					try
						set selection to item Y of item X of searchResults2
						set theSelDate2 to the word 1 of selection
						display dialog theSelDate2
					end try
				end repeat
			end repeat
		end try
	end tell
end tell




Something like this (I’m assuming you want AppleScript dates, and a few well placed tells would clean this up)

set tD to {"1/2/07", "1/12/06", "10/13/07", "3/14/2007", "11/22/2007", "12/3/2007", "4/5/2007"}
set newD to {}
repeat with D in tD
	set tid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to "/"
	set S to text items of contents of D
	if (count of item 1 of S) is 1 then set item 1 of S to "0" & item 1 of S
	if (count of item 2 of S) is 1 then set item 2 of S to "0" & item 2 of S
	if (count of item 3 of S) is 2 then set item 3 of S to "20" & item 3 of S
	set AppleScript's text item delimiters to tid
	set S to item 3 of S & item 1 of S & item 2 of S
	set end of newD to S as «class isot» as date
end repeat
newD --> {date "Wednesday, January 2, 2002 12:00:00 AM", date "Thursday, January 12, 2012 12:00:00 AM", date "Sunday, October 13, 2013 12:00:00 AM", date "Wednesday, March 14, 2007 12:00:00 AM", date "Thursday, November 22, 2007 12:00:00 AM", date "Monday, December 3, 2007 12:00:00 AM", date "Thursday, April 5, 2007 12:00:00 AM"}

[Corrected Third test to get it right.]

Hi Adam,
That’s not exactly what I was after, but it sure is a great thing to have and see how it works. So thank you very much. What I was trying to do, was find date that were in a document and just gather them in a list so I could compare them with another date later on. I should have explained myself better. An hour ago I didn’t know how to obtain a list. But I think this is what I need. - I was looking in the wrong section of the book :frowning:

tell application "Adobe InDesign CS2"
	activate
	set dateRanges to {}
	tell document 1
		try
			set theExpire1 to "^9/^9/^9"
			set searchResults1 to search every story for theExpire1
			repeat with X from 1 to count of searchResults1
				repeat with Y from 1 to count of item X of searchResults1
					try
						set selection to item Y of item X of searchResults1
						set theSelDate1 to the word 1 of selection
						set end of dateRanges to theSelDate1
						--display dialog theSelDate1
					end try
				end repeat
			end repeat
		end try
		try
			set theExpire2 to "^9/^9^9/^9"
			set searchResults2 to search every story for theExpire2
			repeat with X from 1 to count of searchResults2
				repeat with Y from 1 to count of item X of searchResults2
					try
						set selection to item Y of item X of searchResults2
						set theSelDate2 to the word 1 of selection as string
						set end of dateRanges to theSelDate2
						--display dialog theSelDate2
					end try
				end repeat
			end repeat
		end try
	end tell
end tell


set theResults to dateRanges as list