Re: Passing String Variables to Visual Basic within Applescript???
Re: Passing String Variables to Visual Basic within Applescript???
- Subject: Re: Passing String Variables to Visual Basic within Applescript???
- From: Ian MacLachlan <email@hidden>
- Date: Mon, 25 Jun 2001 21:56:41 -0700
Paul et al,
Well the trick seems to have been in the handling of the strings within VB as Paul
said like so:
.Text = " & old_string & "
.Replacement.Text = " & new_string & "
The final script works fine and is enclosed. (The reference to different windows in
Word was so that the header and footer sections of the document could be processed
as well as the main body.)
Thanks so much for all your help. I need to use this to convert more than 1800 MS
Word documents at work!!! Our IT guys said it would take a couple of days to write
a program to do this on a Windows platform. I figured I might be able to make it
happen on the Mac. It took me a couple of days with Pauls help, but then this is
the first time I've programmed either Applescript or Visual Basic.
Thanks again Paul.
Ian MacLachlan
------------------Script Starts Here
-----------------------------------------------------
-- this is an AppleScript / Visual Basic 'droplet'
-- this droplet searches folders recursively for MS WORD, TEXT, RTF and WORDPERFECT
files
-- each file is processed such that all instances of the old_string are removed and
replaced with the new_string
-- any unprocessed files are logged
-- this script is designed to satisfy QA requirements for document control
--
-- the list of file types which will be processed
-- W8BN etc is Word for Mac or Win 2000, 98, 95, 6.0
-- RTF is RTF, TEXT is TEXT
property type_list : {"W8BN", "W6BN", "W5BN"}
-- the droplet processes files dropped onto the applet
--
on open these_items
-- initialize log file
--
set log_title to ("File Conversion Log - List of Untouched Files")
set target_file to (((path to desktop folder) as text) & "Conversion Log")
my write_to_file(log_title, target_file, false)
-- initialize text variables
--
--get input from user
--
display dialog "What text would you would like to remove?" default answer "Text to
Be Removed"
set the old_string to the text returned of the result
display dialog "Any occurance of '" & old_string & "' will be replaced with?"
default answer "Replacement Text"
set the new_string to the text returned of the result
set the response_text to "Any text containing the string '" & the old_string & "'
will be replaced with '" & the new_string & "'."
display dialog response_text buttons {"OK"} default button 1
my write_to_file(response_text, target_file, true)
set old_string to my ProcessForVB(old_string)
set new_string to my ProcessForVB(new_string)
-- begin process loop
--
repeat with i from 1 to the count of these_items
set this_item to item i of these_items
set the item_info to info for this_item
set this_data to ((this_item) as string)
if folder of the item_info is true then
process_folder(this_item, old_string, new_string)
else if (alias of the item_info is false) and (the file type of the item_info is
in the type_list) then
try
my process_item(this_item, old_string, new_string)
end try
else if (alias of the item_info is true) or (the file type of the item_info is
not in the type_list) then
try
my write_to_file(this_data, target_file, true)
end try
end if
end repeat
tell application "Microsoft Word"
quit
end tell
end open
-- this subroutine processes folders
--
on process_folder(this_folder, old_string, new_string)
set these_items to list folder this_folder without invisibles
set target_file to (((path to desktop folder) as text) & "Conversion Log")
repeat with i from 1 to the count of these_items
set this_item to alias ((this_folder as text) & (item i of these_items))
set the item_info to info for this_item
set this_data to ((this_item) as string)
if folder of the item_info is true then
process_folder(this_item, old_string, new_string)
else if (alias of the item_info is false) and (the file type of the item_info is
in the type_list) then
try
my process_item(this_item, old_string, new_string)
end try
else if (alias of the item_info is true) or (the file type of the item_info is
not in the type_list) then
try
my write_to_file(this_data, target_file, true)
end try
end if
end repeat
end process_folder
-- this sub-routine processes files
--
on process_item(this_item, old_string, new_string)
tell application "Microsoft Word"
activate
open this_item
do Visual Basic "ActiveDocument.Select
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = " & old_string & "
.Replacement.Text = " & new_string & "
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
' replace instances in header
If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
ActiveWindow.Panes(2).Close
End If
If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
ActivePane.View.Type = wdOutlineView Or ActiveWindow.ActivePane.View.Type _
= wdMasterView Then
ActiveWindow.ActivePane.View.Type = wdPageView
End If
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = " & old_string & "
.Replacement.Text = " & new_string & "
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll"
save document 1
close document 1
end tell
end process_item
on ProcessForVB(theString)
set finalString to ""
set num to (count paragraphs of theString)
repeat with i from 1 to num
set thePar to paragraph i of theString
set thePar to "\"" & thePar & "\""
set finalString to finalString & thePar
if i = num then
set finalString to finalString & return
else
set finalString to finalString & " & vbCr & "
end if
end repeat
return finalString
end ProcessForVB
on write_to_file(this_data, target_file, append_data)
try
set the target_file to the target_file as text
set this_data to ((this_data) as text) & (ASCII character (13))
set the open_target_file to ,
open for access file target_file with write permission
if append_data is false then ,
set eof of the open_target_file to 0
write this_data to the open_target_file starting at eof
close access the open_target_file
return true
on error
try
close access file target_file
end try
display dialog "Error in Writing Log File!"
return false
end try
end write_to_file