Find and Show text

Discuss AppleScripting for QuarkXPress 10, 9 & 8 (and before)

Find and Show text

Postby ssavarese » 16 Sep 2014, 08:09

Hiya! With the help of various posts on the forum I was able to come up with a working script for my needs, but I'm having some speed issues I was hoping someone could help me with.

Im trying to give an operator a means to find specific text in a quark file, then "show" it. Not replace it. But "show" it, prompt a dialog that says "look here this text was found on this layer" and then move on. I threw in some counts and logging, but in essence what I really need is the operator to be aware of certain text strings that may appear in the document.

Below is my code that is working. However, on very large documents it takes and exceedingly long time. 10-15 minutes in some cases. I'm thinking there must be a better way. Can someone please take a look?

Code: Select all
findText({}, {})

on findText(pageList, textboxList)
   set totalWarnings to 0 as number
   tell application "QuarkXPress"
      
      tell document 1
         set documentName to name as text
         
         if pageList = {} then
            set pageList to index of every page
            if class of pageList ≠ list then set pageList to {pageList}
         end if
         set pageCount to count of pageList
         repeat with p from 1 to pageCount
            tell page (item p of pageList)
               if textboxList = {} then
                  set textboxList to index of every text box
                  if class of textboxList ≠ list then set textboxList to {textboxList}
               end if
               set textboxCount to count of textboxList
               repeat with t from 1 to textboxCount
                  try
                     set theString to text of text box (item t of textboxList) as string
                     
                     set lName to layername of text box (item t of textboxList) as text
                     
                     --only give warnings for visible layers
                     if layer lName is visible then
                        
                        set totalWarnings to my stringComparison(theString, lName, text box (item t of textboxList), totalWarnings, documentName)
                        
                        
                     end if
                  end try
                  
               end repeat
               
            end tell
         end repeat
      end tell
   end tell
   
   display dialog "Finished checking document with " & totalWarnings & " warnings." with title "Summary" with icon caution
   
end findText

on stringComparison(theString, lName, textBox, totalWarnings, documentName)
   
   set logFileName to documentName & ".tab"
   
   if theString contains "Text1" then
      tell application "QuarkXPress"
         show textBox
      end tell
      beep
      display dialog theString & " is found on layer " & lName with title "Warning" with icon stop
      set totalWarnings to totalWarnings + 1
      my logFile(theString, lName, logFileName)
   end if
   
   if theString contains "Text2" then
      tell application "QuarkXPress"
         show textBox
      end tell
      beep
      display dialog theString & " is found on layer " & lName with title "Warning" with icon stop
      set totalWarnings to totalWarnings + 1
      logFile(theString, lName, logFileName)
   end if
   
   if theString contains "Text3" then
      tell application "QuarkXPress"
         show textBox
      end tell
      beep
      display dialog theString & " is found on layer " & lName with title "Warning" with icon stop
      set totalWarnings to totalWarnings + 1
      logFile(theString, lName, logFileName)
   end if
   
   return totalWarnings
   
end stringComparison

on logFile(theString, lName, logFileName)
   set cDate to ((current date) as string)
   
   try
      tell application "Finder"
         set myFilePath to "DOCUMENTS:Logs"
         if not (file (myFilePath & ":" & logFileName) exists) then
            set theLogFile to make file at folder (myFilePath as text)
            set the name of theLogFile to logFileName
            set myFile to open for access file (myFilePath & ":" & logFileName as text) with write permission
         else
            set myFile to open for access file (myFilePath & ":" & logFileName as text) with write permission
         end if
         
         set endOfFile to get eof myFile
         
         set theStringWords to words of theString
         set AppleScript's text item delimiters to space
         set theStringWordsSpaced to theStringWords as string
         set AppleScript's text item delimiters to ""
         set theFieldString to "Warnings" & tab & cDate & tab & theStringWordsSpaced & tab & lName
         
         write (theFieldString & return) to myFile starting at (endOfFile + 1)
         close access myFile
      end tell
   on error err
      display dialog "log file error" & err giving up after 5
      try
         close access (myFilePath & ":" & logFileName as text)
      end try
   end try
end logFile


Thank you!
-S
ssavarese
 
Posts: 6
Joined: 10 Sep 2014, 10:02

Return to QuarkXPress 8, 9 & 10: AppleScript

Who is online

Users browsing this forum: No registered users and 1 guest

cron