write title/name of active window to clipboard

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
differentflies
Posts: 3
Joined: 02 Feb 2018, 14:13

write title/name of active window to clipboard

Post by differentflies » 02 Feb 2018, 14:50

[Mod edit: Topic moved to 'Ask For Help'.]

Thanks in advance for any help on this.

My objective is to use a keystroke combination (e.g. CTRL+SHIFT+W) to write the name/title of the active window to the clipboard.

In order to do this, I'd like a very simple .exe which i will then create a shortcut of; i will then assign the keystroke combination to the shortcut file (through the 'Properties' dialogue) in order to be able to execute the script without deactivating the window whose name/title i want to be able to CTRL+V paste elsewhere.

For example, I'm working in an excel file with a long name; I'd like to be able to hit CTRL+SHIFT+W and then switch to an email and CTRL+V paste the name of that file to tell someone what file I'm referencing or working in. This would be extremely helpful while working in Word and PDF files. I think the filename is practically the title of excel, word, and adobe reader windows. Not really concerned about other software window titles.

I scoured the web for a solution and found this, but I have not been able to make it work:

Code: Select all

^!l::
WinGetActiveTitle, Title
Clipboard = %Title%
return
Again, thanks very much in advance for your help!

User avatar
boiler
Posts: 16774
Joined: 21 Dec 2014, 02:44

Re: write title/name of active window to clipboard

Post by boiler » 03 Feb 2018, 06:44

Welcome to the forum. This should be posted in Ask For Help, not Scripts and Functions.

There is no need to turn this into a .exe file and create a shortcut through Windows. Setting a hotkey directly in your script will do what you want. It won't steal focus from the active window.

The script you found should work. Are you activating it by pressing Ctrl+Alt+l? (that's the letter L not the number 1). First you run the script file, then you press that hotkey combination anytime you want to copy the active window title to the clipboard. You can make it work exactly as you want by changing the fist line to:
^+w::

differentflies
Posts: 3
Joined: 02 Feb 2018, 14:13

Re: write title/name of active window to clipboard

Post by differentflies » 04 Feb 2018, 22:57

Boiler, thanks for the reply. I do have more questions about this, but will move it to the 'ask for help' section...

LAPIII
Posts: 667
Joined: 01 Aug 2021, 06:01

Re: write title/name of active window to clipboard

Post by LAPIII » 03 Jan 2022, 09:55

@boiler I'm trying to get the title as Window Spy would. I think this is the code I need but I don't know how to implement it:

Code: Select all

WinGetTitle, title, ahk_id %id%

User avatar
boiler
Posts: 16774
Joined: 21 Dec 2014, 02:44

Re: write title/name of active window to clipboard

Post by boiler » 03 Jan 2022, 10:32

That would work if you already have the ID (HWND) of the window. How is that being identified? Or how else are you identifying the window that you want the title of? If you just want the title of the active window, this code will display the title of whichever window is active when you press the Tab key:

Code: Select all

Tab::
	WinGetTitle, title, A
	ToolTip, % title
return

LAPIII
Posts: 667
Joined: 01 Aug 2021, 06:01

Re: write title/name of active window to clipboard

Post by LAPIII » 03 Jan 2022, 10:48

Sorry, I meant the line I a rectangle around here:

Image

User avatar
boiler
Posts: 16774
Joined: 21 Dec 2014, 02:44

Re: write title/name of active window to clipboard

Post by boiler » 03 Jan 2022, 11:38

You can get the process name like this:

Code: Select all

Tab::
	WinGet, ProcName, ProcessName, A
	ToolTip, % ProcName
return

LAPIII
Posts: 667
Joined: 01 Aug 2021, 06:01

Re: write title/name of active window to clipboard

Post by LAPIII » 20 Jan 2022, 01:39

Thank You @boiler. I've been trying to figure out how to get the PID of a window. but haven't been successful. Also, I want It to be written in a Message box.

User avatar
boiler
Posts: 16774
Joined: 21 Dec 2014, 02:44

Re: write title/name of active window to clipboard

Post by boiler » 20 Jan 2022, 05:41

LAPIII wrote: Also, I want It to be written in a Message box.
In the code above, change where it says ToolTip to MsgBox.

LAPIII
Posts: 667
Joined: 01 Aug 2021, 06:01

Re: write title/name of active window to clipboard

Post by LAPIII » 20 Jan 2022, 10:18

I meant the process number under the process in Windows Spy. How do you add text to a Message box with an expression?

[EDIT] What are the tags called?
Last edited by LAPIII on 20 Jan 2022, 10:33, edited 1 time in total.

User avatar
boiler
Posts: 16774
Joined: 21 Dec 2014, 02:44

Re: write title/name of active window to clipboard

Post by boiler » 20 Jan 2022, 10:28

Code: Select all

Tab::
	WinGet, PID, PID, A
	MsgBox, % "Process ID: " PID
return

LAPIII
Posts: 667
Joined: 01 Aug 2021, 06:01

Re: write title/name of active window to clipboard

Post by LAPIII » 20 Jan 2022, 10:36

I just had to change the hotkey. And I still can't write anything in the message box, Can you show me?
Last edited by LAPIII on 20 Jan 2022, 11:26, edited 3 times in total.

User avatar
boiler
Posts: 16774
Joined: 21 Dec 2014, 02:44

Re: write title/name of active window to clipboard

Post by boiler » 20 Jan 2022, 11:10

I’m not sure what you’re asking. What do you want the MsgBox to show?

LAPIII
Posts: 667
Joined: 01 Aug 2021, 06:01

Re: write title/name of active window to clipboard

Post by LAPIII » 20 Jan 2022, 11:18

I want the numberto also be copied to the clipboard and of the message box to read:

Here is the process number _____ and it's been copied to the clipboard.

User avatar
boiler
Posts: 16774
Joined: 21 Dec 2014, 02:44

Re: write title/name of active window to clipboard

Post by boiler » 20 Jan 2022, 11:20

Code: Select all

Tab:: ; change to your desired hotkey
	WinGet, PID, PID, A
	Clipboard := PID
	MsgBox, % "Here is the process number " PID " and it's been copied to the clipboard."
return

User avatar
boiler
Posts: 16774
Joined: 21 Dec 2014, 02:44

Re: write title/name of active window to clipboard

Post by boiler » 20 Jan 2022, 11:24

LAPIII wrote: [EDIT] What are the tags [ C ] [ /C ] called?
They are called "inline code tags."

User avatar
boiler
Posts: 16774
Joined: 21 Dec 2014, 02:44

Re: write title/name of active window to clipboard

Post by boiler » 20 Jan 2022, 11:32

Are you aware that the process number changes each time the application is run? For that reason, there is usually no reason to know what the actual number is. A script getting and using it dynamically can be useful, but the actual number it happens to be at any one time generally is not. If you were to copy that number and put it into a script, it would not be the PID for that program the next time it is run.

Post Reply

Return to “Ask for Help (v1)”