Jump to content


Photo

Can AutoHotKey retrieve file path of the selected file ?


  • Please log in to reply
27 replies to this topic

#1 zoran

zoran
  • Members
  • 5 posts

Posted 26 November 2010 - 08:04 PM

Hi all,

I would like to use AutoHotkey as following:
- select one file in Win. Explorer and with user defined key combination I would like to execute one exe/bat file over selected file. For that I need to retrieve selected file path and to send it to my exe/bat ... how to do this ? :idea:

Thanks,

bets regards,
Zoki.

#2 girlgamer

girlgamer
  • Moderators
  • 2039 posts

Posted 26 November 2010 - 08:20 PM

Take a look at
Loop, FilePattern [, IncludeFolders?, Recurse?]
Specifically the Special Variables Available Inside a File-Loop section of the AHK help file.

#3 Learning one

Learning one
  • Members
  • 1295 posts

Posted 26 November 2010 - 08:26 PM

F1::MsgBox, % gst()  ; example



gst() {   ; GetSelectedText or FilePath in Windows Explorer  by Learning one 

	IsClipEmpty := (Clipboard = "") ? 1 : 0

	if !IsClipEmpty {

		ClipboardBackup := ClipboardAll

		While !(Clipboard = "") {

			Clipboard =

			Sleep, 10

		}

	}

	Send, ^c

	ClipWait, 0.1

	ToReturn := Clipboard, Clipboard := ClipboardBackup

	if !IsClipEmpty

	ClipWait, 0.5, 1

	Return ToReturn

}


#4 Rapte_Of_Suzaku

Rapte_Of_Suzaku
  • Members
  • 901 posts

Posted 26 November 2010 - 08:51 PM

Getting fancier (and without using the clipboard):

F1::
	selection := FC("explorer","","selection")
	Loop % selection.len()
		run % "notepad.exe """ selection[A_Index] """"
return

Using AHK build with objects and my FileContainer object. Also see Explorer Selection Hotkeys.

#5 girlgamer

girlgamer
  • Moderators
  • 2039 posts

Posted 27 November 2010 - 12:24 AM

Actually there's another way too if you select the file in Win.explorer and the link to your batch file is on the desktop. It's called Drag And Drop.

#SingleInstance force
x2 = 186	;This is where the Notepad icon is on my desktop
y2 = 429	;put the coords for your batch file or exe icon here
Run Explorer.exe 
WinWaitActive, ahk_class CabinetWClass
MsgBox,4160,Drag&Drop Demo, Highlight the file you want D&D'd in Explorer and press F1. Watch  the magic happen.
Return

F1::
Click
Sleep 200
CoordMode,Mouse,Screen
MouseGetPos, x1,y1
MouseClickDrag, Right, x1,y1,x2,y2,50
CoordMode,Mouse,Relative
Return

Select the "Open With" item in the resulting menu and you're on your way.

#6 zoran

zoran
  • Members
  • 5 posts

Posted 27 November 2010 - 10:50 AM

As novice in AutoHotKey I must admit that Rapte_Of_Suzaku solution seems shortest..but it does not work :(

Content of my_path.ahk file is:

#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
#a::
selection := FC("explorer","","selection")
Loop % selection.len()
run % "notepad.exe """ selection[A_Index] """"
return





and if executed I get message:
Error: Call to nonexisting function.

Specifically: FC("explorer", "", "selection")
...

i tried to download in the same folder yours Everything.zip with ShellFileOperation.ahk, Prefs.ahk, Container.ahk, FC.ahk...but it still does not works.

Could you please tell me please what exactlly to do ? additionally if compiled and executed FC.exe AntiVir reports it as TR/Dropper.Gen virus ! ?


Br,
Zoki

#7 Rapte_Of_Suzaku

Rapte_Of_Suzaku
  • Members
  • 901 posts

Posted 27 November 2010 - 03:37 PM

I always use Libraries of Functions: Standard Library and User Library when I download other people's libraries/objects... so I can't remember what needs done to run it directly! I'll get back to you in a bit on how to do it simply.

As for the antivirus thing, it's a false positive -- don't worry about it (or tell the AntiVir people it is a false positive so they fix it).

#8 Rapte_Of_Suzaku

Rapte_Of_Suzaku
  • Members
  • 901 posts

Posted 27 November 2010 - 03:45 PM

Actually, the simplest way would still be to just use Libraries of Functions: Standard Library and User Library.

So to get it to work for just my_path.ahk, take the "everything" files and put them in a folder named "lib" (such that "my_path.ahk" and "lib" are both in the same folder together). Then it should run fine. This way is the "standard library". The "user library" is for when you have lots of different scripts that might want to use FC.ahk.

#9 girlgamer

girlgamer
  • Moderators
  • 2039 posts

Posted 27 November 2010 - 03:47 PM

Could be any number of things happening to prevent your code from working. Off the top of my head I can list two...

1. FC requires a build of AHK that supports objects. That means, if you are using the original ahk, you may have problems with the modules
2. Did you include the libraries with the #Include directive in your program? If not. you won't get the functions to work.

Since you're getting an error indicating that FC() is a non existent function, it sounds like you're not linked, so you might want to check any include statement you have to see if it's mistyped. This assumes that a) you did #Include FC.AHK somewhere in your code and, B) it's on the right path.

#10 Rapte_Of_Suzaku

Rapte_Of_Suzaku
  • Members
  • 901 posts

Posted 27 November 2010 - 03:50 PM

:) I tried to use #Include, but it was too messy. FC.ahk has 3 dependencies, Container.ahk has 1. With all that, it's easier just to have a /lib/ folder in the same directory and skip messing with the #Include junk!

#11 girlgamer

girlgamer
  • Moderators
  • 2039 posts

Posted 27 November 2010 - 03:56 PM

Works for me. I like big code :) and if you think that's messy you should see my bedroom. (Or NOT)

#12 zoran

zoran
  • Members
  • 5 posts

Posted 27 November 2010 - 08:37 PM

Hi Rapte_Of_Suzaku,

Hmm I know C/C++/C# language but fast geting into AutoHotKey is not possible :shock:

I modified my_path.ahk file so that it display just selected file:

#a::

selection := FC("explorer","","selection")
MsgBox selection

return

1. I am getting Message Box but nothing is writen..any clue why?

2. Could you shortlly explain also the line:
run % "notepad.exe """ selection[A_Index] """"

what does it means "%" ? and what tripple """ ? the best would be link to hel file because entering just "%" in chm help bring no matches...

best regards,
Zoki

#13 Rapte_Of_Suzaku

Rapte_Of_Suzaku
  • Members
  • 901 posts

Posted 27 November 2010 - 08:55 PM

One important thing to get down fast is Variables and Expressions. The single % makes the part to the right be an expression.
the first will display 4 while the second will display "2+2":
MsgBox % 2+2
MsgBox 2+2

"" is how you put a quote in a string. so:
str:="this is a quote:"""

For your messagebox problem:
MsgBox % selection

this would also work:
MsgBox %selection%


#14 zoran

zoran
  • Members
  • 5 posts

Posted 28 November 2010 - 11:50 AM

Hi Rapte_Of_Suzaku,

the peace of the code bellow...still does not work ... do we have some new ideas ? :idea: or we run out of those :(

#a::
selection := FC("explorer","","selection")
MsgBox %selection%
return


I just get message box without any content.

best regards,
Zoki

#15 Learning one

Learning one
  • Members
  • 1295 posts

Posted 28 November 2010 - 01:16 PM

@Rapte_Of_Suzaku: could you extract relevant code from your dependencies, and make 1 function that will get full path of selected file(s) in Windows Explorer? I'm doing it on this way, but it would be nice to have pure COM solution (without using clipboard)