AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

? How can I put a newline (`n) in MsgBox

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
gb
Guest





PostPosted: Tue May 03, 2005 9:39 am    Post subject: ? How can I put a newline (`n) in MsgBox Reply with quote

I have a little "inspirational messages" script (one which I adapted from a random tag line script that someone published to these forums - sorry I don't remember who it was!).

The script that runs on startup and displays a message box with a quote or one-liner that it reads from a file.

However, I cannot seem to put a newline (`n) in the text file to cause the message box to break a long line onto a new line.

Is there a way to make this work in MsgBox with a variable which has read its content from a file? I tried all kinds of things, but maybe I'm just missing something small.

This is my script:
Code:

; Look up an inspirational message
; Display it in a message box
; AHK ver 1.x

; Set counter to zero
Count = 0

; Read message lines from file into an array and increment the counter

Loop Read, C:\AHK Work\Messages\messages.txt
{
line%a_index% = %A_LoopReadLine%
Count++
}
; Choose a random line
random choice, 1, %Count%
; extract chosen line from the array
stringtrimleft result, line%choice%,0
; Display the message
MsgBox, 4160,, %Result%

; Quit
Exit


And here are a few lines from my text file (remove the "quote marks"):
"Love is not love which alters when it alteration finds, or bends with the Remover to remove. Oh no, it is an ever fixed mark that looks on tempests and is never shaken. William Shakespeare"

"Pick up life and let it fall, striving is not the Way at all. A Taoist teaching"

gb
Back to top
CarlosTheTackle



Joined: 19 Oct 2004
Posts: 102

PostPosted: Tue May 03, 2005 10:41 am    Post subject: Reply with quote

If you create a GUI rather than a message box, the text will automatically wrap itself for the size of the box, which would probably be more ideal for you than trying to mess round with getting `n in the right place on each phrase.

FWIW, here's one I did a while back that pretty much does the same thing. It reads a line from a file called 'Prompts.txt' and displays it in a nice, simple GUI (which fades out on click). Prompts.txt has 9 lines in it, but if you have more or less, then change the last number in the 'Random' line.
Code:
#NoTrayIcon
trans_init=220
SETTIMER, Elapsed, 90000
Gosub, Elapsed

DETECTHIDDENWINDOWS, on
Random, RndLine, 1, 9
FileReadLine, current_prompt, Prompts.txt, %RndLine%
Gui, +owner
Gui, Font, s20
Gui, color, FFFFFF
Gui, Add, Text, w500 gGuiClose,%current_prompt%
WinSetTitle, %A_SCRIPTNAME%,, Personal Prompts
WinSet, ALWAYSONTOP, On, Personal Prompts
Winset, Transparent, %trans_init%, Personal Prompts
Gui, -caption
Gui, +border
Sleep, 500
Gui, Show, NA
return

NumpadSub::Gosub, GuiClose

GuiClose:
Loop
  {
  Winset, Transparent, %trans_init%, Personal Prompts
  trans_init-=10
  If trans_init<1
    break
  }
ExitApp

Elapsed:
cycle++
If cycle=2
  ExitApp
return


Cheers,

C
Back to top
View user's profile Send private message
gb
Guest





PostPosted: Tue May 03, 2005 1:05 pm    Post subject: Reply with quote

Thanks! Very Happy

I was really just curious as my script is working, and because I'm starting to learn AHK, I just thought I'd ask for the purpose of furthering my understanding about MsgBox.

Your script will also help me learn AHK, so thanks again.

gb
Back to top
olaf



Joined: 20 Apr 2005
Posts: 25

PostPosted: Sun May 08, 2005 12:45 pm    Post subject: Reply with quote

Cool, I was wanting the same kind of thing for linux fortunes.
Quote:
Your nearest Slackware mirror/slackware/slackware_source/y/bsd-games/

two files:
fortunes-linuxcookie.tar.gz
fortunes-o.tar.gz
The "o"s are rotted to protect the innocent. If your easily offended, your should not read them.

My question is, how could this script be coded to pick out messages spanning multiple lines, with the end (or start) of a message determined by a certain character ("%" for example) or something like two blank lines?

And for a similar question to this thread, which may be the same.
Does ahk have a keyboard escape key to wrap a long line, similar to
the "\" in linux? The need is mainly cosmetic when looking at a script.
For example in linux:
./configure \
--prefix=/usr

olaf
_________________
Berserker
Back to top
View user's profile Send private message
Chris
Site Admin


Joined: 02 Mar 2004
Posts: 10480

PostPosted: Sun May 08, 2005 9:44 pm    Post subject: Reply with quote

olaf wrote:
how could this script be coded to pick out messages spanning multiple lines, with the end (or start) of a message determined by a certain character ("%" for example) or something like two blank lines?
You can use StringLeft/Right to extract the first/last character of a string and check if it's a % sign. Two blank lines in a row could be detected by keeping track of the previous line:

if (A_LoopReadLine = "" and LinePrev = "")
{
... end of a item has been detected
}
LinePrev := A_LoopReadLine ; Update LinePrev in preparation for the next iteration.

Quote:
Does ahk have a keyboard escape key to wrap a long line, similar to the "\" in linux? The need is mainly cosmetic when looking at a script.
The ability to split long lines has just been added. Hopefully it will do what you need.
Back to top
View user's profile Send private message Send e-mail
olaf



Joined: 20 Apr 2005
Posts: 25

PostPosted: Mon May 09, 2005 12:00 pm    Post subject: Reply with quote

Chris wrote:
... The ability to split long lines has just been added. Hopefully it will do what you need.


Thanks Chris for your help on both questions.
Sometimes I need to do stuff like this so my brain can comprehend. Laughing
Code:

msgbox,
( join RTrim0
RTrim0 sees spaces at end of line         
Next line in source code with a return feed`n
New line from return feed with another return feed`n
`n
Consecutive empty lines with return feeds`n
`n
`n
`n
`n
`n
`n
`n
Same as above but on one line as in previous versions`n`n`n`n`n`n`n
`t`t2 Tabs
)`n`tAfter parenthesis, the previous way is in effect`n`tNew Line`n`tNewLine

_________________
Berserker
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group