vbscript - 从另一个脚本运行 vbscript 而不写入文件

标签 vbscript runtime wsh rhino3d

我目前正在从主 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

最佳答案

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

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

strCode = Join(arrCode, vbCrLf)

ExecuteGlobal strCode

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

有些事情从评论中变得更加清晰。您写道,您试图获得更快的运行代码速度,但请注意,WshShell.SendKeys 方法比保存和执行文件花费的时间要长得多,而且不可靠。我的线索是避免使用 .SendKeys 并尝试使用 RhinoScript object model 实现自动化应用方法。也许这样你就不需要创建任何代码。这取决于您最初打算自动化的内容。

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

关于vbscript - 从另一个脚本运行 vbscript 而不写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36801156/

相关文章:

sql-server - DTS 转换为 SSIS 派生列表达式

batch-file - 如何防止 VBScript 独立运行?

cordova - 使用create命令的Phonegap-Cordova错误消息

fortran - Fortran FORMAT 是在编译时还是执行时完成的?

java - 无法在 Mac 上安装 Java 运行时 (6)

batch-file - 如何在VBS Shell命令参数中转义等号=

VBScript 将所有文件从一个文件夹移动到另一个文件夹

vbscript - 创建停留在顶部并阻止其他窗口的 vbscript 消息框

vbscript - 处理 VBScript 网站中未处理的错误?

delphi - 在运行时创建 Tframe :