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 

Can you help me escape?

 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
ribbet.1



Joined: 20 Feb 2007
Posts: 197
Location: D.C.

PostPosted: Fri Oct 16, 2009 6:35 am    Post subject: Can you help me escape? Reply with quote

I made a script that works like the "cliptext" window in some text editors. You highlight text, select the item in the cliptex window and it places the text around your higlighted text. Common for things like HTML tags.

OK, so any experienced ahk coder will know, when my script works w/ the clipboard, it is going to lose the special chars like { }#+^ etc. when it pastes them back. So I wanted to ask if there is an easy way of working around that? I guess I could make a function or sub that pastes in the escape codes for every char that needs it. But is there a simpler way? Please take a look at the code below, and if you like try the text below it as a config file when it asks for it.

Code:
#NoEnv
SendMode Input
SetTimer,GetActiveWindow, 30

ButtonLoadFile:  ;;This is where we select the textfile for the tags
Gui Destroy
Gui +OwnDialogs  ; Forces user to dismiss the following dialog before using main window.
FileSelectFile, Nestcepas,, 3, Select a file to read:
if not Nestcepas  ; The user canceled the dialog.
    return
   
; Allow the user to maximize or drag-resize the window:
Gui +Resize

Menu, MyMenu, Add, Load File, ButtonLoadFile
Menu, TargetMenu, Add, Select Target, ButtonSelect_Target
Menu, TargetMenu, Add, DeSelect, Deselect_Target
Menu, MyMenu, Add, Target, :TargetMenu
Gui, Menu, MyMenu


Gui, Add, Button, Hidden Default H0 W0, OK
Gui, Add, ListView, xm r20 w200 vMyListView gMyListView Grid, Name | Text ; r20 w150

LV_ModifyCol()  ; Auto-size each column to fit its contents.

; This is where we get the items from the text file:

FileRead, Durant, %Nestcepas% ;Read file into var Durant

Loop, Parse, Durant, `n, `r ; This should get each tag one at a time
{
   If A_LoopField =
   Continue
   Alpha = %A_Index%
    Will = %A_Loopfield%
   Loop, Parse, Will, |
      {
      If A_Index = 1
         Gomer%Alpha% = %A_LoopField%
      If A_Index = 2
         {
         Jersey = %A_LoopField%
         StringReplace,Jersey,Jersey,!,`{!`}
         StringReplace,Jersey,Jersey,+,`{`+`}
         StringReplace,Jersey,Jersey,^,`{`^`}
         Goober%Alpha% = %Jersey%
         }
      }
      LV_Add("",Gomer%Alpha%,Goober%Alpha%)
      LV_ModifyCol()  ; Auto-size each column to fit its contents.
      LV_ModifyCol(3, 60) ; Make the Size column at little wider to reveal its header.
;GuiControl, +Redraw, MyListView  ; Re-enable redrawing (it was disabled above).
}
      
; performs alphabetical sort:
LV_ModifyCol(1, "Sort", Name)
Gui, +AlwaysOnTop
Gui, Show
return


DeSelect_Target:
Title =
SetTimer,GetActiveWindow,50
Return


ButtonOK:
GuiControlGet, FocusedControl, FocusV
if FocusedControl <> MyListView
    return
Durham := % LV_GetNext(0, "Focused")
LV_GetText(RowText2, Durham,2)
Goto Gogo



if A_GuiEvent = DoubleClick
Goto MyListView
MyListView:
SetKeyDelay,21,41
DialNine = %Clipboard%
Clipboard =
LV_GetText(RowText2, A_EventInfo,2)  ; Get the text of the second field.
;MsgBox, Rowtext = %Rowtext% `n A_EventInfo = %A_EventInfo%
Gogo:
   WinActivate,%Title%
   Raster := RowText2
   IfInString, Raster, #
      {
      Loop,Parse,Raster, #
         {
         If A_Index = 1
            {
            Defender := A_LoopField
            Send,^x
            ClipWait,.2
            Xevious := Clipboard
            Clipboard =
            }
         Else
            {
            Asteroids := A_LoopField
            }
         }
            Clipboard := % Defender . Xevious . Asteroids
         }
   IfNotInstring, Raster, #
      {
      Clipboard = "%Raster%"
      }
   SetKeyDelay,-1,-1
   Send,%Clipboard%
   Send,%A_Space%
      Clipboard =
   ;Clipboard = %DialNine%
SetKeyDelay,11,11
Return

GuiSize:  ; Expand or shrink the ListView in response to the user's resizing of the window.
if A_EventInfo = 1  ; The window has been minimized.  No action needed.
    return
; Otherwise, the window has been resized or maximized. Resize the ListView to match.
GuiControl, Move, MyListView, % "W" . (A_GuiWidth - 20) . " H" . (A_GuiHeight - 20) ;%
return

GuiClose:  ; When the window is closed, exit the script automatically:
^+#Escape::
ExitApp

Clicky:
ButtonSelect_Target:
MsgBox,After pressing OK, click on the Edit field of the word processor or text editor application you would like to have your text placed in.  If you don't have that application open now, do so before pressing the "OK" button in this message box. (Hint, The Edit field is where the actual document is displayed.)
SetTimer,GetActiveWindow,Off
Title =
KeyWait,LButton,D
MouseGetPos, , , id, control
WinGetTitle, Title, ahk_id %id%
WinGetClass, class, ahk_id %id%
;Msgbox,%Title% is title`n%class% is class
Return

^+#t::
InputBox, Outty, Type Name of Editor Application Shown in Titlebar,Just the program name and not the document name`, that is,,,,,,,20
Title := Outty
SetTimer,GetActiveWindow,Off
MsgBox, Title of window is %Outty%
Return

#!t::
WinGetActiveTitle,Title
If Title = %A_ScriptName%
{
WinHIde %A_ScriptName%
Return
}
Else
WinShow,%A_ScriptName%
WinActivate,%A_ScriptName%
ControlFocus,SysListView321,%A_ScriptName%
Return

^#!t::WinHide,%A_ScriptName%

GetActiveWindow:
WinGetActiveTitle, Nookie
If Nookie != %A_ScriptName%
Title = %Nookie%
Return



And here is a config file. Save as a text file and load it when prompted:

Code:
00NewPage | <DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">{Enter}<HTML>{Enter}<HEAD>{Enter}<TITLE>#</TITLE>{Enter}<META NAME="Generator" CONTENT="TextPad 4.0">{Enter}<META NAME="Author" CONTENT="?">{Enter}<META NAME="Keywords" CONTENT="?">{Enter}<META NAME="Description" CONTENT="?">{Enter}</HEAD>{Enter}{Enter}<BODY bgcolor="FFFFFF" text="000000" link="0033CC" vlink="660066" alink="00CC33">#{Enter}{Enter}</BODY>{Enter}</HTML>
00Comment | <!--#-->
Doctype | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
a_link | <a href="??">#</a>
a_name | <a name="??">#</a>
break | <br>
center | <center>#</center>
cite | <cite>#</cite>
code | <code>#</code>
definition | <dfn>#</dfn>
definition list | <dl>#</dl>
div | <div align="left">#</div>
horizontal rule | <hr>
ordered list | <ol>#</ol>
paragraph | <p>#</p>
preformatted | <pre>#</pre>
blockquote | <blockquote>#</blockquote>
sample output | <sample>#</sample>
unordered list | <ul>#</ul>
font_big | <big>#</big>
font_bold | <b>#</b>
font_emph | <em>#</em>
font _ italic | <i>#</i>
font_small | <small>#</small>
font strong | <strong>#</strong>
font_subscript | <sub>#</sub>
font_superscript | <sup>#</sup>
font_underline | <u>#</u>
font 1 | <font size="1">#</font>
font 2 | <font size="2">#</font>
font 3 | <font size="3">#</font>
font 4 | <font size="4">#</font>
font 5 | <font size="5">#</font>
font 6 | <font size="6">#</font>
font 7 | <font size="7">#</font>
h1 | <h1>#</h1>
h2 | <h2>#</h2>
h3 | <h3>#</h3>
h4 | <h4>#</h4>
h5 | <h5>#</h5>
h6 | <h6>#</h6>
image | <img src="#" alt="?" border="0" height="?" width="?">
table | <table border="1" cellspacing="0" cellpadding="0" width="100%">#</table>
table data | <td>#</td>
table heading | <th>#</th>
table row | <tr>#</tr>
CSS style tag | */#/*
Angle Irons | <#>
List Item | <li>
Back to top
View user's profile Send private message
ribbet.1



Joined: 20 Feb 2007
Posts: 197
Location: D.C.

PostPosted: Fri Oct 16, 2009 7:50 am    Post subject: Reply with quote

OK, I'm sorry if this was a dumb post. I just thought there might be some simple solution somebody would know off the top of his/her head.

At any rate, here is my provisional solution. Deals with !+^# chars adequately. I have to go to the gym.

Code:
#NoEnv
SendMode Input
SetTimer,GetActiveWindow, 30

ButtonLoadFile:  ;;This is where we select the textfile for the tags
Gui Destroy
Gui +OwnDialogs  ; Forces user to dismiss the following dialog before using main window.
FileSelectFile, Nestcepas,, 3, Select a file to read:
if not Nestcepas  ; The user canceled the dialog.
    return
   
; Allow the user to maximize or drag-resize the window:
Gui +Resize

Menu, MyMenu, Add, Load File, ButtonLoadFile
Menu, TargetMenu, Add, Select Target, ButtonSelect_Target
Menu, TargetMenu, Add, DeSelect, Deselect_Target
Menu, MyMenu, Add, Target, :TargetMenu
Gui, Menu, MyMenu


Gui, Add, Button, Hidden Default H0 W0, OK
Gui, Add, ListView, xm r20 w200 vMyListView gMyListView Grid, Name | Text ; r20 w150

LV_ModifyCol()  ; Auto-size each column to fit its contents.

; This is where we get the items from the text file:

FileRead, Durant, %Nestcepas% ;Read file into var Durant

Loop, Parse, Durant, `n, `r ; This should get each tag one at a time
{
   If A_LoopField =
   Continue
   Alpha = %A_Index%
    Will = %A_Loopfield%
   Loop, Parse, Will, |
      {
      If A_Index = 1
      {
         Jersey = %A_LoopField%
         StringReplace,Jersey,Jersey,!,`{!`},All
         StringReplace,Jersey,Jersey,+,`{`+`},All
         StringReplace,Jersey,Jersey,^,`{`^`},All
         Gomer%Alpha% = %Jersey%
         Jersey =
      }
      If A_Index = 2
         {
         Jersey = %A_LoopField%
         StringReplace,Jersey,Jersey,!,`{!`},All
         StringReplace,Jersey,Jersey,+,`{`+`},All
         StringReplace,Jersey,Jersey,^,`{`^`},All
         Goober%Alpha% = %Jersey%
         }
      }
      LV_Add("",Gomer%Alpha%,Goober%Alpha%)
      LV_ModifyCol()  ; Auto-size each column to fit its contents.
      LV_ModifyCol(3, 60) ; Make the Size column at little wider to reveal its header.
;GuiControl, +Redraw, MyListView  ; Re-enable redrawing (it was disabled above).
}
      
; performs alphabetical sort:
LV_ModifyCol(1, "Sort", Name)
Gui, +AlwaysOnTop
Gui, Show
return


DeSelect_Target:
Title =
SetTimer,GetActiveWindow,50
Return


ButtonOK:
GuiControlGet, FocusedControl, FocusV
if FocusedControl <> MyListView
    return
Durham := % LV_GetNext(0, "Focused")
LV_GetText(RowText2, Durham,2)
Goto Gogo



if A_GuiEvent = DoubleClick
Goto MyListView

MyListView:
SetKeyDelay,21,41
DialNine = %Clipboard%
Clipboard =
LV_GetText(RowText2, A_EventInfo,2)  ; Get the text of the second field.
;MsgBox, Rowtext = %Rowtext% `n A_EventInfo = %A_EventInfo%
Gogo:
   WinActivate,%Title%
   Raster := RowText2
   IfInString, Raster, #
      {
      Loop,Parse,Raster, #
         {
         If A_Index = 1
            {
            Defender := A_LoopField
            Send,^x
            ClipWait,.2
            Xevious := Clipboard
            StringReplace,Xevious,Xevious,{, `{`{`},All
            StringReplace,Xevious,Xevious,`}, `{`}`},All
            StringReplace,Xevious,Xevious,{{{}},`{`{`}
            StringReplace,Xevious,Xevious,!, `{!`},All
            StringReplace,Xevious,Xevious,+, `{+`},All
            StringReplace,Xevious,Xevious,^, `{^`},All
            StringReplace,Xevious,Xevious,#, `{#`},All
            ;
            Clipboard =
            }
         Else
            {
            Asteroids := A_LoopField
            }
         }
            Clipboard := % Defender . Xevious . Asteroids
         }
   IfNotInstring, Raster, #
      {
      Clipboard = %Raster%
      }
   SetKeyDelay,-1,-1
   Send,%Clipboard%
   Send,%A_Space%
      Clipboard =
   ;Clipboard = %DialNine%
SetKeyDelay,11,11
Return

GuiSize:  ; Expand or shrink the ListView in response to the user's resizing of the window.
if A_EventInfo = 1  ; The window has been minimized.  No action needed.
    return
; Otherwise, the window has been resized or maximized. Resize the ListView to match.
GuiControl, Move, MyListView, % "W" . (A_GuiWidth - 20) . " H" . (A_GuiHeight - 20) ;%
return

GuiClose:  ; When the window is closed, exit the script automatically:
^+#Escape::
ExitApp

Clicky:
ButtonSelect_Target:
MsgBox,After pressing OK, click on the Edit field of the word processor or text editor application you would like to have your text placed in.  If you don't have that application open now, do so before pressing the "OK" button in this message box. (Hint, The Edit field is where the actual document is displayed.)
SetTimer,GetActiveWindow,Off
Title =
KeyWait,LButton,D
MouseGetPos, , , id, control
WinGetTitle, Title, ahk_id %id%
WinGetClass, class, ahk_id %id%
;Msgbox,%Title% is title`n%class% is class
Return

#!t::
WinGetActiveTitle,Title
If Title = %A_ScriptName%
{
WinHIde %A_ScriptName%
Return
}
Else
WinShow,%A_ScriptName%
WinActivate,%A_ScriptName%
ControlFocus,SysListView321,%A_ScriptName%
Return

^#!t::WinHide,%A_ScriptName%

GetActiveWindow:
WinGetActiveTitle, Nookie
If Nookie != %A_ScriptName%
Title = %Nookie%
Return



Back to top
View user's profile Send private message
flak



Joined: 02 Oct 2009
Posts: 283

PostPosted: Fri Oct 16, 2009 9:38 am    Post subject: Reply with quote

Quote:
it is going to lose the special chars like { }#+^ etc. when it pastes them back

SendRaw?
Quote:
Raw mode: The SendRaw command sends the keystrokes exactly as they appear rather than translating {Enter} to an ENTER keystroke, ^c to Control-C, etc. To use raw mode with SendInput, SendPlay, or SendEvent, write {Raw} as the first item in the string; for example: SendInput {Raw}abc
Back to top
View user's profile Send private message
Display posts from previous:   
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