【问题标题】:Focus or open a folder?聚焦还是打开文件夹?
【发布时间】:2015-11-28 04:24:58
【问题描述】:

我在使用这个脚本时遇到了一些问题:

If WinExists ("[CLASS:CabinetWClass]", "Address: C:\Users\Dad\Downloads") Then
    WinActivate ("[CLASS:CabinetWClass]", "Address: C:\Users\Dad\Downloads")
Else
    Run("Explorer.exe" & "C:\Users\Dad\Downloads")
Endif
  1. 如果我打开了 Downloads 的子目录,例如 C:\Users\Dad\Downloads\Pictures,它将聚焦该窗口而不是继续Else 语句。

  2. 如果没有打开 Windows 资源管理器窗口,系统只会对我发出哔哔声,然后脚本关闭。我在这里的答案中选择了我的代码:https://www.autoitscript.com/forum/topic/30600-open-folder-with-autoit/

我尝试将此标记为 Run() 函数和 text 参数。

【问题讨论】:

  • 如果“&”被移除,2.被解析。
  • 由于某种原因,删除“&”也解决了 1。所以,我很好奇为什么。

标签: function text autoit targeting


【解决方案1】:

由于某种原因,以下代码均未出现两种不需要的行为:

If WinExists ("[CLASS:CabinetWClass]", "Address: C:\Users\Dad\Downloads") Then
    WinActivate ("[CLASS:CabinetWClass]", "Address: C:\Users\Dad\Downloads")
Else
    Run("Explorer.exe C:\Users\Dad\Downloads") #this line was changed.
Endif

【讨论】:

  • 问题是你在连接两个字符串“Explorer.exe”和“C:\Users\Dad\Downloads”时错过了一个空格,就像你在问题中所做的那样。基本上你试图运行这个命令:Explorer.exeC:\Users\Dad\Downloads。这是行不通的。
【解决方案2】:

这是您想要做的一个工作示例。基本上,您的代码与您要查找的目录的子字符串匹配。这就是它激活具有相同子目录的窗口的方式。

FindorOpenExporer("C:\Users\Dad\Downloads")

Func FindorOpenExporer($sPath)
    Local $aWinList = WinList("[CLASS:CabinetWClass]")

    ;if no Exporer windows are found
    If IsArray($aWinList) = False Then
        StartEplorer($sPath)
        Return 0
    EndIf

    ;if explorer windows are found
    For $i = 1 To UBound($aWinList) - 1
        $sWinText = WinGetText($aWinList[$i][1])

        ;activates the window and returns the window handle if it is found
        If StringInStr($sWinText, "Address: " & $sPath) Then
            WinActivate($aWinList[$i][1])

            ;returns the window handle
            Return $aWinList[$i][1]
        EndIf
    Next

    StartEplorer($sPath)
EndFunc   ;==>FindorOpenExporer

Func StartEplorer($sPath)
    Run("Explorer.exe " & $sPath)
EndFunc   ;==>StartEplorer

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多