Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Insert text


  • Please log in to reply
14 replies to this topic
dglp
  • Members
  • 30 posts
  • Last active: Jun 17 2010 10:53 PM
  • Joined: 11 Jun 2010
I'm new here, and new to AHK, and am having a typical newbie's frustration at something that seems incredibly simple yet is completely opaque.

I've spent about two hours now looking for some simple explanations of how to automate sending a line of text to a notepad++ file.

I've made my way through the command list, and have searched for sample scripts that promise to modify text files, insert text, and so forth. I've had no success, and am wondering why the solution is so opaque.

Send doesn't do it
ControlSend doesn't do it
FileAppend doesn't do it.
InputBox requires me to retype the line of text every time, which is exactly what I want to avoid.

For example, if I go to the Control Send command description, it says
ControlSend, Edit1, This is a line of text in the notepad window., Untitled
but if I paste that into a new.ahk file, save it and press F5, nothing happens.

If I add #q:: to the start of the line, save & minimise that window, and run it, nothing happens.

Same for each of the other commands.

So, how does one send a line of text to a file? Why isn't it made obvious someplace easy to find?

Can someone point me to an explanation, or even a sample script?

goldenowl
  • Members
  • 52 posts
  • Last active: Sep 09 2015 06:30 PM
  • Joined: 02 Mar 2007
> sending a line of text to a notepad++ file
Y'all want to append text to a file?

#t::
;Windows t
actionlist = C:\Users\my.name\Documents\Personal\action.txt
InputBox, myinputbox,Action,Current,,500,120,400,300
FormatTime, TimeString, %calendar%, h:mm:ss tt
;non-US dates
mytime = %A_DD%/%A_MM%/%A_YYYY% %TimeString%
FileAppend, %mytime% %myinputbox%`n,%actionlist%
return

.

dglp
  • Members
  • 30 posts
  • Last active: Jun 17 2010 10:53 PM
  • Joined: 11 Jun 2010
Thanks for trying, but no, that's not what I'm after.

I want to open an instance of a text editor (notepad, notepad++, whatever), and add some text to it, for example:

Hello World,
this is a line of text.

I want to insert those same two lines with one keystroke, i.e. #t, in whatever file is open, most likely a new, un-named file.

For example, something like this seems like it should work:
#t::
run, notepad.exe
FileAppend, Hello World `n
FileAppend, This is a line of text. `n
return
But it doesn't.

Any suggestions?

SoLong&Thx4AllTheFish
  • Members
  • 4999 posts
  • Last active:
  • Joined: 27 May 2007
There are several things wrong with your code:
#t::
run, notepad.exe ; OK
FileAppend, Hello World `n ; FileAppend CREATES or APPENDS to a FILE it is NOT used to write to a window
FileAppend, This is a line of text. `n ; AND your filename is missing as well so nothing is created
return

You need

SetTitleMatchmode, 2 ; partial title match
#t::
run, notepad.exe ; OK
WinWait, Notepad
WinActivate, Notepad
Send, Hello World `n 
Send, This is a line of text. `n 
Return

or with any window that has focus:

#t::
Send, Hello World `n 
Send, This is a line of text. `n 
Return


dglp
  • Members
  • 30 posts
  • Last active: Jun 17 2010 10:53 PM
  • Joined: 11 Jun 2010
Fab!
Getting somewhere now.

SetTitleMatchmode, 2 ; partial title match
#t::
run, notepad.exe ; OK
WinWait, Notepad
WinActivate, Notepad
Send, Hello World `n 
Send, This is a line of text. `n 
Return


So,
a) Send is the appropriate command; That's not been clear so far, and I've been trying all those other commands unnecessarily...
B) I need to specify a filename; again, none of the examples I've seen said so in their explanations. It's not intuitively obvious.
c) SetTitleMatchmode or a focused window is required for the script to locate the file.
d) I should use WinWait and WinActivate instead of Sleep; I'd been trying that while notepad opened.

With that sorted, here's something closer to what I want:
SetTitleMatchmode, 2 ; partial title match
#k:: ; Windows+k
run, notepad++.exe ; run Notepad++
WinWait, Notepad++ ; wait for it
WinActivate, Notepad++ ; focus it
Send, <NameBlock>`n ; first line
send, <category id=`" ; beginning of second line, up to the open quote mark
InputBox, UserInput, Category Name ; fill in value between quotes
send, `">`n ; close quote
<color>#000000</color> ; color the text
send, </category>`n ; close category tag


What I want out is:
<NameBlock>
<category id="Name">
<color>#000000</color>
</category>

What I get is:
<NameBlock>
<category id=""> ; Name is missing
<color>000000</color> ; # is missing
<description></description>
</category>

So, that's major progress just getting the lines inserted (for which, thanks!), but now having trouble
a) why isn't the InputBlock working?
B) how do I specify the hash key in the color tag?

dglp
  • Members
  • 30 posts
  • Last active: Jun 17 2010 10:53 PM
  • Joined: 11 Jun 2010

Fab!
Getting somewhere now.

SetTitleMatchmode, 2 ; partial title match
#t::
run, notepad.exe ; OK
WinWait, Notepad
WinActivate, Notepad
Send, Hello World `n 
Send, This is a line of text. `n 
Return


So,
a) Send is the appropriate command; That's not been clear so far, and I've been trying all those other commands unnecessarily...
B) I need to specify a filename; again, none of the examples I've seen said so in their explanations. It's not intuitively obvious.
c) SetTitleMatchmode or a focused window is required for the script to locate the file.
d) I should use WinWait and WinActivate instead of Sleep; I'd been trying that while notepad opened.

With that sorted, here's something closer to what I want:
SetTitleMatchmode, 2 ; partial title match
#k:: ; Windows+k
run, notepad++.exe ; run Notepad++
WinWait, Notepad++ ; wait for it
WinActivate, Notepad++ ; focus it
Send, <NameBlock>`n ; first line
send, <category id=`" ; beginning of second line, up to the open quote mark
InputBox, UserInput, Category Name ; fill in value between quotes
send, `">`n ; close quote
<color>#000000</color> ; color the text
send, </category>`n ; close category tag


What I want out is:
<NameBlock>
<category id="Name">
<color>#000000</color>
</category>

What I get is:
<NameBlock>
<category id=""> ; Name is missing, even though the InputBox prompt does run
<color>000000</color> ; # is missing
<description></description>
</category>

So, that's major progress just getting the lines inserted (for which, thanks!), but now having trouble
a) why isn't the InputBlock working?
B) how do I specify the hash key in the color tag?



SoLong&Thx4AllTheFish
  • Members
  • 4999 posts
  • Last active:
  • Joined: 27 May 2007
It is all there in the documentation if you read the correct page that is :wink:
SetTitleMatchmode, 2 ; partial title match
#k:: ; Windows+k
run, notepad++.exe ; run Notepad++
WinWait, Notepad++ ; wait for it
WinActivate, Notepad++ ; focus it
InputBox, [color=red]UserInput[/color], Category Name ; fill in value between quotes
Send, <NameBlock>`n ; first line
send, <category id=`"[color=red]%UserInput%[/color]`">`n ; close quote
Send, <color>[color=red]`#[/color]000000</color> ; color the text #!^+ are "special" keys see key names http://www.autohotkey.com/docs/commands/Send.htm so you can also use Send {#}
send, </category>`n ; close category tag

The code above is not the most efficient but should get you started.

Start with the tutorials (regular and the one for noobs in my sig)

dglp
  • Members
  • 30 posts
  • Last active: Jun 17 2010 10:53 PM
  • Joined: 11 Jun 2010

It is all there in the documentation if you read the correct page that is :wink:

The code above is not the most efficient but should get you started.

Start with the tutorials (regular and the one for noobs in my sig)


Thanks for the UserInput shortcut - very handy!

The hashtag escape doesn't work.
I'd tried it a couple of ways, and even using your sample verbatim still strips it out of the result.
So it's the bracketed version for me...

I've been reading tutorials, sample, and whatnot til I'm red/blue/cross-eyed, which is why I resorted to the forum - which is obviously where I should have gone first!

One further question:
the ultimate aim is to pull category names from a spreadsheet column, insert them in two places as tags, and save the result as an XML file with the same category name.

Do you know of anything similar that I can play with?

SoLong&Thx4AllTheFish
  • Members
  • 4999 posts
  • Last active:
  • Joined: 27 May 2007
If by spreadsheet you mean Excel:
<!-- m -->http://www.autohotke...c31923-135.html<!-- m -->
Note that you could use a CSV file which you can read and parse in AutoHotkey.

dglp
  • Members
  • 30 posts
  • Last active: Jun 17 2010 10:53 PM
  • Joined: 11 Jun 2010

If by spreadsheet you mean Excel:
<!-- m -->http://www.autohotke...c31923-135.html<!-- m -->
Note that you could use a CSV file which you can read and parse in AutoHotkey.


Either is fine with me - though I suspect going the CSV route would be easier.
If I've got a list of 200 categories and want an XML file for each, it doesn't matter what format the list is in, so long as I can pull each one in individually.

SoLong&Thx4AllTheFish
  • Members
  • 4999 posts
  • Last active:
  • Joined: 27 May 2007
AutoHotkey can parse CSV, although you might want to look into this fairly decent CSV library <!-- m -->http://www.autohotke...pic.php?t=55909<!-- m -->

dglp
  • Members
  • 30 posts
  • Last active: Jun 17 2010 10:53 PM
  • Joined: 11 Jun 2010
Okay, that's good for future ref.
It's clear that I need to get to some point higher on the learning curve to make sense of that.

This seems to be based on specialised mini-routines like CSV_Load, CSV_Read and so on. Since I am still struggling to understand the basic syntax, these more sophisticated forms seem like they require some more involved concepts.

TLM
  • Administrators
  • 3864 posts
  • Last active:
  • Joined: 21 Aug 2006
Dont forget you can send text
in the background
setBatchLines, 1
run, notepad,,, npPid
winWait, % npPid := "ahk_pid " npPid

i:=!i
loop {
  random, nM, 0x41, 0x7A
  controlSend, edit1, % ( ( i<0x1E )
	  ? chr( nM ) subStr( i++, 1, 0 )
	  : chr( 0xA ) subStr( i:=!i, 1, 0 ) )
	  , % npPid
}

esc::Exitapp ; <- press escape to exit
I call this GhostInTheNotepad () :lol:..

htms

Posted Image

don't duplicate, iterate!


TehCatNinja
  • Members
  • 1 posts
  • Last active: Dec 10 2015 04:58 AM
  • Joined: 10 Dec 2015

What i want to do is just have it paste text(so i dont have to copy and paste) i dont really want it to open notepad and have it type it i just want to be able to go into a text box and the the hot key and it pastes in the text box.



zippster
  • Members
  • 1 posts
  • Last active: Jan 01 2016 05:26 AM
  • Joined: 31 Dec 2015

Sorry did not want to create  post so i will use this.

Is there any chance to set variable to any symbol in autohotkey?

I want to make a script that would open file , go through test searching for specific format. For example

searchingtext = "anysymbols - anysymbols - anysymbols"

i want to keep dashes in variable but in between them can be any 3 or 4 symbols.

Is it even possible in autohotkey?