 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
Opal Monkey
Joined: 26 Sep 2009 Posts: 22
|
Posted: Fri Oct 09, 2009 12:00 pm Post subject: Best way to input text around selected text? |
|
|
I'm wondering what the best way to insert text around a chunk of selected text is?
I'm using a GUI where the buttons insert various text. I'm using SendInput to send commands, because they are multi-line. They look something like this:
| Code: | ButtonName
IfWinExist ahk_class Notepad++
WinActivate
SendInput {raw}miscellaneous text
SendInput {enter}{raw}more miscellaneous text
SendInput {enter}{enter}{raw}more miscellaneous text
SendInput {enter}{raw}more miscellaneous text
SendInput {enter}{raw}more miscellaneous text
Return |
This is all working quite well, I just need to be able to wrap this around selected text. I'm thinking of trying to cut the selected text first, then paste it in at the right point? But I'm betting there is a better way to do it, rather than just mimicking the actions I would take if I did it all manually.
Now, hopefully that all made some kind of sense!
Thanks,
Opal Monkey |
|
| Back to top |
|
 |
pajenn
Joined: 07 Feb 2009 Posts: 384
|
Posted: Fri Oct 09, 2009 11:10 pm Post subject: |
|
|
You could try "ControlGet, outputVar, Selected,..." to get the selected text, append new text to both sides of it, then use "Control, EditPaste,..." to replace the selected text.
For example, in Notepad, it would work something like this (with notepad active and some text selected):
| Code: | ^z::
ControlGet,sText,Selected,,Edit1,ahk_class Notepad
beforeText := "miscellaneous text`r`n`r`nmore miscellaneous text`r`n"
afterText := "`r`nmore miscellaneous text`r`nmore miscellaneous text"
sText := beforeText . sText . afterText
Control,EditPaste,%sText%,Edit1,ahk_class Notepad
Return |
It doesn't work with all editors. For example, I have Notepad++ v5.5 (Unicode), and it didn't work on that, but maybe it does with the ANSI version (if that's what you have), or you can use copy/paste through clipboard. Something like:
| Code: | ^z::
oCB := ClipboardAll
Clipboard =
Send ^c
ClipWait,1
beforeText := "miscellaneous text`r`n`r`nmore miscellaneous text`r`n"
afterText := "`r`nmore miscellaneous text`r`nmore miscellaneous text"
Clipboard := beforeText . Clipboard . afterText
Send ^v
Clipboard := oCB
Return |
_________________ Hardware: 1.8 GHz laptop with 4 GB ram, Windows XP/SP3
Software: Prevx, Privatefirewall, KeyScrambler. |
|
| Back to top |
|
 |
Leef_me
Joined: 08 Apr 2009 Posts: 5336 Location: San Diego, California
|
Posted: Fri Oct 09, 2009 11:22 pm Post subject: Re: Best way to input text around selected text? |
|
|
Tested, see the sample runs at the bottom of the script.
You could probably use the variable %clipboard% instead of ^v
except I >>think<< you have to contend with `r`n at the end of the text.
I had to change your sendinput to Send and change SetKeyDelay.
I was getting 'misfires' from the send & sendinput statements that resulted in my window being locked, like Win-L had been typed.
| Code: | SetKeyDelay , 50
return
esc::
exitapp
#q::
ButtonName1:
IfWinExist, ahk_class Notepad
WinActivate
Send ^x ; <----------------cut the selected text to the clipboard
Send {raw}miscellaneous text
Send {enter}{raw}more miscellaneous text
Send {enter}{enter}{raw}more miscellaneous text
Send ^v ; <----------------paste the selected text from the clipboard
Send {enter}{raw}more miscellaneous text
Send {enter}{raw}more miscellaneous text
Return
#w::
ButtonName2:
IfWinExist, ahk_class Notepad
WinActivate
; http://www.autohotkey.com/docs/Scripts.htm#continuation
; see method #2: Example #1 for description of how this works
before_text =
(
{raw}miscellaneous text
more miscellaneous text
more miscellaneous text
)
after_text =
(
{raw}more miscellaneous text
more miscellaneous text
)
Send ^x
Send %before_text%
Send ^v
Send %after_text%
Return
;===========================================
; test runs are below
;===========================================
/* example text
this is a test of the emergency broadcasting system
select only the word 'emergency' before hitting the hotkey.
*/
/* example text after using #q
this is a test of the miscellaneous text
more miscellaneous text
more miscellaneous textemergency
more miscellaneous text
more miscellaneous textmiscellaneous text
more miscellaneous text
more miscellaneous textemergencymore miscellaneous text
more miscellaneous text broadcasting system
*/
/* example text after using #w
this is a test of the miscellaneous text
more miscellaneous text
more miscellaneous textemergencymore miscellaneous text
more miscellaneous text broadcasting system
*/
|
|
|
| Back to top |
|
 |
Opal Monkey
Joined: 26 Sep 2009 Posts: 22
|
Posted: Sat Oct 10, 2009 4:20 am Post subject: |
|
|
For the time being I'll just use, what I think is, the simplest option. I'm just using Leef's first example. Using SendInput seems to be working fine with that for me. This should also be easy to add to everything I've already got.
I'll have to try out the other options and see how they go. I've got Notepad++ 5.4.4 (Unicode) at the moment, so I'll skip that one.
Thanks, Leef_me and pajenn, for the help! |
|
| Back to top |
|
 |
Opal Monkey
Joined: 26 Sep 2009 Posts: 22
|
Posted: Fri Oct 16, 2009 5:42 am Post subject: |
|
|
Hi, Pajenn.
I thought I would let you know, after 6 days of learning and a few light bulbs going on in my head, I came to a solution similar to your second idea. Mine now looks like this:
| Code: | ButtonName:
IfWinExist ahk_class Notepad++
WinActivate
ClipSaved := ClipboardAll
Clipboard =
Send ^x
ClipWait 1
Send {raw}Text
Send ^v
Send {raw}Text
Clipboard := ClipSaved
ClipSaved =
Return |
This is working good for now, but I get the feeling that I should move over to using variables for all the text, like you did. When I first started this thread I had no idea how to use variables, so that's why I didn't get what you were doing in that example.
Thanks, and off I go to learn more about variables and test many ways to use them! |
|
| Back to top |
|
 |
ribbet.1
Joined: 20 Feb 2007 Posts: 197 Location: D.C.
|
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|