Another question. I’m trying to locate a number in a document. The format is always 1.xxxxxx.xxx. I’ve managed to ‘find’ this by entering “1.^9^9^9^9^9^9.^9^9^9” into the find menu. Now I’d like to automate that- select the text found and copy it to clipboard. (I don’t want to actually find-and-replace, just find and copy the number, later I’ll rename the document with that number (I can figure that out)).
I’ve been reading the CS3 scripting guide, per Stephan K’s advice, and it appears that CS3 has a much-improved search function, via grep. Unfortunately, our business never gets software upgrades until a year after release or so. We have the InDesign Applescript book on order. Hopefully that wll have some answers… meanwhile-
Below is some mangled code which doesn’t compile.
tell application "Adobe InDesign CS2"
open added_item
set item_code to "1.^9^9^9^9^9^9.^9^9^9"
set theList to every page item of document 1
set c to count theList
repeat with i from 1 to c
set selection to page item i
try
find item_code
copy result
end try
end repeat
set item_code to result
tell application "Adobe InDesign CS2"
set foundText to {}
tell document 1
set searchResults to search text frames for "1.^9^9^9^9^9^9.^9^9^9"
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 foundText to text of selection
end try
end repeat
end repeat
end tell
end tell
if (count of foundText) is 0 then
display dialog "A code was not found in the current document (1.xxxxxx.xxx)" buttons {"Cancel"} with icon stop
else
if (count of foundText) is greater than 1 then
set dialogList to ""
set userChoice to choose from list foundText with prompt "More than one code was found" & return & "please choose one from list"
set the clipboard to userChoice as string
else
set the clipboard to item 1 of foundText as string
display dialog "The code: " & foundText & " has been copied to the clipboard" buttons {"OK"} with icon 1
end if
end if
the find and replace function was working fine in the script I had rolled out to my group until today. Now I’m suddenly getting an error “Adobe InDesign CS2 got an error: Object contains no text for find/change.” It selects
search text frames for "1.^9^9^9^9^9^9.^9^9^9"
from the code:
ell application "Adobe InDesign CS2"
set foundText to {}
tell document 1
set searchResults to search text frames for "1.^9^9^9^9^9^9.^9^9^9"
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 foundText to text of selection
end try
end repeat
end repeat
end tell
end tell
I haven’t changed anything on my computer, and it’s happening across multiple machines.
I’m not seeing the error, but I don’t have the documents that you are working on. The first thing that I would do is look what line of code that the scriot is stopping at and also look at the documents that are giving you the errors and seeing if there is something that is different about those than the ones that it was working on. My guess is that there is something in the document that is causing the problem and you need to add in some error checking into the code to take care of the problem. This really is the part of writing a script that can take the longest, getting it to work out in the “real world” with anyone’s set-up and way’s of working.
I jJUST found an alternate set of code that does work, but I’m baffled and troubled that code would stop working in the middle of the day. I’ve already controlled for 1) document, 2) individual computer. I’m hoping to automate a good deal of the legwork here in the department and it would be very troubling to have things break in the middle of the workday. I am quite prepared to do significant rewriting with system and program upgrades.
tell application "Adobe InDesign CS2"
set foundText to {}
set thedoc to active document
set searchResults to search thedoc for "1.^9^9^9^9^9^9.^9^9^9"