Hi
I am trying to create an automator action to do a find and replace inside multiple word documents.
I had it working fine just using automator, but if the word doc was locked it created an error. I am therefore trying to do the find and replace part using applescript so I can enclose it is a ‘try’ section.
I have never used applescript before this is how far I have got but it is not working.
Also I am unsure how to get the applescript to work on all the word docs that are being passed to it. I have guessed at “repeat with i in input”
on run {input, parameters}
repeat with i in input
try
tell application "Microsoft Word"
set myResult to find object of i
tell myResult
execute find find text "X" replace with "Y" replace replace all
end tell
end tell
return i
end try
end repeat
return input
end run
I have not yet got around to having the word file save and close yet but that is how I would like it to end.
Can anyone help me finish this off?
Thanks
Hi,
I don’t know what class the input is of.
Assuming input is a list of alias specifiers try this
on run {input, parameters}
repeat with aFile in input
tell application "Microsoft Word"
try
open aFile
tell document 1
set myResult to find object of i
tell myResult
execute find find text "X" replace with "Y" replace replace all
end tell
end tell
close document 1 saving yes
on error
try
close document 1 saving no
end try
end try
end tell
end repeat
return input
end run
Thanks for the quick responce.
It’s not quite working,
I think this line was I is wrong
set myResult to find object of i
So I changed it to
set myResult to find object of aFile
As it is not working I took out the ‘try’ section and I am getting the error
The variable myResult is not defined.
sorry, the target object is the reference to the document object, not the file on disk,
as the document is already referenced by the tell document block try
set myResult to find object
or
set myResult to find object of text object
Wonderful thank you very much.
Here is the final working script if anyone else needs it. :
on run {input, parameters}
repeat with aFile in input
tell application "Microsoft Word"
try
open aFile
tell document 1
set myResult to find object of text object
tell myResult
execute find find text "X" replace with "Y" replace replace all
end tell
end tell
close document 1 saving yes
on error
try
close document 1 saving no
end try
end try
end tell
end repeat
return input
end run
In Automator I have two Actions:
1st “Ask for finder items”
2nd “Run Apple Script” - with the above script entered.
Thanks for the help. I think I may like to learn Applescript I know some python and it looks a little similar.
Cheers,
Dan