Jump to content

Large text to variable


therks
 Share

Recommended Posts

I wrote this a while ago and thought it might be nice to share. I use it whenever I have a large amount of text that I need to put in a variable.

*Edit: Fixed a bug I noticed in this post.

#NoTrayIcon
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GUICombo.au3>
#include <GUIEdit.au3>

$gui = GUICreate('Txt2$var - Text to variable code', 420, 400, Default, Default, $WS_OVERLAPPEDWINDOW)

$bt_InputFocus = GUICtrlCreateButton('&i', 0, 0)
    GUICtrlSetState(-1, $GUI_HIDE)
$bt_OutputFocus = GUICtrlCreateButton('&o', 0, 0)
    GUICtrlSetState(-1, $GUI_HIDE)

$bt_SelectAll = GUICtrlCreateButton('Select &All', 360, 0, 60, 20)
    GUICtrlSetResizing(-1, $GUI_DOCKMENUBAR+$GUI_DOCKWIDTH+$GUI_DOCKRIGHT)

$tb_Main = GUICtrlCreateTab(0, 0, 420, 400)
    GUICtrlSetResizing(-1, $GUI_DOCKBORDERS)
$tbi_Input = GUICtrlCreateTabItem('&Input')
$ed_Input = GUICtrlCreateEdit('', 10, 30, 400, 315)
    GUICtrlSetResizing(-1, $GUI_DOCKBORDERS)

$bt_Convert = GUICtrlCreateButton('&Convert', 295, 0, 60, 20)
    GUICtrlSetResizing(-1, $GUI_DOCKMENUBAR+$GUI_DOCKWIDTH+$GUI_DOCKRIGHT)

Opt('GUIResizeMode', $GUI_DOCKSTATEBAR)

GUICtrlCreateLabel('&String encapsulation:', 10, 350, 100, 20, $SS_CENTERIMAGE)
$cb_QuoteMethod = GUICtrlCreateCombo('', 115, 350, 90, 100, $CBS_DROPDOWNLIST)
    GUICtrlSetData(-1, 'Single Quotes|Double Quotes', 'Single Quotes')

GUICtrlCreateLabel('&New line char:', 10, 375, 90, 20, $SS_CENTERIMAGE)
$cb_LineChar = GUICtrlCreateCombo('', 115, 375, 90, 100, $CBS_DROPDOWNLIST)
    GUICtrlSetData(-1, '@CRLF|@LF|@CR', '@CRLF')

GUICtrlCreateLabel('S&eparate lines with:', 215, 350, 100, 20, $SS_CENTERIMAGE)
$cb_LineMethod = GUICtrlCreateCombo('', 320, 350, 90, 100, $CBS_DROPDOWNLIST)
    GUICtrlSetData(-1, '"&="|" _"|Single line|Single line**', '"&="')

GUICtrlCreateLabel('** - Use Opt("ExpandVarStrings", 1)', 215, 375, 195, 20, BitOR($SS_RIGHT, $SS_CENTERIMAGE))

$tbi_Output = GUICtrlCreateTabItem('&Output')
$ed_Output = GUICtrlCreateEdit('', 10, 30, 400, 360)
    GUICtrlSetResizing(-1, $GUI_DOCKBORDERS)
GUICtrlCreateTabItem('')

GUISetState()
ControlFocus($gui, '', $ed_Input)

While 1
    $gm = GUIGetMsg()
    Switch $gm
        Case $bt_SelectAll, $tb_Main, $bt_InputFocus, $bt_OutputFocus
            If $gm = $bt_InputFocus Then
                GUICtrlSetState($tbi_Input, $GUI_SHOW)
            ElseIf $gm = $bt_OutputFocus Then
                GUICtrlSetState($tbi_Output, $GUI_SHOW)
            EndIf
            $i_Tab = GUICtrlRead($tb_Main)
            If $i_Tab = 0 Then
                ControlFocus($gui, '', $ed_Input)
                _GUICtrlEditSetSel($ed_Input, 0, -1)
            Else
                ControlFocus($gui, '', $ed_Output)
                _GUICtrlEditSetSel($ed_Output, 0, -1)
            EndIf
        Case $bt_Convert
            $i_Quotes = _GUICtrlComboGetCurSel($cb_QuoteMethod)
            $i_Method = _GUICtrlComboGetCurSel($cb_LineMethod)
            GUICtrlSetData($ed_Output, _ConvertTxt2Var(GUICtrlRead($ed_Input), $i_Method, $i_Quotes, GUICtrlRead($cb_LineChar)))
            GUICtrlSetState($tbi_Output, $GUI_SHOW)
            ControlFocus($gui, '', $ed_Output)
            _GUICtrlEditSetSel($ed_Output, 0, -1)
        Case $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch
WEnd

Func _ConvertTxt2Var($s_Input, $i_Method, $i_Quotes, $s_LineChar)
    Local $s_Output
    Local $QC = "'" ; Quote char
    If $i_Quotes Then $QC = '"'
    
    If $i_Method = 3 Then
        $s_Output = 'Opt(' & $QC & 'ExpandVarStrings' & $QC & ', 1)' & @CRLF
        $s_Input = StringReplace($s_Input, $QC, $QC & $QC)
        $s_Input = StringReplace($s_Input, '@', '@@')
        $s_Input = StringReplace($s_Input, '$', '$$')
        $s_Input = StringReplace($s_Input, @TAB, '@TAB@')
        $s_Input = StringReplace($s_Input, @CRLF, $s_LineChar & '@')
        $s_Input = StringReplace($s_Input, @CR, $s_LineChar & '@')
        $s_Input = StringReplace($s_Input, @LF, $s_LineChar & '@')
        
        $s_Output &= '$v = ' & $QC & $s_Input & $QC & @CRLF
        $s_Output &= 'Opt(' & $QC & 'ExpandVarStrings' & $QC & ', 0)' & @CRLF
        
        Return $s_Output
    ElseIf $i_Method = 2 Then
        $s_Input = StringReplace($s_Input, $QC, $QC & $QC)
        $s_Input = StringReplace($s_Input, @CRLF, $QC & ' & ' & $s_LineChar & ' & ' & $QC)
        $s_Input = StringReplace($s_Input, @CR, $QC & ' & ' & $s_LineChar & ' & ' & $QC)
        $s_Input = StringReplace($s_Input, @LF, $QC & ' & ' & $s_LineChar & ' & ' & $QC)
        $s_Input = StringReplace($s_Input, ' & ' & $QC & $QC & ' & ', ' & ')
        
        $s_Output &= '$v = ' & $QC & $s_Input & $QC & @CRLF
        $s_Output = StringReplace($s_Output, ' & ' & $QC & $QC, '')
        
        Return $s_Output
    Else
        $s_Input = StringStripCR($s_Input)
        $s_Input = StringReplace($s_Input, $QC, $QC & $QC)
        $s_Input = StringReplace($s_Input, @CRLF, @LF)
        $s_Input = StringReplace($s_Input, @CR, @LF)
        
        Local $a_Lines = StringSplit($s_Input, @LF)
        
        For $i = 1 to $a_Lines[0]
            $s_Line = $a_Lines[$i]
            
            If $s_Line <> '' Then
                $s_Line = $QC & $s_Line & $QC
                If $i < $a_Lines[0] Then $s_Line &= ' & '
            ElseIf $i = $a_Lines[0] Then
                ExitLoop
            EndIf
            
            If $i = 1 Then
                $s_Output = '$v = ' & $s_Line
            ElseIf $i_Method = 0 Then
                $s_Output &= '$v &= ' & $s_Line
            Else
                $s_Output &= $s_Line
            EndIf
            
            If $i < $a_Lines[0] Then
                $s_Output &= $s_LineChar
                If $i_Method = 1 And Not ($i + 1 = $a_Lines[0] And $a_Lines[$i + 1] = '') Then
                    $s_Output &= ' & _' & @CRLF
                Else
                    $s_Output &= @CRLF
                EndIf
            EndIf
        Next
        
        Return $s_Output
    EndIf
EndFunc
Edited by Saunders
Link to comment
Share on other sites

Ah neat. I never felt a need to convert back since I use the tabs and the original text is still available. I don't see what you mean about CodeWizard though.. well, I guess you could use the multiline msgbox/inputbox things and use the outputted text. But to be honest I never really liked CodeWizard. It was a bit too much for what I needed. I actually wrote my own MsgBox creating script, nice and simple. What can I say, I like to do things my way, heh. :)

Link to comment
Share on other sites

Ah neat. I never felt a need to convert back since I use the tabs and the original text is still available. I don't see what you mean about CodeWizard though.. well, I guess you could use the multiline msgbox/inputbox things and use the outputted text. But to be honest I never really liked CodeWizard. It was a bit too much for what I needed. I actually wrote my own MsgBox creating script, nice and simple. What can I say, I like to do things my way, heh. :)

Apologies for a stupid question but I'm lost with this one. :)

What is the purpose of the conversion? The text is already in the variable $s_Input so why do you convert it, and if you never convert it back then what do you use it for?

If you wanted to use the text in MsgBox for example then MsgBox(0,'$s_input=',$s_input) works fine without any conversion doesn't it?

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

???

I'm not sure I understand your question, but let me provide an example.

I use my application to quickly turn this:

Dont ask me

What you know is true

Dont have to tell you

I love your precious heart

I

I was standing

You were there

Two worlds collided

And they could never tear us apart

We could live

For a thousand years

But if I hurt you

Id make wine from your tears

I told you

That we could fly

cause we all have wings

But some of us dont know why

I

I was standing

You were there

Two worlds collided

And they could never ever tear us apart

Into this:

$v = 'Dont ask me' & @CRLF & 'What you know is true' & @CRLF & 'Dont have to tell you' & @CRLF & 'I love your precious heart' & @CRLF & 'I' & @CRLF & 'I was standing' & @CRLF & 'You were there' & @CRLF & 'Two worlds collided' & @CRLF & 'And they could never tear us apart' & @CRLF & @CRLF & 'We could live' & @CRLF & 'For a thousand years' & @CRLF & 'But if I hurt you' & @CRLF & 'Id make wine from your tears' & @CRLF & @CRLF & 'I told you' & @CRLF & 'That we could fly' & @CRLF & 'cause we all have wings' & @CRLF & 'But some of us dont know why' & @CRLF & @CRLF & 'I' & @CRLF & 'I was standing' & @CRLF & 'You were there' & @CRLF & 'Two worlds collided' & @CRLF & 'And they could never ever tear us apart'oÝ÷ Øêí+ºÚ"µÍÌÍÝH   ÌÎNÑÛÚÈYIÌÎNÈ  [ÈÔ   [ÈÂÌÎNÕÚ][ÝHÛÝÈÈYIÌÎNÈ    [ÈÔ   [ÈÂÌÎNÑÛ]HÈ[[ÝIÌÎNÈ  [ÈÔ   [ÈÂÌÎNÒHÝH[ÝXÚ[ÝÈX    ÌÎNÈ [ÈÔ   [ÈÂÌÎNÒIÌÎNÈ    [ÈÔ   [ÈÂÌÎNÒHØÈÝ[[ÉÌÎNÈ  [ÈÔ   [ÈÂÌÎNÖ[ÝHÙHIÌÎNÈ [ÈÔ   [ÈÂÌÎNÕÛÈÛÜÈÛÛYY    ÌÎNÈ [ÈÔ   [ÈÂÌÎNÐ[^HÛÝ[]XÈ    ÌÎNÈ [ÈÔ   [ÈÂÔ [ÈÂÌÎNÕÙHÛÝ[]IÌÎNÈ   [ÈÔ   [ÈÂÌÎNÑÜHÝØ[YXÉÌÎNÈ [ÈÔ   [ÈÂÌÎNÐ]YH[ÝIÌÎNÈ  [ÈÔ   [ÈÂÌÎNÒYXZÙHÚ[HÛH[ÝXÉÌÎNÈ  [ÈÔ   [ÈÂÔ [ÈÂÌÎNÒHÛ[ÝIÌÎNÈ  [ÈÔ   [ÈÂÌÎNÕ]ÙHÛÝ[IÌÎNÈ   [ÈÔ   [ÈÂÌÎNØØ]ÙHÙH[]HÚ[ÜÉÌÎNÈ  [ÈÔ   [ÈÂÌÎNÐ]ÛÛYHÙÈÛÛÝÈÚIÌÎNÈ   [ÈÔ   [ÈÂÔ [ÈÂÌÎNÒIÌÎNÈ    [ÈÔ   [ÈÂÌÎNÒHØÈÝ[[ÉÌÎNÈ  [ÈÔ   [ÈÂÌÎNÖ[ÝHÙHIÌÎNÈ [ÈÔ   [ÈÂÌÎNÕÛÈÛÜÈÛÛYY    ÌÎNÈ [ÈÔ   [ÈÂÌÎNÐ[^HÛÝ[]]XÈ   ÌÎNÈ [ÈÔ   [È
* Note, just discovered a bug actually with this last conversion. Thanks for the incentive to test martin! :)
Link to comment
Share on other sites

Fixed the bug I noticed in my last post. I was going to just edit that post but when I tried - holy crap! It turned my autoit tags into pure gibberish. I guess that's the autoit code bug that people on the forums have been complaining about.

Thanks for the example, but I understand what it does, and if my dumb question helped find a bug then that's great. What I don't understand is why you want to do it. Can you give an example, not with code necessarily, but as a description of where this is useful. I'm probably being very thick about this, but so far I've never felt the need to do anything like that, so if there are cases when I do need it I want to be prepared. I expect my programming techniques are just too narrow.

As far as editing posts is concerned, if you copy the text from the forum before you edit it you can then paste it back in and edit if that makes sense.

I haven't used the AutoIt code tags for a long time because of the frequent problems, and I've never had any problem with code tags which is what I always use.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

His script converts an entered text into formatted form that can be pasted directly into a source file. That way, it has all the quotes, new lines, and other special characters set up.

This is nice, but why would you need to enter large amounts like that?

I still don't get it. Source file for what?

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

I still don't get it. Source file for what?

Source file for a script.

Let's say you are creating a book in AutoIt, and you'll have to put all the pages of text inside the source code, you can type or paste the pages into his converted and then paste the output into your book script.. :)

@Richard, you will come across a few of those conversions and you'll be happy when you have bookmarked this page by that time.

Link to comment
Share on other sites

Hi!

Perso, I use this method (example) :

$g=_istr("toto")
#cs toto
Dont ask me
What you know is true
Dont have to tell you
I love your precious heart
I
I was standing
You were there
Two worlds collided
And they could never tear us apart

We could live
For a thousand years
But if I hurt you
Id make wine from your tears

I told you
That we could fly
cause we all have wings
But some of us dont know why

I
I was standing
You were there
Two worlds collided
And they could never ever tear us apart
#ce toto


MsgBox(0,"$g",$g)



Func _istr($id)
    $tmp = FileRead(@ScriptName)
    $i=StringInStr($tmp,"#cs " & $id)
    $j=StringInStr($tmp,"#ce " & $id)
    if $i<1 or $j<1 Then
        Return ""
    Else
        $i+=StringLen($id)+6
        Return StringMid($tmp,$i,$j-$i-2)
    EndIf
EndFunc
Edited by Michel Claveau
Link to comment
Share on other sites

Hey, that's a neat idea, but only works for uncompiled scripts unfortunately. :)

@martin: I've been using it for About dialogs and such. I suppose I could have just written the text to a file and loaded from that, but that means having to FileInstall an extra file.

Here's a better example I suppose. I ripped this right out of a program I wrote a while ago.

#include <GUIConstants.au3>
Func _FK_About()
    Opt('ExpandVarStrings', 1)
    $s_AboutContent = '   I wrote FireKey to replace a program called WinKey, by Copernic Technologies. WinKey was nice, but a bit limited, it only had support for a specific list of key combinations that you couldn''t customize. For example you couldn''t define hotkeys with the tilde, apostrophe, other punctuation, or even the Alt key, strictly Win, Ctrl, and Shift. FireKey doesn''t suffer from that limitation, and I also added in a few custom commands. Like the Open and Close CD Tray commands, you simply supply a parameter of the drive you want to open or close, or the Send Keys command, which allows you to simply send a string of text to whatever window currently has focus. Unfortunately, FireKey does suffer from one limitation that I know of, it can only support a total of 64 hotkeys. This may or may not be a problem, 64 is a rather high number. I''ve got everything hotkeyed that I could ask for right now and I''m only using about 20 hotkeys. The program will always keep track for you, right in it''s titlebar.@CRLF@@CRLF@   Special Commands:@CRLF@   There are several commands you can use besides just running a specific file. They are listed and explained here.@CRLF@     - FireKey Commands:@CRLF@     - Show/Hide FireKey Window - Just what it says. Show the FireKey window if it''s not shown, and hide it if it is.@CRLF@     - Shutdown FireKey - Ends FireKey and disables all hotkeys.@CRLF@     - Misc Commands:@CRLF@    - Turn Off Monitor - Puts the monitor in it''s sleep mode if available.@CRLF@       - Open/Close CD Tray - Open or close the defined CD Tray. Enter the letter of the drive to open or close in the Parameters box.@CRLF@       - Send Keys - This will automatically send a string of text to whatever window has focus. Enter the text to send in the Parameters box.@CRLF@       - Send Keys (Adv) - Same as above, but certain characters and strings have special meaning. Have a look at the Send Key List on the AutoIt website for more information. (http://www.autoitscript.com/autoit3/docs/appendix/SendKeys.htm)@CRLF@   - Active Window Commands:@CRLF@       - Minimize/Maximize/Restore/Close - Exactly what they say. When this command is executed, whichever window currently has focus will attempt to be minimized, maximized, etc. This will not necessarily work for every window.@CRLF@  @CRLF@   Uninstalling:@CRLF@   - First: There will be a maximum of 3 files left on your system.@CRLF@      1. FireKey.exe (obviously)@CRLF@    2. FireKey_ReadMe.txt@CRLF@     3. PSApi.dll@CRLF@    The first two files will be found wherever you run FireKey from. The third is only placed in your system directory if you''re running Windows NT 4 and do not already have psapi.dll. This file is needed to adjust process priority of the program so that it doesn''t eat up the CPU.@CRLF@   - Second: Registry settings@CRLF@     * Warning: Do not edit the registry unless you know what you''re doing!@CRLF@       - FireKey stores all of it''s settings and hotkeys in a registry key under HKEY_CURRENT_USER\Software\SaundersSoft\FireKey. If you really want all traces of FireKey gone, delete the "FireKey" registry key.@CRLF@  @CRLF@Special Thanks to:@CRLF@   - The AutoIt3 development team, for the wonderful, easy to use, and best of all free, software.@CRLF@   - Especially special thanks to JPM, for his awesome dedication and constant beta updates.@CRLF@   - gafrost, for his wonderful GuiListView and GuiCombo functions.@CRLF@   - jftuga and w0uter for the _ReduceMemory function.@CRLF@   - ezzetabi for the _ShellExecute function.@CRLF@   - Copernic Technologies, for originally creating WinKey, which served me well for eons.@CRLF@@CRLF@Updates:@CRLF@   - Version 1.1 - 2006-02-23@CRLF@  Bug fix: Error when Start Dir field not empty.@CRLF@   - Version 1.0 - 2005-08-25@CRLF@@CRLF@Written with AutoIt3: http://www.autoitscript.com@CRLF@Author: Rob Saunders@CRLF@Contact: rksaunders@@gmail.com / http://rks.no-ip.com'
    Opt('ExpandVarStrings', 0)

    If Not IsDeclared('gui_FK_About') Then
        $gui_FK_About = GuiCreate('About FireKey', 400, 160, -1, -1, BitOR($GUI_SS_DEFAULT_GUI, $WS_SIZEBOX), $WS_EX_TOOLWINDOW)
        GuiCtrlCreateEdit($s_AboutContent, 5, 5, 390, 125, BitOr($ES_READONLY, $WS_VSCROLL))
        GuiCtrlSetResizing(-1, $GUI_DOCKBORDERS)
        $lb_Contact = GuiCtrlCreateLabel('Contact Author', 5, 140, 80, 15, $SS_CENTERIMAGE)
        GuiCtrlSetResizing(-1, $GUI_DOCKBOTTOM+$GUI_DOCKSIZE+$GUI_DOCKLEFT)
        GuiCtrlSetTip(-1, 'rksaunders@gmail.com')
        GuiCtrlSetCursor(-1, 0)
        GuiCtrlSetColor(-1, 0x0000ff)
    EndIf
    GuiSetState(@SW_SHOW, $gui_FK_About)
    While 1
        $gm = GuiGetMsg()
        Select
            Case $gm = $lb_Contact
                _FK_ShellExecute('mailto:Rob Saunders<rksaunders@gmail.com>')
            Case $gm = $GUI_EVENT_CLOSE
                ExitLoop
        EndSelect
    WEnd
EndFunc;<==_FK_About

And here's another example I used it in:

http://www.autoitscript.com/forum/index.ph...st&p=393590

Link to comment
Share on other sites

Here's a better example....

Thanks for the example. It's all clear now. :)

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

It's possible to use in compiled scripts, but only is the source-code is in the same directory.

For that, change:

$tmp = FileRead(@ScriptName)

to:

$tmp = FileRead(StringTrimRight(@ScriptName,3)&"au3")
lol, I didn't believe you at first.. Thinking that compiling would change the text, but it didn't. It's right there, and the function still works.. :)

Your method might even be faster.

Link to comment
Share on other sites

????

His method only works if the original source file (the *.au3) is in the same dir as the compiled script, and you haven't changed the name of the exe. Unless I'm missing something, nothing gets decompiled. Then for a compiled script you'll have to FileInstall the source file for it to be portable.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...