MsgBox - how to split a long string across lines only at '/'?

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
UserNameForAH_Board
Posts: 54
Joined: 02 Jan 2018, 15:55

MsgBox - how to split a long string across lines only at '/'?

05 May 2024, 07:35

Dear all

My question might be a silly one, but I am having trouble finding the answer to it.

I have a msgbox that displays a file path, and I want that path to be line-split only at the '/' character, i.e. the character that divides directories from each other (and the final directory from the leafname). How to do that, please?

Here is the sort of thing that I see currently and want to avoid:

[EDIT: the screenshots feature some instances of the wrong type of slash. But you get the idea.]
image.png
image.png (14.14 KiB) Viewed 536 times
By contrast, the following is fine (but I see that only because, here, no line splitting at all is necessary):
image.png
image.png (13.36 KiB) Viewed 536 times
User avatar
mikeyww
Posts: 27165
Joined: 09 Sep 2014, 18:38

Re: MsgBox - how to split a long string across lines only at '/'?

05 May 2024, 07:55

Code: Select all

#Requires AutoHotkey v2.0
str := 'asdf\aspldkfj\alspkdjf\palskjdf\alpsk\fapsldjf\/palskjfdp\laskdjf\palskdjfqqq\palskdjf\palskdjf\alpskdfj\asdkplf'
MsgBox StrReplace(str, '/', '/`n')
UserNameForAH_Board
Posts: 54
Joined: 02 Jan 2018, 15:55

Re: MsgBox - how to split a long string across lines only at '/'?

05 May 2024, 08:51

Thanks. That - or rather StrReplace(str, '\', '\`n') produces the likes of this:
image.png
image.png (10.73 KiB) Viewed 497 times
That is not exactly what I want. I will investigate `StrReplace` and see what I can come up with.

EDITED to fix formatting.
Last edited by UserNameForAH_Board on 05 May 2024, 09:12, edited 1 time in total.
User avatar
mikeyww
Posts: 27165
Joined: 09 Sep 2014, 18:38

Re: MsgBox - how to split a long string across lines only at '/'?

05 May 2024, 08:54

It seems that the script that I posted is the one that you want.

What the script does:
How to split a long string across lines only at '/'?
If not, you can use other characters, examine substrings, and check the length of these strings to act accordingly. If your string exceeds a certain length and contains a certain character in the substring that does not exceed that length, you can insert a line feed after the final occurrence of the character in the substring.
UserNameForAH_Board
Posts: 54
Joined: 02 Jan 2018, 15:55

Re: MsgBox - how to split a long string across lines only at '/'?

05 May 2024, 09:14

Thanks. Your code splits the string only and always at \. I do not want the always. But I think you've given me enough to work with. So, as I say: thank you.
User avatar
mikeyww
Posts: 27165
Joined: 09 Sep 2014, 18:38

Re: MsgBox - how to split a long string across lines only at '/'?

05 May 2024, 09:25

My StrReplace contains no backslashes, but if you have it working, then it's all fine.
User avatar
kunkel321
Posts: 1125
Joined: 30 Nov 2015, 21:19

Re: MsgBox - how to split a long string across lines only at '/'?

05 May 2024, 09:29

Also, depending on what you are trying to accomplish, this might be helpful.
https://www.autohotkey.com/docs/v2/lib/SplitPath.htm
ste(phen|ve) kunkel
User avatar
mikeyww
Posts: 27165
Joined: 09 Sep 2014, 18:38

Re: MsgBox - how to split a long string across lines only at '/'?

05 May 2024, 10:08

Code: Select all

#Requires AutoHotkey v2.0
str := 'asdf\aspldkfj\alspkdjf\palskjdf\alpsk\fapsldjf\/palskjfdp\'
     . 'laskdjf\palskdjfqqq\palskdjf\palskdjf\alpskdfj\asdkplf'
MsgBox split(str, 65), 'Output', 'Iconi'

split(str, maxi) {
 Static regex := '[/\\]'
 If StrLen(str) > maxi && str ~= regex {
  If !pos := RegExMatch(SubStr(str, 1, maxi), '.+\K' regex)
      pos := RegExMatch(str, regex)
  Return SubStr(str, 1, pos) '`n' split(SubStr(str, pos + 1), maxi)
 } Else Return str
}
User avatar
boiler
Posts: 17180
Joined: 21 Dec 2014, 02:44

Re: MsgBox - how to split a long string across lines only at '/'?

05 May 2024, 10:16

Another option: Instead of using a MsgBox, create a simple GUI to display your message so you don’t have to worry about it breaking in the wrong place because you can have it not break at all.
User avatar
mikeyww
Posts: 27165
Joined: 09 Sep 2014, 18:38

Re: MsgBox - how to split a long string across lines only at '/'?

05 May 2024, 10:25

An example of boiler's idea is below.

Code: Select all

#Requires AutoHotkey v2.0
str := 'asdf\aspldkfj\alspkdjf\palskjdf\alpsk\fapsldjf\/palskjfdp\'
     . 'laskdjf\palskdjfqqq\palskdjf\palskdjf\alpskdfj\asdkplf'
box(str)
MsgBox 'Done!', 'Status', 'Iconi'

box(str) {
 Try g.Destroy
 Global g := Gui('-MinimizeBox', 'Output')
 g.SetFont 's10'
 g.AddText 'y20', str
 btn := g.AddButton('w113 y+20 Default', 'OK')
 btn.OnEvent('Click', (btn, info) => btn.Gui.Destroy())
 g.Show('Hide'), g.GetPos(,, &gw), btn.GetPos(,, &bw), btn.Move((gw - bw) / 2)
 g.Show(), WinWaitClose(g)
}
UserNameForAH_Board
Posts: 54
Joined: 02 Jan 2018, 15:55

Re: MsgBox - how to split a long string across lines only at '/'?

05 May 2024, 12:16

Thanks, all.

I think I've got it working. I had a lot of trouble working out how the logic should be and I'm unsure I could sum up the logic that I have implemented! Here though is the relevant part of my code.

Code: Select all

local displayStr, maxLen

path_display := path

maxLen := 30

if StrLen(path_display) > maxLen {
	
	; response := MsgBox("String needs wrapping. Length: " . StrLen(path_display) . ". Str: " . path, progName, "YNC Icon?")
	; switch response {
	; 	case "No": return
	; 	case "Cancel": exitApp
	; }

	local n, position, workingStr

	n := 1
	workingStr := path_display

	; InStr syntax: FoundPos := InStr(Haystack, Needle , CaseSense, StartingPos, Occurrence)
	loop {
		if not position := InStr(path_display, '\' , , , n)
			break

		workingStr := SubStr(path_display, 1, position)

		; response := MsgBox("Iteration: " . n . ". Position: " . position . ". Working string: " . workingStr, progName, "YNC Icon?")
		; switch response {
		; 	case "No": return
		; 	case "Cancel": exitApp
		; }

		n++
	} until StrLen(workingStr) >= maxLen
	path_display := workingStr . "`n" . SubStr(path, position + 1)
}

; Make spaces in path-for-display non-breaking.
path_display := StrReplace(path_display, ' ', ' ')

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: No registered users and 43 guests