Re: Passing String Variables to Visual Basic withinApplescript???
Re: Passing String Variables to Visual Basic withinApplescript???
- Subject: Re: Passing String Variables to Visual Basic withinApplescript???
- From: Paul Berkowitz <email@hidden>
- Date: Mon, 25 Jun 2001 08:48:15 -0700
>
Paul,
>
>
Have implemented your handler to no avail. Do I still refer to variables in VB
>
the way I have in my example?
>
>
Cheers,
>
>
Ian
No, I hadn't looked at it closely enough. This sure took some debugging:
1. VB will just read "old_string" as the actual words there, not as a variable.
You need the variable to remain in applescript by removing it from the quotes
that the VB script is in when you do Visual Basic "a whole visual basic script
here", and concatenating the variables normally with ' & '.
2. You also shouldn't really have 6 different 'do Visual basic' commands when 1
will do.
3. Also it's not 'document 1 of window 1': it's just 'save document 1'. Even
those lines would go faster in VB (and by using VB you can also get Word to open
without making an extra blank document), but I've left them in applescript since
they are among the few commands that work OK in Word applescripting.
4. Your VB script needed to be debugged! You included in it, twice, the Str
function, which messed up the script. The Str function turns integers into
text, like
set num to num as string
does in applescript. But you weren't giving it numbers, you were giving it text,
so it errored. They just had to be removed.
5. The VB script (which you must have copied from somewhere?) works on a
selection in the front document (window). But you haven't selected anything,
either in the applescript, nor in the VB script! So nothing at all is going to
happen. You either have to select 1) what you want, or 2) the whole document, or
3) rewrite the VB script to work on a range, not selection (harder). I very
strongly recommend that you not to try to select anything in applescript, except
the whole document. It won't work right. Do it in VB. Here I've selected the
whole document in VB in the first line (ActiveDocument.Select).
6, In the applescript, if you want to use process_item as a genuine handler, you
have to include your variables as parameters, or it won't know what you're
talking about. That includes the alias or file 'this_item', plus old_string and
new_string. So its definition will be
on process_item(this_item, old_string, new_string)
When you call it, you call it like this:
my process_item(this_item, old_string, new_string)
First run old_string and new_string through the ProcessForVB handler I gave you
to add interior quotes and the right sort of internal carriage returns. Then the
fixed script below. I tested this with old_string as "cat" and new_string as
"dog" in a saved Word document that had many references to "cat". They all
changed to "dog". I assume that's what you're trying to do here.
----------------------------------------
--set old_string to "cat" -- my example
--set new_string to "dog" -- my example
--set this_item to alias "Data HD:cat.doc" -- my saved document
set old_string to my ProcessForVB(old_string)
set new_string to my ProcessForVB(new_string)
my process_item(this_item, old_string, new_string)
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"
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
-------------------------end script--------------------------------
On 6/24/01 1:26 PM, "Ian MacLachlan" <email@hidden> wrote:
>
>
Paul Berkowitz wrote:
>
>
> On 6/24/01 12:07 PM, "Ian MacLachlan" <email@hidden> wrote:
>
>
>
>> Help,
>
>>
>
>> I' m putting together a script (droplet) that takes user input
>
>> (old_string, new_string) and opens a series of Word Documents and makes
>
>> changes to the text using MS Word's replace command.
>
>>
>
>> Question: How do I refer to the string variables captured in the
>
>> Applescript section when I initiate the Visual Basic section? The
>
>> relevant parts of the script follow. I'll attach the whole thing as
>
>> well.
>
>> Applescript that gets variables:
>
>>
>
>> display dialog "What text would you would like to remove?" default
>
>> answer "Remove this text"
>
>> set the old_string to the text returned of the result
>
>> set old_string to ((old_string) as text)
>
>
>
> You don't need this line. 'text returned' of display dialog is ALWAYS text.
>
>
>
>>
>
>> display dialog "Any occurance of '" & old_string & "' will be replaced
>
>> with?" default answer "Replace with this text"
>
>> set the new_string to the text returned of the result
>
>> set new_string to ((new_string) as text)
>
>
>
> Same. You don't need that line.
>
>
>
>>
>
>> 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
>
>>
>
>> The section that opens Word and should make the repacements:
>
>
>
> Before you do this, you need another handler to add escape characters (\")
>
> for internal strings. I've also included the stuff you need in case you have
>
> any carriage returns I your applescript strings (you don't this time, but
>
> you might another time. Run each of your string variables (old_string,
>
> new_string) through this handler.
>
>
>
> 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
>
>
>
> --
>
> Paul Berkowitz
>
> _______________________________________________
>
> applescript-users mailing list
>
> email@hidden
>
> http://www.lists.apple.com/mailman/listinfo/applescript-users
>
>
--
Paul Berkowitz