This script is so close to perfection but I am baffled by the repeat loop in the center. Here’s what it does and doesn’t do.
The script will search for a date range entered by the User. It works great if it only has to find one instance of the date range in a single text frame (story).
The problem comes when there are multiple instances found in one text frame, i.e.,
10/10/08
¨10/10/08
The above throws the tagging off (It doesn’t tag each range perfectly). I know it is based on the repeat loop in my script, but I don’t have any clue how to fix it?
Is there anybody who could explain the repeat mechanism in this instance, and why it doesn’t work. I know I have been annoying lately, sorry I am a very sloooow learner
-Jeff
(* clear InDesign's find/change preferences *)
tell application "Adobe InDesign CS2"
set find preferences to nothing
set change preferences to nothing
end tell
tell application "SystemUIServer"
activate
set myDate to text returned of (display dialog "Enter Expiration date" default answer "??/??/??")
end tell
tell application "Adobe InDesign CS2"
activate
set foundText to {}
tell document 1
try
set searchResults to search every word of every story for myDate
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 selection
tell XML element 1
set t to make XML element with properties {markup tag:"expirationdate"}
end tell
tell selection to markup using t
delay 2
end try
end repeat
end repeat
set x to (count of foundText) as string
display dialog "Found " & x & " expiration dates equal to " & myDate
end try
end tell
end tell
if (count of foundText) is 0 then
display dialog "That date was not found" buttons {"Cancel"} with icon stop
end if
Marc,
I appreciate it. I will look into this and hopefully you are right. In the meantime I decided to warn an operator if multiple items were found in the same story that satisfied the search results. I did this by checked the XML markup tags. The number of expiration dates should technically never be more than one. But it would be nice to figure out how to satisfy for more than one down the road.
Just for laughs, I am posting my entire code. It is a mess and I will be the first to admit I am the worst at Applescript. But believe it or not this does what I want it to. This took me a long time
I will eventually clean it up, since there is a ton of redundant code within.
Thanks again for looking and helping me out. I need all the help I can get.
-Jeff
(* This removes just the expiration and APNBox tags *)
on ExpDateTags()
tell application "Adobe InDesign CS2"
tell active document
try
set theRoot to (item 1 of XML elements) -- the root element
set theElements to every XML element of theRoot
repeat with x in theElements
--this will untag any main tags not equal to , idLine, or tagline
if (name of markup tag of x is not "idline") and (name of markup tag of x is not "tagline") then tell x to untag
try
set y to every XML element of x
if (name of markup tag of x is equal to "aPNBox") then set theSubElements to every XML element of x
repeat with y in theSubElements
if (name of markup tag of y is not equal to "expirationdate") then untag y
end repeat
end try
end repeat
end try
end tell
end tell
end ExpDateTags
(* This checks for expiration date tags *)
on myLoopLoop(myElement)
tell application "Adobe InDesign CS2"
tell active document
if (name of markup tag of myElement is "expirationdate") then
set userResponse to display dialog "There are already expiration date tags. Do you want to remove all existing tags and tag with this new date?" buttons {"OK", "Cancel"} default button {"OK"} with icon 2
if button returned of userResponse is "OK" then
my ExpDateTags()
else
if button returned of userResponse is "Cancel" then return
end if
end if
try
set moreElement to every XML element of myElement
repeat with x from 1 to (count of moreElement)
tell me to myLoopLoop(item x of moreElement)
end repeat
end try
end tell
end tell
end myLoopLoop
(* removes unnecessary tags *)
tell application "Adobe InDesign CS2"
tell active document
try
set theRoot to (item 1 of XML elements) -- the root element
set theElements to every XML element of theRoot
repeat with x in theElements
--this will untag any main tags not equal to aPNBox, idLine, or tagline
if (name of markup tag of x is not "aPNBox") and (name of markup tag of x is not "idline") and (name of markup tag of x is not "tagline") and (name of markup tag of x is not "expirationdate") then tell x to untag
--this will delete any href tags
try
set z to (every XML attribute of x whose name is equal to "href")
delete z
end try
--This will untag any subtags of aPNBox that are not equal to expirationdate.
try
set y to every XML element of x
if (name of markup tag of x is equal to "aPNBox") then set theSubElements to every XML element of x
repeat with y in theSubElements
if (name of markup tag of y is not equal to "expirationdate") then untag y
end repeat
end try
--This will untag any primary tags that are equal to aPNBox and have a subtag whose name is not "expirationdate" For example main tag equals aPNBox, and subtag equals aPNBox
try
set y to every XML element of x
if (name of markup tag of x is equal to "aPNBox") then set theSubElements to every XML element of x
repeat with y in theSubElements
end repeat
if (name of markup tag of y is equal to "expirationdate") then set theSubElements to every XML element of x
on error
try
if (name of markup tag of x is not "idline") and (name of markup tag of x is not "tagline") and (name of markup tag of x is not "expirationdate") then tell x to untag
end try
end try
end repeat
end try
try
set theElements to every XML element of theRoot
repeat with x in theElements
if (parent story of x is equal to nothing) then tell x to untag
end repeat
end try
--Delete unused tags
delete unused tags
--Pause for a second to help refresh page, and then show finished dialog
delay 1
--this will warn of any main tags set to expirationdate. It repeat again through the tags
try
set theRoot to (item 1 of XML elements) -- the root element
set theElements to every XML element of theRoot
repeat with x in theElements
end repeat
if (name of markup tag of x is "expirationdate") then
set userResponse to display dialog "WARNING !!!
You have a frame (or frames) that is tagged expirationdate but lacks an aPNBox master tag.
Find and Fix!
" buttons {"OK"} default button {"OK"} with icon 2
if button returned of userResponse is "OK" then tell application "Adobe InDesign CS2" to activate
end if
end try
---Make expirationdate tag in case it was deleted
tell application "Adobe InDesign CS2"
tell document 1
try
make XML tag with properties {name:"expirationdate"}
end try
end tell
end tell
end tell
end tell
myLoopLoop(theRoot)
(* clear InDesign's find/change preferences *)
tell application "Adobe InDesign CS2"
set find preferences to nothing
set change preferences to nothing
end tell
tell application "SystemUIServer"
activate
set {tButton, myDate} to {button returned, text returned} of (display dialog "Enter the Expiration Date" default answer "??/??/??" buttons {"Cancel", "It's on the Clipboard", "Enter"})
if tButton contains "Clipboard" then set myDate to the clipboard
end tell
tell application "Adobe InDesign CS2"
activate
set foundText to {}
tell document 1
try
set searchResults to search every story for myDate
set textFrameResults to count of text frames with myDate
--display dialog textFrameResults
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
tell XML element 1
set t to make XML element with properties {markup tag:"expirationdate"}
end tell
tell selection to markup using t
end try
end repeat
end repeat
try
set finalCount to (count of foundText) as string
end try
tell application "Adobe InDesign CS2"
tell active document
try
set theRoot to (item 1 of XML elements) -- the root element
set theElements to every XML element of theRoot
--repeat with x in theElements
set numberAPNBox to count of (every XML element of theRoot whose name of markup tag is equal to "aPNBox")
--display dialog numberAPNBox
set theAPNBoxes to every XML element of theRoot whose name of markup tag is equal to "aPNBox"
repeat with z in theAPNBoxes
set ExpDates to count of (every XML element of z whose name of markup tag is equal to "expirationdate")
if ExpDates is greater than or equal to 2 then
my ExpDateTags()
tell application "SystemUIServer"
activate
display dialog "One of the text frames had multiple expiration dates. You will have to tag all the expiration dates manually." buttons {"Cancel"} default button "Cancel" with icon stop
end tell
else
if ExpDates is less than 2 then display dialog finalCount & " expiration dates of " & myDate & " were tagged." buttons {"OK"} default button "OK"
exit repeat
end if
--display dialog ExpDates
end repeat
end try
end tell
end tell
(* End checking for multiple Expiration Dates for one aPNBox *)
end try
end tell
end tell
if (count of foundText) is 0 then
tell application "SystemUIServer"
activate
display dialog "That date was not found" buttons {"Cancel"} default button "Cancel" with icon stop
end tell
end if