2

我目前正在从我的主 vbscript 运行一个新的 vbscript,方法是从字符串数组“即时”创建第二个脚本并将其写入外部secondfile.vbs文件,然后WshShell.Run "C:\secondfile.vbs"在第一个运行时使用我需要它来继续运行。

我遇到的问题是,因为我必须重复很多次,所以使用 FSO 编写新的 vbs 文件会稍微减慢这个过程 - 我正试图消除它。有没有办法在不创建物理文件的情况下从内存中运行 vbscript?寻找一种将我的脚本作为字符串并直接作为独立的 vbscript 执行的方法。在此先感谢您的任何建议。

以下是其当前工作方式的简化代码:

Option Explicit

'this is the main RhinoScript file
Call MainRhinoScript()
Sub MainRhinoScript()

    'create Wscript.Shell object
    Dim WshShell : Set WshShell = CreateObject("WScript.Shell")
    Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject"): 
    'get or create user settings plugin folder and vbs file path string 
    Dim strVBSEventWatcherFile : strVBSEventWatcherFile = "C:\eventwatcher.vbs"

    Do
        'create auto-generated vbs file here and run it
        Call WriteVBS_EventWatcher_File(strVBSEventWatcherFile,
        Call WshShell.Run(strVBSEventWatcherFile)

        'The main code goes here, looped until done.
        'VBS eventwatcher script file is running simultaneously.
    Loop

End Sub

'this is simplified VBS writing subroutine to demonstrate how the code works, the actual one is much longer:
Private Sub WriteVBS_EventWatcher_File(strFilePath)

    Dim i,fso : Set fso = CreateObject("Scripting.FileSystemObject")
    Dim file : Set file = fso.CreateTextFile(strFilePath, True)

    file.WriteLine("Option Explicit")
    file.WriteLine("")
    file.WriteLine("Call Main()")
    file.WriteLine("Sub Main()")
    file.WriteLine("    Dim WshShell : Set WshShell = CreateObject(""WScript.Shell"")'create shell script object")
    file.WriteLine("WScript.Sleep 10")
    file.WriteLine("WshShell.SendKeys ""Rhino_Preselect "" ")
    file.WriteLine("End Sub")   
    file.Close()

End Sub
4

1 回答 1

2

使用运行 WSH VB 脚本运行某些代码的最简单方法是将代码传递给ExecuteGlobal函数(或Execute使用本地范围):

arrCode = Array(_
    "For x = 1 To 10", _
    "MsgBox x", _
    "Next"_
    )

strCode = Join(arrCode, vbCrLf)

ExecuteGlobal strCode

您可以通过链接找到 VBScript“多进程”实现。该代码仅适用于 WSH 脚本引擎。RhinoScript 环境需要重新编写和调整该代码,因为存在差异:RhinoScript 没有WScript可用的对象,因此问题是无法检索启动的 RhinoScript 文件的路径。唯一的方法是在代码中硬编码路径。WSH VBS 文件扩展名也是.vbs.RhinoScript 是.rvb.

有些东西从评论中变得更清楚了。您写道,您正在尝试提高运行代码的速度,但请注意,该WshShell.SendKeys方法比保存和执行文件花费的时间要长得多,而且不可靠。我的线索是避免使用.SendKeys并尝试使用RhinoScript 对象模型应用程序方法来实现自动化。也许这样你就不需要任何代码创建。这取决于您最初打算自动化的内容。

您还指出,vbscript 代码片段是从一个主要的 RhinoScript 运行时一个接一个地创建、修改和执行的。因此,即使继续使用您的初始脚本,也不太清楚为什么有必要制作任何“多进程”模型?为什么不创建每个片段并将其传递给ExecuteGlobal我上面的示例?

于 2016-04-22T20:36:52.473 回答