Indesign Find text string & display alert

I need to find a specific text string in the current active document & if it doesn’t exist then display an alert. The script below works in some documents but not all of them. Sometimes I get this error when I have more than one document open or when the file contains more than 1 page:
Can’t make {text from character 2 to character 11 of story id 274567 of document id 63 of application “Adobe InDesign CS6”, text from character 2 to character 11 of story id 274648 of document id 63 of application “Adobe InDesign CS6”} into type Unicode text.

What possible can be wrong? :confused:

set findThis to "XX28.18"
tell application "Adobe InDesign CS6"
	activate
	
	--clear fields ...
	set find text preferences to nothing
	
	-- set find options
	set case sensitive of find change text options to true
	set whole word of find change text options to true
	
	set include hidden layers of find change text options to false
	set include locked layers for find of find change text options to false
	set include locked stories for find of find change text options to false
	
	set find what of find text preferences to findThis -- load string into the 'Find'
	tell document 1
		set pageCode to find text findThis
		
		if pageCode is not in findThis then
			display alert "XX28.18 not found"
		end if	
		
	end tell
	set find text preferences to nothing
end tell

Model: iMac (Retina 5K, 27-inch, Late 2015)
AppleScript: 2.8.1 (183.1)
Browser: Safari 537.36
Operating System: Mac OS X (10.11.6)

Hi There,

The code below seems to work ok in InDesign CC 2015.

HTH


set findThis to "XX28.18"

tell application "Adobe InDesign CC 2015"
	
	activate
	
	--clear fields ...
	set find text preferences to nothing
	
	-- set find options
	set case sensitive of find change text options to true
	set whole word of find change text options to true
	
	set include hidden layers of find change text options to false
	set include locked layers for find of find change text options to false
	set include locked stories for find of find change text options to false
	
	set find what of find text preferences to findThis
	
	set findTextResult to find text front document
	
	if (count of findTextResult) is 0 then
		display alert "Nothing found!"
	else
		set alertText to ((count of findTextResult) & " instance(s) of " & findThis & " found.") as string
		display alert alertText
	end if
	
	set find text preferences to nothing
	
end tell

That is perfect :smiley: thanks. Can I ask why the command count is needed. I just need to find a text string & not count the times found in the document. I’m still learning

Hi. Using count is a valid solution but isn’t strictly necessary. The explanation for what went wrong is that find returns an object reference or group of same. When there is one, you can ask for equality to a comparator, and ID obliges by automatically coercing it to text; when there are multiple references, it doesn’t.

Ok thanks Marc & TecNik