nixda
nixda

Reputation: 2697

Get path of selected Windows Explorer file in AutoIt

My AutoIt script worked as long as I used it via command line. There I could use $CmdLine[1] and pass a path as argument. Now I try to convert the script to a new method to avoid command line arguments.

You open an Explorer Window and select a file e.g C:\test.txt. After that you trigger the AutoIt function with CTRL+WIN+C. The script should look what file is selected in the active Explorer Window and retrieve the path C:\test.txt and assign it to $file variable.

This is my work-in-progress where I'm stuck.
Line 5 $CmdLine[1] needs to be changed to a secret function I don't know.

;Assign key combination "CTRL-WIN-C" to function "copyUNC"
HotKeySet("^#c", "CopyUNC") 

;function to copy UNC path of selected Windows Explorer file/folder to clipboard
func CopyUNC()
    $file = FileGetLongName($CmdLine[1])   ;THIS LINE NEEDS TO BE CHANGED
    $drive = StringLeft($file, 2)
    $UNCdrive = DriveMapGet($drive)
    If $UNCdrive = "" Then  
        $UNCfile = $file    
    else 
        $UNCfile = $UNCdrive & StringTrimLeft($file, 2)
    endif   
    ClipPut($UNCfile)
endfunc

;necessary loop so AutoIt script stays active and in Tray
While 1
    Sleep(100)
WEnd

Q: How do I get the path of a selected file/folder from Windows Explorer into AutoIt v3.3.8.1?

Note #1: I don't want to use registry and right-click tricks to pass the argument
Note #2: If multiple files are selected just pass the first file. Don't overcomplicate things

Upvotes: 0

Views: 5254

Answers (2)

虫子樱桃
虫子樱桃

Reputation: 51

maybe this

HotKeySet('{F8}','ccc')
Func ccc()
$ObjWindows = ObjCreate("Shell.Application").Windows()
For $i = 0 To $ObjWindows.Count -1
        Local $w = $ObjWindows.Item($i)
        Local $aSeLected = $w.Document.SelectedItems()
        For $b = 0 To $aSeLected.Count -1
                $x = $aSeLected.Item($b)
                MsgBox(0,'',$x.Path&'_______'&$x.Name)
        Next
Next
EndFunc
While 1
        Sleep(100)
WEnd

Upvotes: 0

Milos
Milos

Reputation: 2946

CMDLINE[1] has nothing to do with what you want.

If you want to activate your script by hotkey AFTER manually selecting the file in Windows Explorer, you need to examine the Explorer window itself.

Here is the function to retrieve selected item in explorer

Func GetExplorerSelection()
Local $saveClip = ""
Local $filesFolders = ""
Local $handle = _WinAPI_GetForegroundWindow()
Local $className = _WinAPI_GetClassName($handle)
If $className = "ExploreWClass" Or $className = "CabinetWClass" Then
$saveClip = ClipGet()
Send("^c")
Sleep(50) ; give clipboard time to react
$filesFolders = ClipGet()
ClipPut($saveClip)
; test if $filesFolders contains @LF and split if so etc..
; code to call StringSplit() etc..
EndIf
EndFunc

Upvotes: 1

Related Questions