【问题标题】:How do i automate the simulation of right clicking a file and selecting the first option using vbscript如何自动模拟右键单击文件并使用 vbscript 选择第一个选项
【发布时间】:2016-06-20 02:07:57
【问题描述】:

目前我正在这样做。但这不起作用,只是创建 vb 脚本的快捷方式。

   Dim objShell
   Set objShell = WScript.CreateObject ("WScript.shell")
   objShell.Run "E:\folder\xyz.cmd"
   objShell.Sendkeys ("+{F10}")
   objShell.Sendkeys "s"
   Set objShell = Nothing

有人可以帮我解决这个问题吗?谢谢

【问题讨论】:

    标签: vbscript right-click


    【解决方案1】:

    这使用了 shell 的 COM 对象。我们选择我们想要的动词。

    运行不带参数的脚本以获得帮助。

    列出动词

    shverb.vbs  "E:\folder\xyz.cmd" 
    

    打开

    shverb.vbs  "E:\folder\xyz.cmd" edit
    

    shell 的文档在这里https://msdn.microsoft.com/en-us/library/windows/desktop/bb787868(v=vs.85).aspx


    HelpMsg = vbcrlf & "  ShVerb" & vbcrlf & vbcrlf & "  David Candy 2014" & vbcrlf & vbcrlf & "  Lists or runs an explorer verb (right click menu) on a file or folder" & vbcrlf  & vbcrlf & "    ShVerb <filename> [verb]" & vbcrlf & vbcrlf & "  Used without a verb it lists the verbs available for the file or folder" & vbcrlf & vbcrlf
    HelpMsg = HelpMsg & "  The program lists most verbs but only ones above the first separator" & vbcrlf & "  of the menu work when used this way" & vbcrlf & vbcrlf 
    HelpMsg = HelpMsg & "  The Properties verb can be used. However the program has to keep running" & vbcrlf & "  to hold the properties dialog open. It keeps running by displaying" & vbcrlf & "  a message box." 
    Set objShell = CreateObject("Shell.Application")
    Set Ag = WScript.Arguments 
    set WshShell = WScript.CreateObject("WScript.Shell") 
    Set fso = CreateObject("Scripting.FileSystemObject")
    
        If Ag.count = 0 then 
            wscript.echo "  ShVerb - No file specified"
            wscript.echo HelpMsg 
            wscript.quit
        Else If Ag.count = 1 then 
            If LCase(Replace(Ag(0),"-", "/")) = "/h" or Replace(Ag(0),"-", "/") = "/?" then 
                wscript.echo HelpMsg 
                wscript.quit
            End If
        ElseIf Ag.count > 2 then 
            wscript.echo vbcrlf & "  ShVerb - To many parameters" & vbcrlf & "  Use quotes around filenames and verbs containing spaces"  & vbcrlf
            wscript.echo HelpMsg 
            wscript.quit
        End If
    
        If fso.DriveExists(Ag(0)) = True then
            Set objFolder = objShell.Namespace(fso.GetFileName(Ag(0)))
    '       Set objFolderItem = objFolder.ParseName(fso.GetFileName(Ag(0)))
            Set objFolderItem = objFolder.self
            msgbox ag(0)
        ElseIf fso.FolderExists(Ag(0)) = True then
            Set objFolder = objShell.Namespace(fso.GetParentFolderName(Ag(0)))
            Set objFolderItem = objFolder.ParseName(fso.GetFileName(Ag(0)))
        ElseIf fso.fileExists(Ag(0)) = True then
            Set objFolder = objShell.Namespace(fso.GetParentFolderName(Ag(0)))
            Set objFolderItem = objFolder.ParseName(fso.GetFileName(Ag(0)))
        Else
            wscript.echo "  ShVerb - " & Ag(0) & " not found"
            wscript.echo HelpMsg 
            wscript.quit
        End If
    
        Set objVerbs = objFolderItem.Verbs
    
        'If only one argument list verbs for that item
    
        If Ag.count = 1 then
            For Each cmd in objFolderItem.Verbs
                If len(cmd) <> 0 then CmdList = CmdList & vbcrlf & replace(cmd.name, "&", "") 
            Next
            wscript.echo mid(CmdList, 2)
    
        'If two arguments do verbs for that item
    
        ElseIf Ag.count = 2 then
            For Each cmd in objFolderItem.Verbs
                If lcase(replace(cmd, "&", "")) = LCase(Ag(1)) then 
                    wscript.echo(Cmd.doit)
                    Exit For
                End If
            Next
        'Properties is special cased. Script has to stay running for Properties dialog to show.
            If Lcase(Ag(1)) = "properties" then
                WSHShell.AppActivate(ObjFolderItem.Name & " Properties")
                msgbox "This message box has to stay open to keep the " & ObjFolderItem.Name & " Properties dialog open."
            End If  
        End If
    End If
    

    【讨论】:

    • @Noodles非常感谢您的回复。但是请您帮我理解代码。我在哪里传递我需要右键单击的文件的路径以及我在哪里提到我想要的选项点击
    • 我在文章的顶部给了你两个例子。在代码中Ag(0) 是宾语,Ag(1) 是动词。
    • @Noodles所以为了打开这个文件,我需要这样。设置 Ag = "E:\path\abc.cmd" 打开
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-12
    • 2019-03-26
    相关资源
    最近更新 更多