I would like to copy a file name and the file type to the clipboard. When I click on Job22.txt, only Job22 is highlighted, I would like the entire Job22.txt to be highlighted and copied to the clipboard. I am new to this and tried the script below, it doesnt work very well. Thanks.
F2::Send {F2}^a::^c
Copy file name and file type Topic is solved
Re: Copy file name and file type
Try this - just highlight the file (single click, not doubleclick) and press F2:
Code: Select all
F2::
Clipboard := ; empty clipboard
Send ^c ; copy via Ctrl+c
Clipwait ; Wait until Clipboard holds contents
SplitPath, Clipboard, name ; get filename - clipboard holds complete file path
msgbox % name ; just for testing
clipboard := name ; assign filename to clipboard for pasting
return
Re: Copy file name and file type
gregster wrote:Try this - just highlight the file (single click, not doubleclick) and press F2:Code: Select all
F2:: Clipboard := ; empty clipboard Send ^c ; copy via Ctrl+c Clipwait ; Wait until Clipboard holds contents SplitPath, Clipboard, name ; get filename - clipboard holds complete file path msgbox % name ; just for testing clipboard := name ; assign filename to clipboard for pasting return
This works great, just what I wanted. Thanks.
Re: Copy file name and file type
gregster wrote:Try this - just highlight the file (single click, not doubleclick) and press F2:Code: Select all
F2:: Clipboard := ; empty clipboard Send ^c ; copy via Ctrl+c Clipwait ; Wait until Clipboard holds contents SplitPath, Clipboard, name ; get filename - clipboard holds complete file path msgbox % name ; just for testing clipboard := name ; assign filename to clipboard for pasting return
Hmmm, this worked yesterday but not today. I have reloaded the script several times and even when back and copied the original script again.
When I single click the file name, today, I get a pop-up and the file is not on the clipboard. When I click and highlight the name I get the same pop-up. Please advise. Thanks.
Re: Copy file name and file type
It still works for me - and I don't see why it wouldn't. We are talking about windows' file explorer, right?
Do you have other scripts running? Or several instances? That might cause problems... Then you could add #SingleInstance force at the top of your script (above F2::)
If you are not sure, look at the task manager and count how much Autohotkey.exe processes you are seeing...
Do you have other scripts running? Or several instances? That might cause problems... Then you could add #SingleInstance force at the top of your script (above F2::)
If you are not sure, look at the task manager and count how much Autohotkey.exe processes you are seeing...
Re: Copy file name and file type
You could also add a timeout to Clipwait, like Clipwait,1 in case the Clipwait gets stuck (can happen, if you hit F2 without having anything highlighted). In case of timeout you would see an empty msgbox (but that would be correct, because nothing was selected).
Without a timeout parameter added, there shouldn't be an empty msgbox (well, in case there are unintentionally multiple script instances running, it could probably appear, I guess)
So, I would add the timeout to Clipwait and remove the msgbox (it was only for testing anyway) - and add #SingleInstance force, of course.
Without a timeout parameter added, there shouldn't be an empty msgbox (well, in case there are unintentionally multiple script instances running, it could probably appear, I guess)
So, I would add the timeout to Clipwait and remove the msgbox (it was only for testing anyway) - and add #SingleInstance force, of course.
- divanebaba
- Posts: 805
- Joined: 20 Dec 2016, 03:53
- Location: Diaspora
Re: Copy file name and file type
Try this
Code: Select all
F2::
Clipboard := "" ; empty clipboard
Send ^c ; copy via Ctrl+c
sleep 50 ; <---- this should prevent clipwait issues
Clipwait,1 ; Wait until Clipboard holds contents
if (ErrorLevel = 1) ; Zero if clipboard not empty, else one
return
SplitPath, Clipboard, name ; get filename - clipboard holds complete file path
msgbox % name ; just for testing
clipboard := name ; assign filename to clipboard for pasting
return
Einfach nur ein toller Typ.
Re: Copy file name and file type
Plain guessing, but have a try at assigning another key. I've faced the same thing here.JMD wrote: Hmmm, this worked yesterday but not today. I have reloaded the script several times and even when back and copied the original script again.
When I single click the file name, today, I get a pop-up and the file is not on the clipboard. When I click and highlight the name I get the same pop-up. Please advise. Thanks.
Btw, you can shorten this ...
Code: Select all
msgbox % name ; just for testing
clipboard := name ; assign filename to clipboard for pasting
Code: Select all
msgbox % clipboard := name ; assign filename to clipboard for pasting
Spoiler
Re: Copy file name and file type
Since the original request was just for loading the clipboard (to be ready for pasting) and the msgbox was just added (by me) for testing, I didn't consider msgbox % clipboard := name a good idea Of course, if the msgbox should stay, then it is possible.
But that last section could still be shortened to SplitPath, clipboard, clipboard
But that last section could still be shortened to SplitPath, clipboard, clipboard
Re: Copy file name and file type
Ooops. Haven't realized that the code I wanted to refer to wasn't from JMD himself but quoted from yougregster wrote:Since the original request was just for loading the clipboard (to be ready for pasting) and the msgbox was just added (by me) for testing, I didn't consider msgbox % clipboard := name a good idea Of course, if the msgbox should stay, then it is possible.
But that last section could still be shortened to SplitPath, clipboard, clipboard
You're damn right in every regard. Thx for your support muchacho
Re: Copy file name and file type
gregster, I do have one other script running so I added #SingleInstance force above F2:: I still got the msgbox with the name of the file. The name was in the clipboard to paste but it did not include the file type. And yes I am using Windows file explorer.
divanebaba, I tried your code because it had the Clipwait,1. I got a msgbox that said filename.ahk
BoBo, I assigned several other F keys. I still got the msgbox with the name of the file. The name was in the clipboard to paste but it did not include the filetype.
I also tried the script you had under Hide button, I got a msgbox that said C:\Program Files\dir\subdir
Thanks for all the help, I am still open to more ideas.
divanebaba, I tried your code because it had the Clipwait,1. I got a msgbox that said filename.ahk
BoBo, I assigned several other F keys. I still got the msgbox with the name of the file. The name was in the clipboard to paste but it did not include the filetype.
I also tried the script you had under Hide button, I got a msgbox that said C:\Program Files\dir\subdir
Thanks for all the help, I am still open to more ideas.
Re: Copy file name and file type
One more Idea. Select a file in any explorer window and press q.
Code: Select all
q::
fileselected := Explorer_GetSelection()
SplitPath,fileselected, fn
MsgBox % "FileName :`t" fn "`nFullName :`t" fileselected
return
Explorer_GetSelection(hwnd="") {
WinGet, process, processName, % "ahk_id" hwnd := hwnd? hwnd:WinExist("A")
WinGetClass class, ahk_id %hwnd%
if (process = "explorer.exe")
if (class ~= "Progman|WorkerW") {
ControlGet, files, List, Selected Col1, SysListView321, ahk_class %class%
Loop, Parse, files, `n, `r
ToReturn .= A_Desktop "\" A_LoopField "`n"
} else if (class ~= "(Cabinet|Explore)WClass") {
for window in ComObjCreate("Shell.Application").Windows
if (window.hwnd==hwnd)
sel := window.Document.SelectedItems
for item in sel
ToReturn .= item.path "`n"
}
return Trim(ToReturn,"`n")
}
____________________________________________________________________________
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
Re: Copy file name and file type Topic is solved
@JMD, so you are actually looking for the file type - more exactly, what is displayed in the "type" column of file explorer and not (only) for the filename inluding extension (like .ahk or .txt?), right?I still got the msgbox with the name of the file. The name was in the clipboard to paste but it did not include the filetype.
I thought by "type" you meant the file extension
Something like filename.ahk AutoHotkey Script ? That cannot be done by my script, but I think Odlanir's version could be changed to do that, by accessing theitem.type property of the Shell.Application object.
But first confirm that you are looking for this! Give us an example output!
Re: Copy file name and file type
gregster, My bad, wrong choice of words, file extension is what I want. When I click on Job22.txt, only Job22 is highlighted, I would like the entire Job22.txt to be highlighted and copied to the clipboard.
Odlanir, when I tried your script I got an msgbox that said:
AutoHotke;.ahk
Error at line 43.
Line Text: for window in ComObjCreate("Shel|.App|ication").Window
Error: This line does not contain a recognized action.
The program will exit.
Thanks.
Odlanir, when I tried your script I got an msgbox that said:
AutoHotke;.ahk
Error at line 43.
Line Text: for window in ComObjCreate("Shel|.App|ication").Window
Error: This line does not contain a recognized action.
The program will exit.
Thanks.
Re: Copy file name and file type
Edit: OK, forget the following, I see you are not interested in file type.
Are looking for something like this? Highlight one file and Hit 'q':
Note: For clarification purposes, I quickly "doctored" Odlanir's code (sorry, Odlanir) to display the filename with extension, followed by a space and then the "type" from the corresponding file explorer column, so that msgbox output would be something like this: filename.ahk AutoHotkey Script.
Is that what you want, JMD?
Odlanir's original function is actually more sophisticated and can work with more than one selected file. I only mutilated it for demonstration. If that goes in the right direction, the original function could probably be adapted in a much better way.
Are looking for something like this? Highlight one file and Hit 'q':
Code: Select all
q::
item := Explorer_GetSelection()
Splitpath, % item.path , filename
MsgBox % filename " " item.type
return
Explorer_GetSelection(hwnd="") {
WinGet, process, processName, % "ahk_id" hwnd := hwnd? hwnd:WinExist("A")
WinGetClass class, ahk_id %hwnd%
if (process = "explorer.exe")
if (class ~= "Progman|WorkerW") {
ControlGet, files, List, Selected Col1, SysListView321, ahk_class %class%
Loop, Parse, files, `n, `r
ToReturn .= A_Desktop "\" A_LoopField "`n"
} else if (class ~= "(Cabinet|Explore)WClass") {
for window in ComObjCreate("Shell.Application").Windows
if (window.hwnd==hwnd)
sel := window.Document.SelectedItems
for item in sel
ToReturn .= item.path "`n"
}
return item
}
Is that what you want, JMD?
Odlanir's original function is actually more sophisticated and can work with more than one selected file. I only mutilated it for demonstration. If that goes in the right direction, the original function could probably be adapted in a much better way.
Last edited by gregster on 01 Mar 2018, 11:39, edited 1 time in total.
Re: Copy file name and file type
Well, that is what my script does - complete filename, including extension. At least on my system. For clarification, do you see the file extensions in file explorer or is it disabled via display options? Well, for me it works both ways, anyway, with visible extensions and without. So, that shouldn't be a problem.JMD wrote:gregster, My bad, wrong choice of words, file extension is what I want. When I click on Job22.txt, only Job22 is highlighted, I would like the entire Job22.txt to be highlighted and copied to the clipboard.
The Splitpath docs also seem to confirm me: https://autohotkey.com/docs/commands/SplitPath.htm
Odlanir's script should also deliver the data that you want. It should work, I tried it.OutFileName
Name of the variable in which to store the file name without its path. The file's extension is included.
In the next step, you will just have to put the filename into the clipboard variable, like in my script. But the error message is a bit strange. Line 43? Can you show your complete script? Make sure you copy the complete code from the code box.
Strange things seem to be going on, on your end
Re: Copy file name and file type
Stop! You're only allowed to check this code if you're a bloody noob - and not able to deal with the already provided rocket science!
Spoiler
Re: Copy file name and file type
gregster wrote:Well, that is what my script does - complete filename, including extension. At least on my system. For clarification, do you see the file extensions in file explorer or is it disabled via display options? Well, for me it works both ways, anyway, with visible extensions and without. So, that shouldn't be a problem.JMD wrote:gregster, My bad, wrong choice of words, file extension is what I want. When I click on Job22.txt, only Job22 is highlighted, I would like the entire Job22.txt to be highlighted and copied to the clipboard.
The Splitpath docs also seem to confirm me: https://autohotkey.com/docs/commands/SplitPath.htm
Odlanir's script should also deliver the data that you want. It should work, I tried it.OutFileName
Name of the variable in which to store the file name without its path. The file's extension is included.
In the next step, you will just have to put the filename into the clipboard variable, like in my script. But the error message is a bit strange. Line 43? Can you show your complete script? Make sure you copy the complete code from the code box.
Strange things seem to be going on, on your end
I have other items in my AutoHotkey.ahk file so I am coping your script into notepad. I named it gregster.ahk. So what I click on to activate your script is the only thing in my code box [I think I am using the term code box properly...]. Below is the latest from you and I get an error when I click on the file to activate it.
The error is first, your code is next. Thanks.
Error at line 3.
LineText: % item.path
Error: This parameter contains a variable name missing its ending percent sign.
The program will exit.
q::
item := Explorer_GetSelection()
Splitpath, % item.path, filename
MsgBox % filename " " item.type
return
Explorer_GetSelection(hwnd="") {
WinGet, process, processName, % "ahk_id" hwnd := hwnd? hwnd:WinExist("A")
WinGetClass class, ahk_id %hwnd%
if (process = "explorer.exe")
if (class ~= "Progman|WorkerW") {
ControlGet, files, List, Selected Col1, SysListView321, ahk_class %class%
Loop, Parse, files, `n, `r
ToReturn .= A_Desktop "\" A_LoopField "`n"
} else if (class ~= "(Cabinet|Explore)WClass") {
for window in ComObjCreate("Shell.Application").Windows
if (window.hwnd==hwnd)
sel := window.Document.SelectedItems
for item in sel
ToReturn .= item.path "`n"
}
return item
}
Re: Copy file name and file type
BoBo wrote:Stop! You're only allowed to check this code if you're a bloody noob - and not able to deal with the already provided rocket science!Spoiler
BoBo, I have other items in my AutoHotkey.ahk file so I am coping your script into notepad. I named it BoBo.ahk. When I hit F2 it looks like it copies the file name and extension, but when I ctrl-V to paste, it only shows the file name, not the extension. Thanks.
Re: Copy file name and file type
@JMD: My guess is that you are using an archaic version of AutohotKey, something like 1.0.x. Am I right?
Windows version would be interesting, too.
(I am using AHK 1.1.28.00 Unicode on Windows 10 64 bit - but Win 7 or 8 should at least be ok, too.)
Windows version would be interesting, too.
(I am using AHK 1.1.28.00 Unicode on Windows 10 64 bit - but Win 7 or 8 should at least be ok, too.)
Last edited by gregster on 01 Mar 2018, 13:15, edited 1 time in total.