Menu Which Selects Paragraphs?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
AlFlo
Posts: 360
Joined: 29 Nov 2021, 21:46

Menu Which Selects Paragraphs?

13 May 2024, 23:52

I've created a little AHK clipboard manager. I am having trouble creating a MENU out of the data in a text file in which I store my clips to show PARAGRAPHS.

Specifically, this code works to show LINES in my menu:

Code: Select all

FilePath= C:\RunningListOClip.txt 
FileRead, Contents, %FilePath%
Sleep, 1000
Clipboard:= Contents
Sleep, 1000
Return

#z::

For each, line in StrSplit(Contents, "`r", "`n")
Menu, scripts, Add, %line%, Execute	
Menu, scripts, Show
Return

Execute:
SendInput {Text}%A_ThisMenuItem%
Return
But when I try to modify that script to display PARAGRAPHS - and let me click on a paragraph to paste a paragraph - it still only pastes one LINE at a time:

Code: Select all

FilePath= C:\RunningListOClip.txt 
FileRead, Contents, %FilePath%
Sleep, 1000
Clipboard:= Contents
Sleep, 1000
Return

#z::

For each, paragraph in StrSplit(Contents, "`r", "`n`n") ; HERE'S THE FIRST NEW LINE
Menu, scripts, Add, %paragraph%, Execute	 ; HERE'S THE SECOND NEW LINE
Menu, scripts, Show
Return

Execute:
SendInput {Text}%A_ThisMenuItem%
Return
Any ideas?

P.S. The text in my C:\RunningListOClip.txt textfile is currently:

[BeginningChronology]
[EndChronology]

Send !e
Sleep, 100
Send f
Sleep, 100
SendInput [EndChronology]
Sleep, 100
Send {Return}
Sleep, 2000
Send {Esc}
Sleep, 4000
Send {Back}
Sleep, 500

$583.76

5/13/2024 Refund of $583.76 from Account Due to Overpayment

So what I want to do is - if I click on the top menu item - it pastes:

"[BeginningChronology]
[EndChronology]"

If I click on the second menu item, it pastes:

"Send !e
Sleep, 100
Send f
Sleep, 100
SendInput [EndChronology]
Sleep, 100
Send {Return}
Sleep, 2000
Send {Esc}
Sleep, 4000
Send {Back}
Sleep, 500"

If I click on the third menu item, it pastes "$583.76"

And if click on the bottom menu item, it pastes "5/13/2024 Refund of $583.76 from Account Due to Overpayment"

Does that make sense?
Rohwedder
Posts: 7732
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Menu Which Selects Paragraphs?

14 May 2024, 01:48

Hallo,
perhaps?:

Code: Select all

FilePath= C:\RunningListOClip.txt 
FileRead, Contents, %FilePath%
Sleep, 1000
Clipboard:= Contents
Sleep, 1000
Return

#z::
For each, line in StrSplit(Contents, "`r", "`n")
Menu, scripts, Add, %line%, Execute	
Menu, scripts, Show
Return

Execute:
P1 := InStr(Contents, A_ThisMenuItem)
P2 := RegExMatch(Contents, "\R\R",, P1)
SendInput,% "{Text}" SubStr(Contents, P1, P2-P1)
Return
AlFlo
Posts: 360
Joined: 29 Nov 2021, 21:46

Re: Menu Which Selects Paragraphs?

14 May 2024, 11:06

Hallo Rohwedder! Thanks, your script is awesome. The one issue is that it can't paste the final line in textfile.

Here's what it looks like when I press Win Z:
Clipboard_05-14-2024_01.png
Clipboard_05-14-2024_01.png (11.94 KiB) Viewed 467 times
When I click the last line, it just pastes "$583.76". In other words, the menu SHOWS "$583.765/13/2024 Refund of $583.76 from Account Due to Overpayment" - jamming two lines together, "$583.76" and "5/13/2024 Refund of $583.76 from Account Due to Overpayment" - but then just pastes the second to last line "$583.76".
AlFlo
Posts: 360
Joined: 29 Nov 2021, 21:46

Re: Menu Which Selects Paragraphs?

14 May 2024, 11:51

I think I figured it out. By changing the way my other script appends the clipboard textfile to the following, your script now works like a champ:

Code: Select all

Var := "`n`n" . Clipboard . "`r`r"
FileAppend, %Var%, C:\RunningListOClip.txt
return
Thanks!
AlFlo
Posts: 360
Joined: 29 Nov 2021, 21:46

Re: Menu Which Selects Paragraphs?

14 May 2024, 13:30

Uh-oh, I'm getting a "Menu Item Name Too Long" error when I have much text in a paragraph in my textfile. Does anyone know how to fix that error?

I see that the normal maximum for menu item names is 260 characters. https://www.autohotkey.com/docs/v1/lib/Menu.htm. Is there a way to somehow increase that limit - or else have the script break my paragraphs up into 260-character paragraphs?

Or is the only way to be able to select and paste longer clips from a text file to use a Listbox? If so, how would I load the paragraphs in my textfile as list items in my listbox?
User avatar
CoffeeChaton
Posts: 35
Joined: 11 May 2024, 10:50

Re: Menu Which Selects Paragraphs?

15 May 2024, 10:05

Code: Select all


global bigMap := {} ; #1 https://www.autohotkey.com/docs/v1/Objects.htm#Usage_Associative_Arrays

#z::fn_win_z()

fn_win_z() {
    ; FilePath := "C:\RunningListOClip.txt"
    ; FileRead, Contents, %FilePath%

    ; mock big big file
    Contents := "
        (LTrim
            aa
            bb
            cc
            dd
            ee
            ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
        )"

    bigMap := {} ; clear
    For each, lineText in StrSplit(Contents, "`n")
    {
        short := StrLen(lineText) > 20 ? SubStr(lineText, 1, 20) : lineText
        bigMap[each] := lineText
        Menu, scripts, Add, % short, Execute 
    }
    Menu, scripts, Show
}


Execute() {
    lineText := bigMap[A_ThisMenuItemPos] ;#2 https://www.autohotkey.com/docs/v1/Variables.htm#ThisMenuItemPos
    SendInput, {Text}%lineText%
}



#1 https://www.autohotkey.com/docs/v1/Objects.htm#Usage_Associative_Arrays

Code: Select all

;Retrieve an item:
Value := Array[Key]

;Assign an item:
Array[Key] := Value
also can use array.

#2 A_ThisMenuItemPos https://www.autohotkey.com/docs/v1/Variables.htm#ThisMenuItemPos
AlFlo
Posts: 360
Joined: 29 Nov 2021, 21:46

Re: Menu Which Selects Paragraphs?

15 May 2024, 14:26

Thank you, CoffeeChaton! I'm still playing around with this, but the key value approach seems to be very helpful!!!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: AlFlo, Bing [Bot], VaritySpice and 120 guests