AutoHotkey Community

It is currently May 26th, 2012, 10:02 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: October 9th, 2009, 1:00 pm 
Offline

Joined: September 26th, 2009, 10:04 am
Posts: 22
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 10th, 2009, 12:10 am 
Offline

Joined: February 7th, 2009, 11:28 pm
Posts: 384
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.


Report this post
Top
 Profile  
Reply with quote  
PostPosted: October 10th, 2009, 12:22 am 
Online

Joined: April 8th, 2009, 7:49 pm
Posts: 6065
Location: San Diego, California
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

*/



Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 10th, 2009, 5:20 am 
Offline

Joined: September 26th, 2009, 10:04 am
Posts: 22
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!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 16th, 2009, 6:42 am 
Offline

Joined: September 26th, 2009, 10:04 am
Posts: 22
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!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 16th, 2009, 9:08 am 
Offline

Joined: February 20th, 2007, 1:37 pm
Posts: 198
Location: D.C.
OpalMonkey, here is my solution to a similar problem. It may not be perfect yet but it's getting better.

http://www.autohotkey.com/forum/viewtop ... 487#303487


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot], dra, Exabot [Bot], Google Feedfetcher, hyper_, JSLover, Leef_me, patgenn123 and 57 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group