Selecting text in word

I’m new to apple script… Please help before I burn down the building with my mac still inside.

I’ve downloaded all sorts of reference documentation… searched and searched. I give up.

Trying to do what I thought would be a very simple operation!

I get word documents with information in columns like this:

832222 6 6
832218 39 39
839998 44 44
839907 2 2
839919 4 4

the first number (example: 832222) is the identifier.
the second and third number (example: 6) is the relevant data.

The location of the data is slightly variable, so I can’t set a “text range” - or at least I don’t think I can.

My thought was to find the identifier with this code:



activate "microsoft word"

tell application "Microsoft Word"
	set selFind to find object of selection
	set forward of selFind to true
	set wrap of selFind to find stop
	set content of selFind to "832222"
	execute find selFind	
end tell

then somehow tell apple script to select and copy the entire line. I can’t seem to find a command for this.

A better solution would be to select “word 2” of the current line - as I don’t need to copy the identifier, just one of the numbers to the right but I can’t seem to get that to work.

After I get my selection the information will be pasted into excel. Then the script will repeat for the next identifier, ect…

Thanks for any help!

Please go easy on me with any answers… I know almost nothing about apple script.

Hi,

assuming that the identifier string occurs only once in the text
and the relevant data are two strings space separated in the same line as the identifier
I suggest to use text item delimiters.
The result are the variables d1 and d2 which contain the relevant data


set theID to text returned of (display dialog "Enter identifier" default answer "")
tell application "Microsoft Word" to set theContent to content of text object of active document
set {TID, text item delimiters} to {text item delimiters, theID}
try
	set theData to 1st paragraph of text item 2 of theContent
	set {d1, d2} to words 1 thru 2 of theData
on error e
	display dialog "no matching text found" buttons {"OK"} default button 1
end try
set text item delimiters to TID

Thanks StefanK!

Unfortunately it didn’t work!

However I did read up on Text item delimiters and I now have a script that grabs the correct number:


tell application "Microsoft Word" to set theContent to content of text object of active document

set theParagraph to paragraph 51 of theContent
set AppleScript's text item delimiters to " "
set theData to word 3 of theParagraph

tell application "Microsoft Excel" to activate
tell application "Microsoft Excel"
	tell worksheet "Sheet1" of active workbook
		set value of range "F14" of active sheet to theData
		
	end tell
end tell

Thank you very much for the help!!!

What did not work?
Did you get error messages?

I have a set of data that looks like this:

80528 FORT COLLINS, CO 374
80553 FORT COLLINS, CO 1
80631 GREELEY, CO 1638
80632 GREELEY, CO 10
80633 GREELEY, CO 10
80634 GREELEY, CO 1497
80638 GREELEY, CO 1
80639 GREELEY, CO 13
80901 COLORADO SPRINGS, CO 40

It is a rather long list.

I’m trying to capture only the last number by doing a search like StefanK suggested:


set theID to text returned of (display dialog "Enter identifier" default answer "")

An example of theID would be 80901

From here I am trying to search the word doc for theID - Then I need to set theContent to the line of theID so in the case of my example the line would be:

80901 COLORADO SPRINGS, CO 40

Then:


set AppleScript's text item delimiters to " "

And some type of command to select the last delimited item?

As you can see above, each line is a different length, so I can’t just say word 5 of theContent.

any thoughts?

One thought I had was to search for theID, then is there a way to find out what paragraph the cursor is on?

for example:

tell application “Microsoft Word” to set theContent to content of text object of active document
find theID ← not the right command
set theparagraph to active paragraph of the content ← is there a command like that?

This line of thought would at least get the right content selected. Then I could set text item delimiters, and if there is a way to count text items, I could use a variable that is set to the result of: “count text items”

Are any of these ideas practical?

I’m getting close!

I can almost pull the line out with this script:


property matchString : ""

tell application "Text Edit"
	display dialog "Extract (into a new document) all paragraphs containing what string?" default answer matchString
	set matchString to text returned of result
	set matches to every paragraph in text of front document where it contains matchString
	return matches
end tell

The trouble is that text edit is considering everything one big paragraph, unless I put a space between lines. Pretty annoying!! I can’t go through manually entering spaces, and I can’t write a script to do it because even though there are line breaks, either applescript of text edit is considering everything to be one long string.

I tried it in Microsoft Word. doesn’t work :frowning:

this gets the last word of the found paragraph


set theID to text returned of (display dialog "Enter identifier" default answer "")
tell application "Microsoft Word" to set theContent to content of text object of active document
set {TID, text item delimiters} to {text item delimiters, theID}
try
	set theData to 1st paragraph of text item 2 of theContent
	set text item delimiters to space
	set theNumber to last text item of theData
on error e
	display dialog "no matching text found" buttons {"OK"} default button 1
end try
set text item delimiters to TID
display dialog "found paragraph: " & theData & return & "last item: " & theNumber as text