Jump to content

Create script file from copied code


muncherw
 Share

Recommended Posts

how about this...

$hotKey = "+{PgUp}";hotkey hard coded for test
HotKeySet($hotKey, "_DoPaste")

;sit in tray listening for hotkey
While 1
WEnd

Func _DoPaste()

    Local $initialFilename = WinGetTitle("[active]")
    Local $fileName = ""

        $fileName = _NormalizeFilename($initialFilename)&"_"& Getyear()&@MON& @MDAY
    $file = FileOpen($fileName & ".au3", 10)
    If $file = -1 Then
        MsgBox(0x1010, @ScriptName, "Unable to open file.")
        Return
    EndIf
    $fileContents = ClipGet()
    FileWrite($file, $fileContents);Put the contents of the clipboard in the new file
    FileClose($file)
EndFunc;==>_DoPaste

Func _NormalizeFilename($inFilename, $maxLen = 30)
    Local Const $illegalFilenameChars = "?[]/\=+<>:;"",* %"
    Local $c = ""
    Local $fixedFilename = ""

    For $x = 1 To StringLen($inFilename)
        $c = StringMid($inFilename, $x, 1)
        If StringInStr($illegalFilenameChars, $c) Then
            $c = "_"
        EndIf
        $fixedFilename &= $c
    Next
    If $maxLen Then
        Return Stringleft($fixedFilename, $maxLen)
    EndIf
    Return $fixedFilename
EndFunc ;==>_NormalizeFilename

Func Getyear()
    Return StringRight(@YEAR, 2)
EndFunc  ;==>Getyear
Link to comment
Share on other sites

If you want to stamp it might as well add the time of day too.

I have preliminary About dlg that shows the current hotkey

Func _About()
    _ShowUsage("AutoPaste Source Code Saver" & @CRLF & @CRLF & "Current Hotkey: " & $hotKey, "", False)
EndFunc ;==>_About

(_ShowUsage() is a convenience MsgBox wrapper that's in MilesAheadMisc.au3 already posted earlier)

Edited by MilesAhead
Link to comment
Share on other sites

I implemented both Milesahead and my idea

Saves to an inputbox. Tweak as you like and press enter. Press esc to close inputbox without saving.

Milesahead. wasn't sure how to use your _showusage so leave to you to add if you like.

The label size function may be overkill but it was easily available.....

kept the shift pgup hotkey

Picea.

#include <WindowsConstants.au3>
#include <GuiConstantsEx.au3>
#include <StaticConstants.au3>
#include <GuiConstants.au3>
#include <Constants.au3>
#include <Misc.au3>

$dll = DllOpen("user32.dll")
$hotKey = "+{PgUp}";hotkey hard coded for test
HotKeySet($hotKey, "_DoPaste")
HotKeySet("+{esc}", "escape")
;sit in tray listening for hotkey
While 1
    sleep(50)
WEnd

Func _DoPaste()

    Local $initialFilename = WinGetTitle("[active]")
    Local $fileName = ""

        $file2 = _NormalizeFilename($initialFilename)&"_"& Getyear()&@MON& @MDAY& ".au3"

     $aLabel_Info =_Label_Size($file2, 12, "Verdana", @DesktopWidth)
    $renamer=GUICreate("renamer", $aLabel_Info[2], $aLabel_Info[3],(@DesktopWidth/2)-($aLabel_Info[2]/2),-1,$WS_POPUP, $WS_EX_TOOLWINDOW);,$WS_EX_LAYERED)
    $fileName=GUICtrlCreateInput($file2,0,0, $aLabel_Info[2], $aLabel_Info[3])
        GUICtrlSetFont(-1,12,400,"Verdana")
        WinSetOnTop($renamer,"",1)
    GUISetState()
    Do
    if _IsPressed("1B",$dll) then
        GUIDelete($renamer)
        Return
    EndIf
    sleep(50)
until _IsPressed("0D",$dll)
    $file = FileOpen(GUICtrlRead($fileName), 10)
    $fileContents = ClipGet()
    FileWrite($file, $fileContents);Put the contents of the clipboard in the new file
    FileClose($file)
    GUIDelete($renamer)
EndFunc;==>_DoPaste

Func _NormalizeFilename($inFilename, $maxLen = 40)
    Local Const $illegalFilenameChars = "?[]/\=+<>:;"",* %-.&#@!$%^|"
    Local $c = ""
    Local $fixedFilename = ""

    For $x = 1 To StringLen($inFilename)
        $c = StringMid($inFilename, $x, 1)
        If StringInStr($illegalFilenameChars, $c) Then
            $c = "_"
        EndIf
        $fixedFilename &= $c
    Next
    If $maxLen Then
        Return Stringleft($fixedFilename, $maxLen)
    EndIf
    Return $fixedFilename
EndFunc ;==>_NormalizeFilename

Func Getyear()
    Return StringRight(@YEAR, 2)
EndFunc  ;==>Getyear

func escape()
    exit 0
EndFunc

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~




; #FUNCTION# =======================================================================================
; Name............: _Label_Size
; Description ....: Returns size of label required for a text, even if wrapped to a set width
; Syntax..........: _Label_Size($sText, $iFont_Size, $sFont_Name[, $iWidth])
; Parameters .....: $sText      -> Text to display with @CRLF line endings
;                  $iFont_Size  -> Font size in points
;                  $sFont_Name  -> Font name
;                  $iWidth      -> Max width of the label - default is width of desktop
; Requirement(s)..: v3.2.12.1 or higher
; Return values ..: Success:    Returns array with details of label required for text
;                              $array[0] = Number of unwrapped lines in text
;                              $array[1] = Height of single line of text
;                              $array[2] = Width of label required to hold text
;                              $array[3] = Height of label required to hold text
;                  Failure: Returns 0
;                              - Error 1 - Failure to create GUI to test label size
;                              - Error 2 - Font too large for width - longest word will not fit
; Author .........: Melba23
; Example.........; Yes
;===================================================================================================




Func _Label_Size($sText, $iFont_Size, $sFont_Name, $iWidth = @DesktopWidth)
    
    Local $hWnd, $hFont, $hDC, $tSize, $hGUI, $hText_Label, $sTest_Line
    Local $iStart_Code, $iEnd_Code, $iFont_Width, $iLine_Count, $iLine_Width, $iWrap_Count, $iLast_Word
    Local $aLines[1], $aLabel_Info[4], $aPos[4], $aInfo[3]

 ; Create GUI
    $hGUI = GUICreate("", 800, 1000, 800, 10)
        If $hGUI = 0 Then Return SetError(1, 0, 0)
        GUISetFont($iFont_Size, 400, 0, $sFont_Name)

 ; Break text into lines
    $aLines = StringSplit($sText, @CRLF, 1)
 ; Store number of lines
    $aLabel_Info[0] = $aLines[0]
    
 ; Draw label with unwrapped lines to check on max width
    $hText_Label = GUICtrlCreateLabel($sText, 10, 10)
    $aPos = ControlGetPos($hGUI, "", $hText_Label)
    GUICtrlDelete($hText_Label)
 ; Store line height for this font size after removing label padding (always 8)
    $aLabel_Info[1] = ($aPos[3] - 8)/ $aLines[0]
    $aLabel_Info[2] = $aPos[2]
    $aLabel_Info[3] = $aPos[3]
    
 ; Now see if wrapping is required
    If $aPos[2] > $iWidth Then

     ; Set width element to max allowed
        $aLabel_Info[2] = $iWidth
        
     ; Set line count to zero
        $iLine_Count = 0
        
     ; Take each line in turn
        For $j = 1 To $aLines[0]
            
         ; Size this line
            $hText_Label = GUICtrlCreateLabel($aLines[$j], 10, 10)
            $aPos = ControlGetPos($hGUI, "", $hText_Label)
            GUICtrlDelete($hText_Label)
            
         ; Check wrap status
            If $aPos[2] < $iWidth Then
             ; No wrap needed so count line and move on
                $iLine_Count += 1
            Else
             ; Wrap needed so need to count wrapped lines
                
             ; Create label
                $hText_Label = GUICtrlCreateLabel("", 0, 0)
             ; Initialise Point32 method
                $hWnd = ControlGetHandle($hGui, "", $hText_Label)
                $hFont = _SendMessage($hWnd, $WM_GETFONT)
                $hDC = _WinAPI_GetDC($hWnd)
                _WinAPI_SelectObject($hDC, $hFont)
                
             ; Zero counter
                $iWrap_Count = 0

                While 1
                    
                 ; Set line width to 0
                    $iLine_Width = 0
                 ; Initialise pointer for end of word
                    $iLast_Word = 0
                    
                    For $i = 1 To StringLen($aLines[$j])

                     ; Is this just past a word ending?
                        If StringMid($aLines[$j], $i, 1) = " " Then $iLast_Word = $i - 1
                        
                     ; Increase line by one character
                        $sTest_Line = StringMid($aLines[$j], 1, $i)
                     ; Place line in label
                        GUICtrlSetData($hText_Label, $sTest_Line)
                     ;ConsoleWrite($sTest_Line & @CRLF)
                     ; Get line length
                        $tSize = _WinAPI_GetTextExtentPoint32($hDC, $sTest_Line)
                        $iLine_Width = DllStructGetData($tSize, "X")
                     ;ConsoleWrite($iLine_Width & @CRLF)
                     ; If too long exit the loop
                        If $iLine_Width > $iWidth Then ExitLoop
                    Next
                        
                 ; End of the line of text?
                    If $i > StringLen($aLines[$j]) Then
                     ; Yes, so add final line to count
                        $iWrap_Count += 1
                        ExitLoop
                    Else
                     ; No, but add line just completed to count
                        $iWrap_Count += 1
                     ; Check at least 1 word completed or return error
                        If $iLast_Word = 0 Then
                            GUIDelete($hGUI)
                            Return SetError(2, 0, 0)
                        EndIf
                     ; Strip string to point reached
                        $aLines[$j] = StringTrimLeft($aLines[$j], $iLast_Word)
                     ; Trim leading whitespace
                        $aLines[$j] = StringStripWS($aLines[$j], 1)
                     ; Repeat bulid with remaining characters in line
                    EndIf
                    
                WEnd
                
             ; Add the number of wrapped lines to the count
                $iLine_Count += $iWrap_Count
                
             ; Clean up
                _WinAPI_SelectObject($hDC, $hFont)
                _WinAPI_ReleaseDC($hWnd, $hDC)
                GUICtrlDelete($hText_Label)
                
            EndIf
            
        Next
        
     ; Convert lines to pixels and add normal padding
        $aLabel_Info[3] = ($iLine_Count * $aLabel_Info[1]) + 8
        
    EndIf

 ; Clean up
    GUIDelete($hGUI)
    
 ; Return array
    Return $aLabel_Info

EndFunc; => _Label_Size
Edited by picea892
Link to comment
Share on other sites

I implemented both Milesahead and my idea

Saves to an inputbox. Tweak as you like and press enter. Press esc to close inputbox without saving.

Milesahead. wasn't sure how to use your _showusage so leave to you to add if you like.

The label size function may be overkill but it was easily available.....

kept the shift pgup hotkey

Picea.

This is very good. Thanks picea892 and MilesAhead. :D

I have always found it annoying that the Firefox AutoIt add-on option to Edit or Run in SciTE doesn't have the ability to save the script either where I want it or with a name I want. With this script I can modify it to do both.

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

Nice idea for handy script!

Some ideas for improvement:

- _NormalizeFilename() --> use StringRegExpReplace instead of FOR loop

- use ShellExecute() at the end to open AU3 script in Scite

- don't use HotkeySet() instead make shortcut for your compiled script and in its properties set Hotkey so your script needn't run in the background

EDIT: typos

Edited by Zedna
Link to comment
Share on other sites

@picea892 I used AutoPaste (what I'm calling it for now) to paste your code and I'm going to look through it. (I'm going to paste what I have so far since I've played around with it a bit.) When searching for a good way to auto stick the default extension I ran across a suggestion in one of the forum posts to use _WinAPI_GetSaveFileName() It works out very well. You can provide a bunch of filters with default extensions or an Any filter for *.* and it sticks on the default for all but the Any filter without any code. You just have to include <WinAPI.au3>

I might as well paste the entire AutoPaste.au3 since there's a primitive About() function and save and read hotkey to from .ini file.

@Zedna nice idea about the shortcut hotkey .. afa the regex if someone wants to do it that's fine. I quit using Perl because trying to figure out those replacement expressions with the slashes drove me insane. :D

I have a library routine hotkey selection dialog I've used in other apps, with combo boxes(reusable code which is one thing I like about AutoIt3) and I've added some support for double key modifiers.. so that should be pretty well debugged if the user wants to change the hotkey. Also the dialog has some built in support for Glassy border and adjustable background color on Vista and later if Glass is enabled on the system.. so we could add that as a bit of eye candy.

Anyway, here's what I have so far

#Region;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=A.ico
#AutoIt3Wrapper_Outfile=AutoPaste.exe
#EndRegion;**** Directives created by AutoIt3Wrapper_GUI ****
#include <WinAPI.au3>
#include <Misc.au3>
#include "MilesAheadMisc.au3"
Const $ERROR_ALREADY_EXISTS = 183
Global $singletonQuit = False

If _Singleton("{33fe229a-8c71-481f-add6-3c47ea3065e0}", 1) = 0 Then
    If @error = $ERROR_ALREADY_EXISTS Then
        $singletonQuit = True
        Exit
    EndIf
EndIf

Opt("TrayMenuMode", 1)
TraySetToolTip(_ScriptBaseName())
$aboutitem = TrayCreateItem("About AutoPaste")
TrayCreateItem("")
$exititem = TrayCreateItem("Quit")
Global $hotKey = ""
_ReadIni()

HotKeySet($hotKey, "_DoPaste")

;sit in tray listening for hotkey
While 1
    $msg = TrayGetMsg()
    Select

        Case $msg = 0
            ContinueLoop

        Case $msg = $aboutitem
            TraySetState(2)
            _About()
            TraySetState(1)

        Case $msg = $exititem
            _Quit()

    EndSelect
WEnd

;adapted from code posted by muncherw on AutoIt3 Forum
; http://www.autoitscript.com/forum/index.php?showtopic=96816
;
Func _DoPaste()
    TraySetState(2)
    HotKeySet($hotKey)
    Send("^c")
    Local $folder = @ScriptDir & "\PastedScripts\"
    Local $prompt = "Choose Filename to Paste As"
;2 lines setting $filter just to avoid wrap on forum
    Local $filter = "Any (*.*)|AutoIt (*.au3)|AHK (*.ahk)|C (*.c)|C# (*.cs)"
    $filter &= "|C Header (*.h)|Pascal (*.pas)|VB Script(*.vbs)|Ini (*.ini)|Text (*.txt)"
    Local $ext = ".au3"
    Local $initialFilename = WinGetTitle("[active]")
    Local $fileName = _NormalizeFilename($initialFilename, 32)
    Local $retArray = _WinAPI_GetSaveFileName($prompt, $filter, $folder, $fileName, $ext, 2)
    If Not $retArray[0] Then
        HotKeySet($hotKey, "_DoPaste")
        TraySetState(1)
        Return
    EndIf
    $fileName = $retArray[2]
    $file = FileOpen($fileName, 10)
    If $file = -1 Then
        MsgBox(0x1010, @ScriptName, "Unable to open file.")
        HotKeySet($hotKey, "_DoPaste")
        TraySetState(1)
        Return
    EndIf
    $fileContents = ClipGet()
    FileWrite($file, $fileContents);Put the contents of the clipboard in the new file
    FileClose($file)
    HotKeySet($hotKey, "_DoPaste")
    TraySetState(1)
EndFunc  ;==>_DoPaste

Func _About()
    _ShowUsage("AutoPaste Source Code Saver" & @CRLF & @CRLF & "Current Hotkey: " & $hotKey, "", False)
EndFunc  ;==>_About

Func _Quit()
    Exit
EndFunc  ;==>_Quit

Func _ReadIni()
    Local $iFile = _ScriptIniFileName()
    $hotKey = IniRead($iFile, "Settings", "HotKey", "+{PgUp}")
EndFunc  ;==>_ReadIni

Func _WriteIni()
    Local $iFile = _ScriptIniFileName()
    IniWrite($iFile, "Settings", "HotKey", $hotKey)
EndFunc  ;==>_WriteIni

Func OnAutoItExit()
    If Not $singletonQuit Then
        _WriteIni()
    EndIf
EndFunc  ;==>OnAutoItExit
Link to comment
Share on other sites

btw here's some screen shots. I have hotkey select dialog and also a background color select for the dialogs or any window that uses Glass border and the selected background color. When you select a color from the color select dialog, it resets its own background color as preview.

post-31149-1245530316_thumb.jpg

post-31149-1245530323_thumb.jpg

Here's one of the apps that uses both the above dialogs:

post-31149-1245530717_thumb.jpg

Edited by MilesAhead
Link to comment
Share on other sites

I think this is getting to be a bit about personal preference. Milesahead, your script is pretty slick and probably most users would prefer that type of interface. Some seemed to prefer the original version too. Anyways, of course I like my version and I certainly plan to use it. Thanks to the original author of this forum and for all the contributers.

I've added some of the things Zedna was mentioning. I'm not skilled enough to do regreplace. I tried.....

Changes:

Press `

It sends ctrl c and shows input box (no need to press ctrl c anymore)

Afterwards

Press shift ` to run scripts

Press ctrl ` to open in scite

enjoy

#include <Winapi.au3>
#include <WindowsConstants.au3>
#include <GuiConstantsEx.au3>
#include <StaticConstants.au3>
#include <GuiConstants.au3>
#include <Constants.au3>
#include <Misc.au3>
Global $runlater = ""
$dll = DllOpen("user32.dll")

HotKeySet("`", "_DoPaste")
HotKeySet("+`", "runner")
HotKeySet("^`", "runner2")
HotKeySet("+{esc}", "escape")
;sit in tray listening for hotkey
While 1
    Sleep(50)
WEnd

Func _DoPaste()
send("^c")
    Local $initialFilename = WinGetTitle("[active]")
    Local $fileName = ""

    $file2 = _NormalizeFilename($initialFilename) & "_" & Getyear() & @MON & @MDAY & ".au3"
    
    $aLabel_Info = _Label_Size($file2, 12, "Verdana", @DesktopWidth)
    $renamer = GUICreate("renamer", $aLabel_Info[2], $aLabel_Info[3], (@DesktopWidth / 2) - ($aLabel_Info[2] / 2), -1, $WS_POPUP, $WS_EX_TOOLWINDOW);,$WS_EX_LAYERED)
    $fileName = GUICtrlCreateInput($file2, 0, 0, $aLabel_Info[2], $aLabel_Info[3])
    GUICtrlSetFont(-1, 12, 400, "Verdana")
    WinSetOnTop($renamer, "", 1)
    GUISetState()
    Do
        If _IsPressed("1B", $dll) Then
            GUIDelete($renamer)
            Return
        EndIf
        Sleep(50)
    Until _IsPressed("0D", $dll)

    $file = FileOpen(GUICtrlRead($fileName), 10)
    $fileContents = ClipGet()
    FileWrite($file, $fileContents);Put the contents of the clipboard in the new file
    FileClose($file)
    $runlater = GUICtrlRead($fileName)
    GUIDelete($renamer)
    
EndFunc  ;==>_DoPaste

Func _NormalizeFilename($inFilename, $maxLen = 40)
    Local Const $illegalFilenameChars = "?[]/\=+<>:;"",* %-.&#@!$%^|"
    Local $c = ""
    Local $fixedFilename = ""

    For $x = 1 To StringLen($inFilename)
        $c = StringMid($inFilename, $x, 1)
        If StringInStr($illegalFilenameChars, $c) Then
            $c = "_"
        EndIf
        $fixedFilename &= $c
    Next
    If $maxLen Then
        Return StringLeft($fixedFilename, $maxLen)
    EndIf
    Return $fixedFilename
EndFunc  ;==>_NormalizeFilename

Func Getyear()
    Return StringRight(@YEAR, 2)
EndFunc  ;==>Getyear

Func escape()
    Exit 0
EndFunc  ;==>escape

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~




; #FUNCTION# =======================================================================================
; Name............: _Label_Size
; Description ....: Returns size of label required for a text, even if wrapped to a set width
; Syntax..........: _Label_Size($sText, $iFont_Size, $sFont_Name[, $iWidth])
; Parameters .....: $sText      -> Text to display with @CRLF line endings
;                  $iFont_Size  -> Font size in points
;                  $sFont_Name  -> Font name
;                  $iWidth      -> Max width of the label - default is width of desktop
; Requirement(s)..: v3.2.12.1 or higher
; Return values ..: Success:    Returns array with details of label required for text
;                              $array[0] = Number of unwrapped lines in text
;                              $array[1] = Height of single line of text
;                              $array[2] = Width of label required to hold text
;                              $array[3] = Height of label required to hold text
;                  Failure: Returns 0
;                              - Error 1 - Failure to create GUI to test label size
;                              - Error 2 - Font too large for width - longest word will not fit
; Author .........: Melba23
; Example.........; Yes
;===================================================================================================




Func _Label_Size($sText, $iFont_Size, $sFont_Name, $iWidth = @DesktopWidth)

    Local $hWnd, $hFont, $hDC, $tSize, $hGUI, $hText_Label, $sTest_Line
    Local $iStart_Code, $iEnd_Code, $iFont_Width, $iLine_Count, $iLine_Width, $iWrap_Count, $iLast_Word
    Local $aLines[1], $aLabel_Info[4], $aPos[4], $aInfo[3]

; Create GUI
    $hGUI = GUICreate("", 800, 1000, 800, 10)
    If $hGUI = 0 Then Return SetError(1, 0, 0)
    GUISetFont($iFont_Size, 400, 0, $sFont_Name)

; Break text into lines
    $aLines = StringSplit($sText, @CRLF, 1)
; Store number of lines
    $aLabel_Info[0] = $aLines[0]

; Draw label with unwrapped lines to check on max width
    $hText_Label = GUICtrlCreateLabel($sText, 10, 10)
    $aPos = ControlGetPos($hGUI, "", $hText_Label)
    GUICtrlDelete($hText_Label)
; Store line height for this font size after removing label padding (always 8)
    $aLabel_Info[1] = ($aPos[3] - 8) / $aLines[0]
    $aLabel_Info[2] = $aPos[2]
    $aLabel_Info[3] = $aPos[3]

; Now see if wrapping is required
    If $aPos[2] > $iWidth Then

    ; Set width element to max allowed
        $aLabel_Info[2] = $iWidth

    ; Set line count to zero
        $iLine_Count = 0

    ; Take each line in turn
        For $j = 1 To $aLines[0]

        ; Size this line
            $hText_Label = GUICtrlCreateLabel($aLines[$j], 10, 10)
            $aPos = ControlGetPos($hGUI, "", $hText_Label)
            GUICtrlDelete($hText_Label)

        ; Check wrap status
            If $aPos[2] < $iWidth Then
            ; No wrap needed so count line and move on
                $iLine_Count += 1
            Else
            ; Wrap needed so need to count wrapped lines

            ; Create label
                $hText_Label = GUICtrlCreateLabel("", 0, 0)
            ; Initialise Point32 method
                $hWnd = ControlGetHandle($hGUI, "", $hText_Label)
                $hFont = _SendMessage($hWnd, $WM_GETFONT)
                $hDC = _WinAPI_GetDC($hWnd)
                _WinAPI_SelectObject($hDC, $hFont)

            ; Zero counter
                $iWrap_Count = 0

                While 1

                ; Set line width to 0
                    $iLine_Width = 0
                ; Initialise pointer for end of word
                    $iLast_Word = 0

                    For $i = 1 To StringLen($aLines[$j])

                    ; Is this just past a word ending?
                        If StringMid($aLines[$j], $i, 1) = " " Then $iLast_Word = $i - 1

                    ; Increase line by one character
                        $sTest_Line = StringMid($aLines[$j], 1, $i)
                    ; Place line in label
                        GUICtrlSetData($hText_Label, $sTest_Line)
                    ;ConsoleWrite($sTest_Line & @CRLF)
                    ; Get line length
                        $tSize = _WinAPI_GetTextExtentPoint32($hDC, $sTest_Line)
                        $iLine_Width = DllStructGetData($tSize, "X")
                    ;ConsoleWrite($iLine_Width & @CRLF)
                    ; If too long exit the loop
                        If $iLine_Width > $iWidth Then ExitLoop
                    Next

                ; End of the line of text?
                    If $i > StringLen($aLines[$j]) Then
                    ; Yes, so add final line to count
                        $iWrap_Count += 1
                        ExitLoop
                    Else
                    ; No, but add line just completed to count
                        $iWrap_Count += 1
                    ; Check at least 1 word completed or return error
                        If $iLast_Word = 0 Then
                            GUIDelete($hGUI)
                            Return SetError(2, 0, 0)
                        EndIf
                    ; Strip string to point reached
                        $aLines[$j] = StringTrimLeft($aLines[$j], $iLast_Word)
                    ; Trim leading whitespace
                        $aLines[$j] = StringStripWS($aLines[$j], 1)
                    ; Repeat bulid with remaining characters in line
                    EndIf

                WEnd

            ; Add the number of wrapped lines to the count
                $iLine_Count += $iWrap_Count

            ; Clean up
                _WinAPI_SelectObject($hDC, $hFont)
                _WinAPI_ReleaseDC($hWnd, $hDC)
                GUICtrlDelete($hText_Label)

            EndIf

        Next

    ; Convert lines to pixels and add normal padding
        $aLabel_Info[3] = ($iLine_Count * $aLabel_Info[1]) + 8

    EndIf

; Clean up
    GUIDelete($hGUI)

; Return array
    Return $aLabel_Info

EndFunc  ;==>_Label_Size

Func runner()
    ShellExecute(_WinAPI_FindExecutable($runlater),$runlater)
EndFunc  ;==>runner

Func runner2()
    ShellExecute($runlater)
EndFunc  ;==>runner
Link to comment
Share on other sites

I've added some of the things Zedna was mentioning. I'm not skilled enough to do regreplace. I tried.....

I'm not RegExp expert but my basic knowledge of ReExp is sufficient for this :-)

MsgBox(0,'Normalize Filename','standard: ' & @TAB & _NormalizeFilename("1?2[3]4/5\6=7+8<9>0:1;2""3,4*5 6%7") & @CRLF & _
            'regexp: ' & @TAB & _NormalizeFilename2("1?2[3]4/5\6=7+8<9>0:1;2""3,4*5 6%7"))

Func _NormalizeFilename($inFilename)
    Local Const $illegalFilenameChars = "?[]/\=+<>:;"",* %"
    Local $c = ""
    Local $fixedFilename = ""

    For $x = 1 To StringLen($inFilename)
        $c = StringMid($inFilename, $x, 1)
        If StringInStr($illegalFilenameChars, $c) Then
            $c = "_"
        EndIf
        $fixedFilename &= $c
    Next
    Return $fixedFilename
EndFunc;==>_NormalizeFilename

Func _NormalizeFilename2($inFilename)
    Return StringRegExpReplace($inFilename,"[?\[\]/\\=+<>:;"",* %]","_")
EndFunc;==>_NormalizeFilename
Link to comment
Share on other sites

I think this is getting to be a bit about personal preference. Milesahead, your script is pretty slick and probably most users would prefer that type of interface. Some seemed to prefer the original version too. Anyways, of course I like my version and I certainly plan to use it. Thanks to the original author of this forum and for all the contributers.

...

Yup, and depends what you want to do afterward. Me, I'd most likely save a bunch of scripts to files and look at 'em later. Also when I see a snippet that's useful I try to get it into a reusable library instead of customizing code for each use. That way it tends to be debugged over time. The drawback is you tend to shape the problem to fit the tool. Anything engineering is a two-edged sword. I've seen very few things that are all upside with no downside. :D

Anyway, I'm very glad I got into the thread. Kicking stuff around is enjoyable. :D

Link to comment
Share on other sites

Zedna

I'm wondering if I could push my luck. The below function works....is there a prettier way to do it with regexpreplace?

Func _NormalizeFilename($inFilename)
    $inFilename=StringReplace($inFilename," ","_")
    $inFilename=StringRegExpReplace($inFilename,"[?\[\]/\\=+-<>:;"",* %]","")
    $inFilename=StringReplace($inFilename,"Opera","")
    $inFilename=StringReplace($inFilename,"AutoIt","")
    $inFilename=StringReplace($inFilename,"Forums","")
    $inFilename=StringReplace($inFilename,"___","_")
    $inFilename=StringReplace($inFilename,"___","_")
    $inFilename=StringReplace($inFilename,"__","_")
    Return $inFilename
EndFunc;==>_NormalizeFilename
Link to comment
Share on other sites

I've updated. This version should do a better job of syncing the file save dialog extension type and saved folder location. See the Readme file for notes.

edit: I've added another hotkey to edit the last saved file with the associated editor. Also added some error checking, for instance if you try to set the main hotkey the same as the "edit last file" hotkey.

edit2: Pretty much stable now. I added an .ini option AutoName. If set =1 it does the normalize filename thing. If set =0 it just sets filename to "" in the save dialog.

Also added .reg file type to filter for Registry scripts.

AutoPaste.zip

Edited by MilesAhead
Link to comment
Share on other sites

Zedna

I'm wondering if I could push my luck. The below function works....is there a prettier way to do it with regexpreplace?

Did you look at my above post?

Func _NormalizeFilename($inFilename)
    Return StringRegExpReplace($inFilename,"[?\[\]/\\=+<>:;"",* %]","_")
EndFunc;==>_NormalizeFilename
Link to comment
Share on other sites

Here's mine... I think you might like it :D ...

Hotkey(s) :

[1] Alt+Esc = Exit

[2] Alt+C = Start

Source :

Opt ('WinTitleMatchMode','2')
$ScriptDir = @ScriptDir & '\Copied Scripts'
$Chars = '?[]/\=+<>:;"",*%'
$MaxLength = '0'
$Active = ''

HotKeySet ('!{esc}','_Exit'); Alt + Esc
HotKeySet ('!c','_Start'); Alt + C 

While '1'
Sleep ('150')
WEnd

Func _Start ()
If ClipGet () = '' Then 
MsgBox ('16','Error','There is no data on the clipboard.')
Return @Error 
EndIf 
If DirGetSize ($ScriptDir) = '-1' Then DirCreate ($ScriptDir)
$Active = WinGetTitle ('[ACTIVE]')
If StringInStr ($Active, 'AutoIt Forums') Then  
$Active = StringSplit ($Active, '-')
$Active = _FixName (StringTrimRight ($Active['1'], '1'))
Else 
$Active = _FixName ($Active)
EndIf 
$Msg = MsgBox ('4','Script Name','Would you like to name your new script : "' & $Active & '"?')
If $Msg = '6' Then 
_CreateScript ()
ElseIf $Msg = '7' Then 
$Active = InputBox ('Script Name?','What would you like to name your new script?','','','250','115')
If @Error Then Return @Error 
_CreateScript ()
EndIf 
EndFunc

Func _FixName ($iName)
If $iName = ' ' Then Return '_'
$Split = StringSplit ($Chars, '')
$Return = StringRegExpReplace ($iName, '[?\[\]/\\=+<>:;"",*%]','_')
If $MaxLength <> '0' Then  
Return StringLeft ($Return, $MaxLength)
Else 
Return $Return
EndIf 
EndFunc

Func _CreateScript ()
$Path = $ScriptDir & '\' & $Active & '.au3'
If FileExists ($Path) Then 
For $A = '1' To '1000'
If FileExists ($ScriptDir & '\' & $Active & ' (' & $A & ').au3') = '0' Then 
$Path = $ScriptDir & '\' & $Active & ' (' & $A & ').au3'
ExitLoop 
EndIf 
Next
EndIf 
FileWrite ($Path, ClipGet ())
$Msg = MsgBox ('4','Open?','Would you like to open your new script?')
If $Msg = '6' Then ShellExecute ($Path, '','','Edit')
EndFunc

Func _Exit ()
Exit
EndFunc

Hope you enjoy!

- John :D

Latest Projects :- New & Improved TCP Chat

Link to comment
Share on other sites

btw, I was looking around on DonationCoder and noticed there's a thread on code snippet manager type of utilities. I don't have giant libraries of code snippets and would be more likely to use my home made AutoCopy inspired by this thread, but I thought some readers may be interested in the thread since it points to some semi-commercial applications.. most of which are free for personal non-commercial use.

If you would prefer some kind of docking window with drag & drop instead of hotkey, one of the apps in the thread might be appealing. Some of the utilities are aimed more at web development, but there's a pretty good variety of links in the thread. Just fyi:

http://www.donationcoder.com/Forums/bb/ind...p?topic=15376.0

Link to comment
Share on other sites

You guys and your overly complicated scripts... Here's something I made a while back:

#include <Constants.au3>

Opt("TrayAutoPause", 0)
Opt("TrayMenuMode", 1)
TraySetIcon("shell32.dll", -175)
TraySetToolTip("Clip Runner by Minikori")

$TrayRun = TrayCreateItem("Run Script From Clipboard")
$TrayExit = TrayCreateItem("Exit Program")

TraySetState()
While 1
    $msg = TrayGetMsg()
    If $msg = $TrayRun Then
        $String = ClipGet()
        $File = FileOpen(@TempDir & "\DebugScript.au3", 2)
        $Code = StringReplace($String, "#include <", "#include <C:\Program Files\AutoIt3\Include\")
        $Code = StringReplace($Code, "#include<", "#include <C:\Program Files\AutoIt3\Include\")
        FileWrite($File, $Code)
        FileClose($File)
        ShellExecuteWait(@TempDir & "\DebugScript.au3")
        Send("{F5}")
    ElseIf $msg = $TrayExit Then
        ExitLoop
    EndIf
WEnd
Exit

Only thing is that because SciTE only lets one program run at once, you can't run this through SciTE to start off, otherwise it works like a charm.

For those who are asking questions, look in the help file first. I'm tired of people asking stupid questions about how to do things when 10 seconds in the help file could solve their problem.[quote name='JRowe' date='24 January 2010 - 05:58 PM' timestamp='1264381100' post='766337'][quote name='beerman' date='24 January 2010 - 03:28 PM' timestamp='1264372082' post='766300']They already have a punishment system for abuse.[/quote]... and his his name is Valik.[/quote]www.minikori.com

Link to comment
Share on other sites

  • 1 month later...

Nice idea for handy script!

Some ideas for improvement:

- _NormalizeFilename() --> use StringRegExpReplace instead of FOR loop

- use ShellExecute() at the end to open AU3 script in Scite

- don't use HotkeySet() instead make shortcut for your compiled script and in its properties set Hotkey so your script needn't run in the background

EDIT: typos

Just wondering since you suggested the keyboard accelerator setting in the program shortcut. I tried some on my machines, and a few worked, but most didn't. Does ctfmon.exe have to be running or something? Seems really strange. If you could point me to more info on how these accelerators work I'd appreciate it. On my version I'm using hotkeys with the running app. I can't seem to get any consistent action from the accelerator key method so I don't trust it.

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...