Re: do visual basic
Re: do visual basic
- Subject: Re: do visual basic
- From: Paul Berkowitz <email@hidden>
- Date: Wed, 10 Mar 2004 08:56:17 -0800
On 3/10/04 8:08 AM, "Giampiero Cairo" <email@hidden> wrote:
>
>
I use applescript and in particular the command "Do Visual Basic"
>
>
I want to get a value of a variable from the routine of Visual basic to the
>
rest of applescript code.
>
>
>
this is my snippet of script:
>
>
>
set myvar to ""
>
>
Do Visual Basic "
>
dim x as string
>
x = 10
>
Return x
>
"
>
>
repeat with x from 1 to 10
>
set AnotherVar to x + 1
>
end repeat
>
>
>
Is it correct if I would pass the x value to the rest of my script for
>
example to the "repeat cicle"?
You can't get values, or any real result, back to your main script, from 'do
Visual Basic'. The "result" of a 'do Visual Basic' is just a confirmation
that the command ran OK.
To get a result back, you need to write it to a text file within VB, then
read the text file back in AppleScript. I have always first coerced any
non-string data type (numbers, booleans. etc.) to string and read it back as
string and then coerce back to the other type. I'm not sure that's really
necessary - you might be able to 'read as integer,' 'read as date', but it's
simple and straightforward: use the 'CStr' function. The command which
writes to file in VB is Print# - check both out in the VB Editor's Help. You
can see an example of how to do this in the 'Print Tasks List PREFS' script
- at the end of the DoVBScript handler - that comes in my 'Print Tasks List'
script available at
MacScripter.net <
http://macscripter.net/scriptbuilders/>
Here's the relevant except. At the top of the handler, I define a file
specification in temporary items for the file to be written, suitably quoted
for 'do Visual basic.' I've left out the maion part of the handler here
where I do things and get values for a Word Table. Then I coerce the various
items in myPropertyString variable to string with CStr() and string them
together to make a comma-delimited string. Then I do the equivalent of 'open
for access with write permission' (FreeFile and Open for Outpu) and write
(PrintX) to the file:
on DoVBScript()
set vbTextFilePath to "\"" & (path to temporary items as string) &
"Shared Text File\""
return "
Dim myPropertyString As String ' etc. etc.
'[snip most of script, getting table values]
myPropertyString = CStr(lAutoFormatType) & \", \" &
CStr(bAllowPageBreaks) & _
\", \" & CStr(bAllowAutoFit) & \", \" & CStr(lRowsAllowOverlap) &
\", \" & CStr(lRowsHeightRule) _
& \", \" & CStr(sngRowsHeight) & \", \" & CStr(lRowsWrapAroundText)
& \", \" & CStr(bFirstColumnApplied)
' [snip]
'write myPropertyString to text file, replacing any text there
FileNumber = FreeFile
Open " & vbTextFilePath & " For Output As #FileNumber
Print #FileNumber, myPropertyString
Close #FileNumber
"
end DoVBScript
--------------
Back in the main script I run the VB script by calling the handler, then
read from the file:
tell application "Microsoft Word"
-- reads data off the user-customized table in front, uses
MacScript to load the main script and writes the data as a string to main
Script's vTablePropertyString property
do Visual Basic vbScript
end tell
set tempFilePath to ((path to temporary items as string) & "Shared
Text File")
set g to open for access file tempFilePath
set myPropertyString to read g
close access g
tell application "Finder" to delete alias tempFilePath -- not really
necessary
-------------
Then it's just a matter of parsing the comma-delimited string and converting
some of its string elements to integers, reals, dates, etc. (I do that back
in the main Print Tasks List script when it's run. In this PREFS script I
simply load Print Tasks List script and store myPropertyString as the main
script's vTablePropertyString property.)
This way I'm able to set user preferences for a custom Table format for the
main script. Just to get a simple string result back basically involves 4
lines :
FileNumber = FreeFile
Open " & vbTextFilePath & " For Output As #FileNumber
Print #FileNumber, myPropertyString
Close #FileNumber
in VB (given a file path) and then the two AS lines:
set tempFilePath to ((path to temporary items as string) & "Shared
Text File")
set myPropertyString to read g
and that's it. The tricky part is all the quoting for 'do Visual Basic', but
you probably know all about that already.
--
Paul Berkowitz
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.