Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Can AutoHotKey retrieve file path of the selected file ?


  • Please log in to reply
30 replies to this topic
zoran
  • Members
  • 5 posts
  • Last active: Nov 28 2010 01:32 PM
  • Joined: 26 Nov 2010
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.

girlgamer
  • Moderators
  • 3263 posts
  • Last active: Feb 01 2015 09:49 AM
  • Joined: 04 Jun 2010
Take a look at
Loop, FilePattern [, IncludeFolders?, Recurse?]
Specifically the Special Variables Available Inside a File-Loop section of the AHK help file.

The universe is a wondrous place! The faster you create unbreakable code, the faster the universe creates people that can break it. All scripting follows the rule Rule Of Twos -- 1) Good, 2) Fast 3) Cheap -- pick any Two.
I guarantee absolutely nothing about any code I provide except that it works in my machine. ●
MMO Fighter   KeyLooperDemo   Key Spammer   TinyClickRecorder  GGs Password Generator.ahk
For the newest version of AutoHotkey and some killer scripts go here.
Rock-on%20kitten.gif


Learning one
  • Members
  • 1483 posts
  • Last active: Jan 02 2016 02:30 PM
  • Joined: 04 Apr 2009
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

}


Rapte_Of_Suzaku
  • Members
  • 901 posts
  • Last active: Jul 08 2011 02:12 PM
  • Joined: 29 Feb 2008
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.

girlgamer
  • Moderators
  • 3263 posts
  • Last active: Feb 01 2015 09:49 AM
  • Joined: 04 Jun 2010
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.

The universe is a wondrous place! The faster you create unbreakable code, the faster the universe creates people that can break it. All scripting follows the rule Rule Of Twos -- 1) Good, 2) Fast 3) Cheap -- pick any Two.
I guarantee absolutely nothing about any code I provide except that it works in my machine. ●
MMO Fighter   KeyLooperDemo   Key Spammer   TinyClickRecorder  GGs Password Generator.ahk
For the newest version of AutoHotkey and some killer scripts go here.
Rock-on%20kitten.gif


zoran
  • Members
  • 5 posts
  • Last active: Nov 28 2010 01:32 PM
  • Joined: 26 Nov 2010
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

Rapte_Of_Suzaku
  • Members
  • 901 posts
  • Last active: Jul 08 2011 02:12 PM
  • Joined: 29 Feb 2008
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).

Rapte_Of_Suzaku
  • Members
  • 901 posts
  • Last active: Jul 08 2011 02:12 PM
  • Joined: 29 Feb 2008
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.

girlgamer
  • Moderators
  • 3263 posts
  • Last active: Feb 01 2015 09:49 AM
  • Joined: 04 Jun 2010
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.

The universe is a wondrous place! The faster you create unbreakable code, the faster the universe creates people that can break it. All scripting follows the rule Rule Of Twos -- 1) Good, 2) Fast 3) Cheap -- pick any Two.
I guarantee absolutely nothing about any code I provide except that it works in my machine. ●
MMO Fighter   KeyLooperDemo   Key Spammer   TinyClickRecorder  GGs Password Generator.ahk
For the newest version of AutoHotkey and some killer scripts go here.
Rock-on%20kitten.gif


Rapte_Of_Suzaku
  • Members
  • 901 posts
  • Last active: Jul 08 2011 02:12 PM
  • Joined: 29 Feb 2008
:) 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!

girlgamer
  • Moderators
  • 3263 posts
  • Last active: Feb 01 2015 09:49 AM
  • Joined: 04 Jun 2010
Works for me. I like big code :) and if you think that's messy you should see my bedroom. (Or NOT)

The universe is a wondrous place! The faster you create unbreakable code, the faster the universe creates people that can break it. All scripting follows the rule Rule Of Twos -- 1) Good, 2) Fast 3) Cheap -- pick any Two.
I guarantee absolutely nothing about any code I provide except that it works in my machine. ●
MMO Fighter   KeyLooperDemo   Key Spammer   TinyClickRecorder  GGs Password Generator.ahk
For the newest version of AutoHotkey and some killer scripts go here.
Rock-on%20kitten.gif


zoran
  • Members
  • 5 posts
  • Last active: Nov 28 2010 01:32 PM
  • Joined: 26 Nov 2010
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

Rapte_Of_Suzaku
  • Members
  • 901 posts
  • Last active: Jul 08 2011 02:12 PM
  • Joined: 29 Feb 2008
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%


zoran
  • Members
  • 5 posts
  • Last active: Nov 28 2010 01:32 PM
  • Joined: 26 Nov 2010
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

Learning one
  • Members
  • 1483 posts
  • Last active: Jan 02 2016 02:30 PM
  • Joined: 04 Apr 2009
@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)