Thursday, December 14, 2017

#1 2015-04-30 07:46:48 am

heaford
Member
Registered: 2015-04-30
Posts: 6

Automator and AppleScript

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"

Applescript:


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

Offline

 

#2 2015-04-30 08:02:16 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11482
Website

Re: Automator and AppleScript

Hi,

I don't know what class the input is of.
Assuming input is a list of alias specifiers try this

Applescript:


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


regards

Stefan

Offline

 

#3 2015-04-30 08:22:58 am

heaford
Member
Registered: 2015-04-30
Posts: 6

Re: Automator and AppleScript

Thanks for the quick responce.

It's not quite working,

I think this line was I is wrong

Applescript:

set myResult to find object of i

So I changed it to

Applescript:

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.

Offline

 

#4 2015-04-30 08:31:13 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11482
Website

Re: Automator and AppleScript

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

Applescript:

set myResult to find object

or

Applescript:

set myResult to find object of text object

Last edited by StefanK (2015-04-30 08:33:20 am)


regards

Stefan

Offline

 

#5 2015-04-30 09:48:43 am

heaford
Member
Registered: 2015-04-30
Posts: 6

Re: Automator and AppleScript

Wonderful thank you very much.

Here is the final working script if anyone else needs it. :

Applescript:

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

Last edited by heaford (2015-04-30 09:49:06 am)

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)