Create a drop-down menu that copies to the clipboard

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
hello123789
Posts: 10
Joined: 21 May 2023, 07:43

Create a drop-down menu that copies to the clipboard

Post by hello123789 » 27 May 2023, 14:41

I've created a pop-up program, and now I want to add a drop-down menu on top of the Draft Window at the position it is in the code below. Specifically, when I choose something from this drop-down menu I want a specific text to get copied to the clipboard. For example when I choose A from the list the text "It's a good day" is copied to the clipboard.

I also have a request that I want the code layout to be as clear/simple as possible as I have plans on adding 20+ different texts (long paragraphs) in this drop-down menu. Maybe it's possible adding this portion of the code (the text it copies to the clipboard) right before ExitApp at the end of the code? So it doesn't muddle up the main code.
Or perhaps even saving these 20+ long paragraphs in a separate .ahk file which I can point to, and later on compile to an .exe file? Please show me the code for both alternatives if you can.

I also want separator lines or the likes in the drop-down choices to separate the 20+ different texts in categories, I assume the lines I've used in the code below is the best one can do?

Code: Select all

#NoEnv
#SingleInstance force
#Persistent
 
; Version 3.1
 
; Grab active window
WinGetActiveTitle, activeWinTitle
 
; Create popup-window
Gui, +AlwaysOnTop
Gui, Add, Text, x18 y7 w200 h20, ID:
Gui, Add, Edit, vID x18 y30 w190 h20
Gui, Add, Button, x20 y630 w90 h30 gCopy, Copy

; Add drop-down menu for prepared written text
Gui, Add, DropDownList, vPreparedText x260 y30 w300 h90, |A|B|-------------|C|D

 
Gui, Show, w590 h690,
  
; size of main window
MainWinWidth := 320
MainWinHeight := 580
 
; Create popup for Draft window besides main window.
Gui, +AlwaysOnTop
Gui, Add, Edit, x%MainWinWidth%+10 y32 x260 w%MainWinWidth% h%MainWinHeight%,
return
 

; Function that runs when the button "Copy" is pressed
Copy:
Gui, Submit, NoHide
Clipboard =
(
ID:
%ID%
)
 
; show confirmation popup
MsgBox, 64, Copied, Copy successful! 
return
  
; Function to be executed when the GUI window is closed
GuiClose:
ExitApp
return

User avatar
mikeyww
Posts: 26851
Joined: 09 Sep 2014, 18:38

Re: Create a drop-down menu that copies to the clipboard

Post by mikeyww » 27 May 2023, 17:18

Code: Select all

#Requires AutoHotkey v1.1.33
txt := {A: "It's a good day"
      , B: "It's a bad night"}
For abbrev in txt
 list .= abbrev "|"
Gui +AlwaysOnTop
Gui Font, s10
Gui Add, DDL   , w300 gCheck vsel, % list
Gui Add, Button, wp Default Disabled, Copy
Gui Show, NoActivate, Phrases
Return

Check:
GuiControl Enable, Copy
Return

ButtonCopy:
Gui Submit, NoHide
Clipboard := txt[sel]
Return

hello123789
Posts: 10
Joined: 21 May 2023, 07:43

Re: Create a drop-down menu that copies to the clipboard

Post by hello123789 » 28 May 2023, 16:11

You have misunderstood me. I want to add the function of "copy to clipboard" to my existing code when choosing A/B/C from the drop-down menu. So the GUI, buttons etc is already there in the code I provided, except the function of copy by click. And I want it to copy to clipboard right when I click on A, B or C in the DDL, not using the Copy button (which is already tied to the editable fields, as intended).

Care to give it another crack?

mikeyww wrote:
27 May 2023, 17:18

Code: Select all

#Requires AutoHotkey v1.1.33
txt := {A: "It's a good day"
      , B: "It's a bad night"}
For abbrev in txt
 list .= abbrev "|"
Gui +AlwaysOnTop
Gui Font, s10
Gui Add, DDL   , w300 gCheck vsel, % list
Gui Add, Button, wp Default Disabled, Copy
Gui Show, NoActivate, Phrases
Return

Check:
GuiControl Enable, Copy
Return

ButtonCopy:
Gui Submit, NoHide
Clipboard := txt[sel]
Return

hello123789
Posts: 10
Joined: 21 May 2023, 07:43

Re: Create a drop-down menu that copies to the clipboard

Post by hello123789 » 28 May 2023, 16:17

Maybe the DDL wasn't visible to understand what I was referring to. So I've moved it up a bit (before it blended in to the Draft Window).

Code: Select all

#NoEnv
#SingleInstance force
#Persistent
 
; Version 3.1
 
; Grab active window
WinGetActiveTitle, activeWinTitle
 
; Create popup-window
Gui, +AlwaysOnTop
Gui, Add, Text, x18 y7 w200 h20, ID:
Gui, Add, Edit, vID x18 y30 w190 h20
Gui, Add, Button, x20 y630 w90 h30 gCopy, Copy

; Add drop-down menu for prepared written text
Gui, Add, DropDownList, vPreparedText x260 y30 w300 h90, |A|B|-------------|C|D

 
Gui, Show, w590 h690,
  
; size of main window
MainWinWidth := 320
MainWinHeight := 580
 
; Create popup for Draft window besides main window.
Gui, +AlwaysOnTop
Gui, Add, Edit, x%MainWinWidth%+10 y52 x260 w%MainWinWidth% h%MainWinHeight%,
return
 

; Function that runs when the button "Copy" is pressed
Copy:
Gui, Submit, NoHide
Clipboard =
(
ID:
%ID%
)
 
; show confirmation popup
MsgBox, 64, Copied, Copy successful! 
return
  
; Function to be executed when the GUI window is closed
GuiClose:
ExitApp
return

User avatar
mikeyww
Posts: 26851
Joined: 09 Sep 2014, 18:38

Re: Create a drop-down menu that copies to the clipboard

Post by mikeyww » 28 May 2023, 17:02

Your DDL can have a g-label such as gCopy. Read: https://www.autohotkey.com/docs/v1/lib/GuiControls.htm#DropDownList. The g-label subroutine would then submit the GUI and copy something to the clipboard.

hello123789
Posts: 10
Joined: 21 May 2023, 07:43

Re: Create a drop-down menu that copies to the clipboard

Post by hello123789 » 30 May 2023, 20:01

^I got it to copy from the DDL! 8-) Care to check if it's correct? Do I just continue to add under Copy2...Clipboard = for every option in the Drop Down list? Or do I have to create a copy3 and so on for every option in the DDL?

Update: as soon as I started to add more under Copy2...Clipboard = only the latest/last add in that snippet copies, all that came before it doesn't copy anymore. What's missing from the code for it to successfully copy only one option, and not every option, from the DDL?

Copies to clipboard what I choose in DDL:

Code: Select all

; Add drop-down menu for prepared written text
Gui, Add, DropDownList, gCopy2 sort x260 y30 w300 h90, |A|B|-------------|C|D

; Function that runs when I choose from the DDL
Copy2:
Gui, Submit, NoHide
Clipboard =
(
A
%A%
)

User avatar
mikeyww
Posts: 26851
Joined: 09 Sep 2014, 18:38

Re: Create a drop-down menu that copies to the clipboard

Post by mikeyww » 30 May 2023, 20:28

If you would like to retrieve the value of your DDL, use a variable when you add the DDL control. An example can be found on line 17 of your previous script. After you submit the GUI, you can access that variable. An example of how to access the variable is found in the script that I posted.

Code: Select all

#Requires AutoHotkey v1.1.33
Gui Add, DDL, w230 gUpdate vsel, A|B|C
Gui Show,, DDL
Return

Update:
Gui Submit, NoHide
MsgBox % sel
Return

hello123789
Posts: 10
Joined: 21 May 2023, 07:43

Re: Create a drop-down menu that copies to the clipboard

Post by hello123789 » 31 May 2023, 13:06

I have literally been reading for the last 2 hours from the AHK manual, and I have picked up a few things about how the code structure is layed out, but I still have no idea what you're saying (I'm just two weeks in to this! :D).

Can't you just provide with a concrete example that is actually aligned to the function I request? You're giving me examples of functions I'm not requesting (why gUpdate? why Msgbox?!), and that just confuses me further.

Gui, Add, DropDownList, gCopy2 sort x260 y30 w300 h90, |A|B|-------------|C|D
Was my gcopy2 variable not acceptable for some reason? What was wrong with the first line? Why remove my Sort command and size and position data? Now you're just making me doubt even the tiniest thing I'v'e learnt. What could've been changed to make it work instead of changing to a completely new line which doesn't mean anything to me.

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

Re: Create a drop-down menu that copies to the clipboard

Post by boiler » 31 May 2023, 13:21

hello123789 wrote: Was my gcopy2 variable not acceptable for some reason? What was wrong with the first line? Why remove my Sort command and size and position data? Now you're just making me doubt even the tiniest thing I'v'e learnt. What could've been changed to make it work instead of changing to a completely new line which doesn't mean anything to me.
If changing from gcopy2 to gUpdate confuses you, then you really haven't learned what you said you have learned, and it's a good thing to demonstrate it on a (barely) more abstract level by showing the same structure with different variables. Also, a much simpler overall script helps isolate and illustrate the concept better than it being buried in a more complex script. You may have your preference on how you would have liked it presented, but don't frame it as you were wronged here somehow.

User avatar
mikeyww
Posts: 26851
Joined: 09 Sep 2014, 18:38

Re: Create a drop-down menu that copies to the clipboard

Post by mikeyww » 31 May 2023, 19:38

I agree completely, and it raises the question: who or what wrote the script that you posted? Did you write it? If so, do you understand what you wrote? If so, what did you intend to do with the variable PreparedText in that script?

gCopy2 is not a variable but a g-label. Did you read about g-labels? Do you understand what they do?

Your answers to all of these questions may clarify a lot.

As noted, my script is a demonstration of what you appear to be trying to accomplish, from what I could tell. If you ran the script and it didn't work, then describe what happened. If you have a question about the script, you are welcome to ask it.

What the script demonstrates:
1. How to show a dropdown list.
2. How to execute a subroutine when the selection changes.
3. How to retrieve the selected item from the list.

Some of this actually does reveal the bug in your own script. Although you wrote, "Copies to clipboard what I choose in DDL", this seems impossible to accomplish without providing a variable name in the "Add" statement, or without using ControlGet or GuiControlGet. My script addresses this by including the variable name in the "Add" statement.

As you can see, my script has nine lines. I do think it would be useful and also efficient to examine that short script, run it, and understand the nine lines. That is why I posted it. At that point, if you succeed, you would also be able to succeed in fixing the bugs in your longer script.

If ChatGPT wrote your script, then I think there is nothing more to be said here, other than that the forum does not support ChatGPT output.

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

Re: Create a drop-down menu that copies to the clipboard

Post by boiler » 31 May 2023, 20:15

mikeyww wrote: If ChatGPT wrote your script, then I think there is nothing more to be said here, other than that the forum does not support ChatGPT output.
Now that you mention it, it looks pretty clear that you are right based on some telltale signs of that script that give it away as ChatGPT-generated (which I won't say what they are and clue others in on what they are).

Post Reply

Return to “Ask for Help (v1)”