Search code examples
ms-wordapplescript

MS word applescript find and select text in app (not find and replace)


I want to make a auto-cue / text follow in MS word with applescript. So i want the document to fysically go the the text what automatically is going in to the applescript. here is the script i'm working with now:

tell application "Microsoft Word"
            activate
            if not (exists active document) then
                display dialog "Er is geen actief document."
                return
            end if
            set myFind to find object of text object of active document
            set properties of myFind to {match case:false, match whole word:false, match wildcards:false, forward:true}
            
            set content of myFind to prefix & CueText & posix
            display dialog content of myFind as string
            set foundRange to execute find myFind
            if foundRange is not equal to missing value then
                select object of foundRange as string
            else
                display dialog "Tekst niet gevonden."
            end if
        end tell

but: find myFind returns "true" and not the position of the text i'm searching for

Also "select" doesn't work because of this but could not really find explanation in the documentation:

select object of foundRange

returns error "object of true kan niet worden opgevraagd. " number -1728 from object of true

(translation: The object of true cannot be retrieved.)

select object of foundRange as string

returns: error "object of true kan niet in type string worden omgezet." number -1700 from object of true to string'

(translation: The object of true cannot be converted to type string.)

but thats logical because its a true/false state. I'm only not sure how to solve to get the position of the found text....


Solution

  • the final script is actually pretty simple:

    tell application id "com.figure53.QLab.5" to tell front workspace
      set CueText to q number of last item of (selected as list) -- q name and q number are interchangeable
    
      set prefix to "L" -- if your script has que numbers with a prefix letter or symbol but your qlab cuenumbers don't you can manually add it here
    
      set match_case_sensitive to false -- case sensitive finds can be be usefull if cuenumbers in the script always have the same prefix
      set match_whole_word to true -- set to true makes it that if you look for "L2" you won't find "L24")
      set match_wildcards to false
      set find_format to true
    
    
      if CueText is "" then
        display dialog "Cue find text is empty"
      else
        tell application "Microsoft Word"
            set selFind to find object of selection
            clear formatting selFind
            set properties of selFind to {match case:match_case_sensitive, match whole word:match_whole_word, match wildcards:match_wildcards, format:find_format}
            set foundIt to execute find selFind find text prefix & CueText wrap find stop
        end tell
      end if 
    end tell
    

    So every time this script is triggerd it finds the text in the cue number from qlab. If there are multiple same finds it wil always find the next one from the selection marker in word.