Jump to content

Recommended Posts

Posted

Hello guys, I'm having trouble to get the current path of the directory from the open/save dialog from an application (not AutoIt GUI).

I created this simple loop that will get the text of an active window's combobox:

while 1
sleep(40)
Global $hwndActive = WinGetHandle("[ACTIVE]")
sleep(40)
$test=ControlGetText($hwndActive, "", "ComboBox1")
sleep(40)
ConsoleWrite($test & @crlF)
sleep(40)
WEnd

The code above works when there's a open/save dialog. Unfortunately, as you can see, it can only get the current directory name, not the complete path of the directory.

So, can someone help me? Any help would be appreciated.

Thank you.

Posted (edited)

One method (not work for desktop or similar objects):

#include <GUIComboBoxEx.au3>

$hCombo = ControlGetHandle("[ClASS:#32770;TITLE:Save As]", "", "ComboBox1")

$sPath = _GUICtrlComboBoxEx_GetPath($hCombo)
ConsoleWrite($sPath & @CRLF)

Func _GUICtrlComboBoxEx_GetPath($hCombo)
    Local $sRet_Path = ""
    Local $iIndex = _GUICtrlComboBoxEx_GetCurSel($hCombo)
    
    For $i = $iIndex To 0 Step -1
        $sCurrent_Text = ""
        _GUICtrlComboBoxEx_GetItemText($hCombo, $i, $sCurrent_Text)
        
        If StringRegExp($sCurrent_Text, "(?i)\A\([a-z]:\)") Then
            $sRet_Path = StringRegExpReplace($sCurrent_Text, "(?i)\A\(([a-z]):\).*", "\1:") & "\" & $sRet_Path
            Return StringRegExpReplace($sRet_Path, "\\$", "")
        EndIf
        
        $sRet_Path = $sCurrent_Text & "\" & $sRet_Path
    Next
    
    $sCurrent_Text = ""
    _GUICtrlComboBoxEx_GetItemText($hCombo, $iIndex, $sCurrent_Text)
    
    Return $sCurrent_Text
EndFunc
Edited by MrCreatoR

 

  Reveal hidden contents

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Posted

Thank you, MrCreatoR for trying to help me. But the _GUICtrlComboBoxEx_GetPath returns only the directory name, not the complete path, just like ControlGetText.

I tested your _GUICtrlComboBoxEx_GetPath function using this loop:

  Quote

While 1

$hCombo = ControlGetHandle("[ClASS:#32770]", "", "ComboBox1")

sleep(20)

$sPath = _GUICtrlComboBoxEx_GetPath($hCombo)

sleep(20)

ConsoleWrite($sPath & @CRLF)

sleep(20)

WEnd

But, because you introduced me to _GUICtrlComboBoxEx_GetCurSel, I think I could do something about this. At least I got something new that I can try. Thanks again, MrCreatoR.
Posted

  Quote

I tested your _GUICtrlComboBoxEx_GetPath function using this loop:

Test it like this:

#include <GUIComboBoxEx.au3>

Run(@AutoItExe & ' /AutoIt3ExecuteLine "FileSaveDialog(''Save As'', ''C:\Windows\System32'', ''All (*.*)'')"')

WinWait("[ClASS:#32770;TITLE:Save As]")

$hCombo = ControlGetHandle("[ClASS:#32770;TITLE:Save As]", "", "ComboBox1")

$sPath = _GUICtrlComboBoxEx_GetPath($hCombo)
ConsoleWrite($sPath & @CRLF)

If not working, then probably you have different drive's label format (i.e: (C:\ Label).

 

  Reveal hidden contents

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Posted

  MrCreatoR said:

Test it like this:

#include <GUIComboBoxEx.au3>

Run(@AutoItExe & ' /AutoIt3ExecuteLine "FileSaveDialog(''Save As'', ''C:\Windows\System32'', ''All (*.*)'')"')

WinWait("[ClASS:#32770;TITLE:Save As]")

$hCombo = ControlGetHandle("[ClASS:#32770;TITLE:Save As]", "", "ComboBox1")

$sPath = _GUICtrlComboBoxEx_GetPath($hCombo)
ConsoleWrite($sPath & @CRLF)

If not working, then probably you have different drive's label format (i.e: (C:\ Label).

I tried it with the example, but it gives the same result. I also think it shows different results on different compy because of my computer settings. But, at least I know the direction where I should go from now.

Thanks again.

Posted (edited)

Another idea is to create a temporary file (folder actualy) using toolbar button, copy that folder to clipboard, and then get the path using ClipGet(), but it's also not perfectly reliable, because on different systems the toolbar might be changed and designed differrently:

#include <GUIComboBoxEx.au3>
#include <GUIToolbar.au3>
;

Run(@AutoItExe & ' /AutoIt3ExecuteLine "FileSaveDialog(''Save As'', ''C:\Windows'', ''All (*.*)'')"')

WinWait("[ClASS:#32770;TITLE:Save As]")

$sPath = _OpenSaveDialog_GetPath("[ClASS:#32770;TITLE:Save As]")
ConsoleWrite($sPath & @CRLF)

Func _OpenSaveDialog_GetPath($sTitle, $iToolbar_Btn_Indx=2)
    Local $sRet_Path = "", $sFileTime, $hToolbar, $nBtn_CmdID
    
    $hToolbar = ControlGetHandle($sTitle, "", "ToolbarWindow321")
    WinActivate($hToolbar)
    
    $nBtn_CmdID = _GUICtrlToolbar_IndexToCommand($hToolbar, $iToolbar_Btn_Indx)
    _GUICtrlToolbar_ClickButton($hToolbar, $nBtn_CmdID)
    
    Sleep(300)
    ControlFocus($sTitle, "", "Edit1")
    
    ClipPut("")
    
    Sleep(300)
    ControlSend($sTitle, "", "SysListView321", "^{INS}")
    
    Sleep(300)
    $sRet_Path = ClipGet()
    
    If $sRet_Path <> "" Then
        $sFileTime = FileGetTime($sRet_Path, 1, 1)
        If StringTrimRight($sFileTime, 2) = @YEAR & @MON & @MDAY & @HOUR & @MIN Then DirRemove($sRet_Path)
    EndIf
    
    Return StringRegExpReplace($sRet_Path, "\\[^\\]*$", "")
EndFunc
Edited by MrCreatoR

 

  Reveal hidden contents

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Posted (edited)

Hi, MrCreatoR. I haven't tried your method with temporary folder yet, but I somehow managed to make your first method to work on me. And I think it should work on others even when they have different format for the drives, because the ( : ) in always front of the drive letters... right?

#include <GUIComboBoxEx.au3>
dim $sText
Run(@AutoItExe & ' /AutoIt3ExecuteLine "FileSaveDialog(''Save As'', ''C:\Windows\System32'', ''All (*.*)'')"')

WinWait("[ClASS:#32770;TITLE:Save As]")

$hCombo = ControlGetHandle("[ClASS:#32770;TITLE:Save As]", "", "ComboBox1")

$sPath = _GetPath($hCombo)
ConsoleWrite($sPath & @CRLF)

Func _GetPath($hCombo)
    Local $sRet_Path = ""
    Local $iIndex = _GUICtrlComboBoxEx_GetCurSel($hCombo)
    
    For $i = $iIndex To 0 Step -1
        $sCurrent_Text = ""
        _GUICtrlComboBoxEx_GetItemText($hCombo, $i, $sCurrent_Text)
        $result = StringInStr($sCurrent_Text, ":",0,-1)
        if $result=0 then
            $sRet_Path = $sCurrent_Text & "\" & $sRet_Path
        Else
            $sCurrent_Text2 = StringMid($sCurrent_Text, $result-1, 2)
            $sRet_Path = $sCurrent_Text2 & "\" & $sRet_Path
            Return $sRet_Path
        EndIf
    Next
   
    $sCurrent_Text = ""
    _GUICtrlComboBoxEx_GetItemText($hCombo, $iIndex, $sCurrent_Text)
   
    Return $sCurrent_Text
EndFunc
Edited by MDCT
Posted

Ok, here is two different versions, the first is using temp file, and the second uses combobox method:

#include <GUIComboBoxEx.au3>
#include <Misc.au3>
;

Run(@AutoItExe & ' /AutoIt3ExecuteLine "FileSaveDialog(''Save As'', @FavoritesDir, ''All (*.*)'')"')
WinWaitActive("[ClASS:#32770;TITLE:Save As]")

$sPath = _OpenSaveDialog_GetPath_FileMode("[ClASS:#32770;TITLE:Save As]")
ConsoleWrite("_OpenSaveDialog_GetPath_FileMode: " & $sPath & @CRLF)

$sPath = _OpenSaveDialog_GetPath_ComboMode("[ClASS:#32770;TITLE:Save As]")
ConsoleWrite("_OpenSaveDialog_GetPath_ComboMode: " & $sPath & @CRLF)

; #FUNCTION# ====================================================================================================
; Name...........:  _OpenSaveDialog_GetPath_FileMode
; Description....:  Retrieves a "working" path from Open/Save dialog.
; Syntax.........:  _OpenSaveDialog_GetPath_FileMode($sTitle="[ClASS:#32770]")
; Parameters.....:  $sTitle - [Optional] Title of Open/Save dialog (default is "[ClASS:#32770]").
;                   
; Return values..:  Success - Current path from Open/Save dialog.
;                   Failure - Empty string ("") and @error set to 1.
; Author.........:  MrCreatoR (CreatoR's Lab, http://creator-lab.ucoz.ru)
; Modified.......:  
; Remarks........:  Tested on Win XP with standard dialogs.
; Related........:  
; Link...........:  
; Example........:  Yes.
; ===============================================================================================================
Func _OpenSaveDialog_GetPath_FileMode($sTitle="[ClASS:#32770]")
    Local $sRet_Path = "", $sFileTime, $sFileName, $sTmp_FileName, $sCurrent_Time
    Local $sTmp_File = @TempDir & "\~GetPath_TmpFile.tmp"
    
    ClipPut("")
    
    FileClose(FileOpen($sTmp_File, 2))
    _ClipPutFile($sTmp_File)
    
    Sleep(500)
    
    ControlFocus($sTitle, "", "SysListView321")
    ControlListView($sTitle, "", "SysListView321", "SelectClear")
    
    ControlSend($sTitle, "", "SysListView321", "+{INS}")
    Sleep(500)
    
    While ControlListView($sTitle, "", "SysListView321", "GetSelectedCount") < 1
        Sleep(10)
    WEnd
    
    ClipPut("")
    FileDelete($sTmp_File)
    
    ControlSend($sTitle, "", "SysListView321", "^{INS}")
    Sleep(500)
    
    $sRet_Path = ClipGet()
    
    ControlSetText($sTitle, "", "Edit1", "", 1)
    
    If $sRet_Path <> "" Then
        $sFileName = StringRegExpReplace($sRet_Path, "^.*\\", "")
        $sTmp_FileName = StringRegExpReplace($sTmp_File, "^.*\\", "")
        $sCurrent_Time = @YEAR & @MON & @MDAY & @HOUR & @MIN
        
        $sFileTime = FileGetTime($sRet_Path, 1, 1)
        If $sFileName = $sTmp_FileName And StringTrimRight($sFileTime, 2) = $sCurrent_Time Then FileDelete($sRet_Path)
    Else
        Return SetError(1, 0, "")
    EndIf
    
    Return StringRegExpReplace($sRet_Path, "\\[^\\]*$", "")
EndFunc

; #FUNCTION# ====================================================================================================
; Name...........:  _OpenSaveDialog_GetPath_ComboMode
; Description....:  Retrieves a "working" path from Open/Save dialog.
; Syntax.........:  _OpenSaveDialog_GetPath_ComboMode($sTitle="[ClASS:#32770]")
; Parameters.....:  $sTitle - [Optional] Title of Open/Save dialog (default is "[ClASS:#32770]").
;                   
; Return values..:  Success - Current path from Open/Save dialog.
;                   Failure - Empty string ("") and @error set to 1.
; Author.........:  MrCreatoR (CreatoR's Lab, http://creator-lab.ucoz.ru)
; Modified.......:  
; Remarks........:  Tested on Win XP with standard dialogs.
; Related........:  
; Link...........:  
; Example........:  Yes.
; ===============================================================================================================
Func _OpenSaveDialog_GetPath_ComboMode($sTitle="[ClASS:#32770]")
    Local $sRet_Path = "", $hCombo, $iIndex, $sCurrent_Text
    
    $hCombo = ControlGetHandle($sTitle, "", "ComboBox1")
    $iIndex = _GUICtrlComboBoxEx_GetCurSel($hCombo)
    
    For $i = $iIndex To 0 Step -1
        $sCurrent_Text = ""
        _GUICtrlComboBoxEx_GetItemText($hCombo, $i, $sCurrent_Text)
        
        If StringRegExp($sCurrent_Text, "(?i)[a-z]:") Then
            $sRet_Path = StringRegExpReplace($sCurrent_Text, "(?i).*([a-z]):\).*", "\1") & ":\" & $sRet_Path
            Return StringRegExpReplace($sRet_Path, "\\$", "")
        EndIf
        
        $sRet_Path = $sCurrent_Text & "\" & $sRet_Path
    Next
    
    $sCurrent_Text = ""
    _GUICtrlComboBoxEx_GetItemText($hCombo, $iIndex, $sCurrent_Text)
    
    If $sCurrent_Text = "" Then Return SetError(1, 0, "")
    
    Return $sCurrent_Text
EndFunc

 

  Reveal hidden contents

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Posted

Wonderful script, MrCreatoR. I use your combobox method, it works nicely on my computer.

Thanks again. :D

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...