Paste multi line long text code snippets with special characters without any lag in pasting the script text

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
sapnachrishore
Posts: 3
Joined: 03 Sep 2020, 15:24

Paste multi line long text code snippets with special characters without any lag in pasting the script text

Post by sapnachrishore » 03 Sep 2020, 15:58

I'm trying to paste multi line long text code snippets with special characters without any lag in pasting the script text

Currently when pasting the script with the following script, it pastes the script but it does not maintain the same line spacing and the paste itself does not paste the characters in order, so there is garbled text. I tried looking around but could not find it.

Essentially, if you have some long text with special characters how do you ensure with some hot key same text is pasted. Any suggestions will be greatly appreciated. Thank you.

Code: Select all

;shrink
^8::
MyMsg=
(
/*
-- Shrink_DB_File.sql
This script is used to shrink a database file in
increments until it reaches a target free space limit.

Run this script in the database with the file to be shrunk.
1. Set @DBFileName to the name of database file to shrink.
2. Set @TargetFreeMB to the desired file free space in MB after shrink.
3. Set @ShrinkIncrementMB to the increment to shrink file by in MB 
4. Run the script

http www.sqlteam.com /forums/topic.asp?TOPIC_ID=80355  Broken Link for safety
*/

declare @DBFileName sysname
declare @TargetFreeMB int
declare @ShrinkIncrementMB int

-- Set Name of Database file to shrink
-- RUN THIS SCRIPT IN TARGET DB
set @DBFileName = 'ENTERDBNAME'

-- Set Desired file free space in MB after shrink
set @TargetFreeMB = 0

-- Set Increment to shrink file by in MB
set @ShrinkIncrementMB = 1024
)
SendInput {Text} %MyMsg%
Return
Last edited by sapnachrishore on 04 Sep 2020, 00:15, edited 2 times in total.

User avatar
mikeyww
Posts: 26885
Joined: 09 Sep 2014, 18:38

Re: Paste multi line long text code snippets with special characters without any lag in pasting the script text

Post by mikeyww » 03 Sep 2020, 20:58

Easy. Just copy it to the Windows clipboard. You can then issue Send ^v to paste it.

Code: Select all

Clipboard = A bunch of stuff.
Sleep, 70
Send ^v

sapnachrishore
Posts: 3
Joined: 03 Sep 2020, 15:24

Re: Paste multi line long text code snippets with special characters without any lag in pasting the script text

Post by sapnachrishore » 03 Sep 2020, 23:04

Thank you very much Mikeyww. I used your idea and came up with this code and it worked!

I have one computer that I connect to that does not paste enabled on it for some legitimate reasons. I maintain that server and sometimes I have to run some long scripts on it. If I use this way, I will not be able to paste the text. If I use the method earlier with SendPut {Text} I had the other issues I had earlier. Is there a way to accommodate this scenario. I know this may not be a common scenario but I deal this computer quite often. Many thanks for any inputs.

Code: Select all

;shrink
^8::
myvar =
(
/*
-- Shrink_DB_File.sql
This script is used to shrink a database file in
increments until it reaches a target free space limit.

Run this script in the database with the file to be shrunk.
1. Set @DBFileName to the name of database file to shrink.
2. Set @TargetFreeMB to the desired file free space in MB after shrink.
3. Set @ShrinkIncrementMB to the increment to shrink file by in MB 
4. Run the script

http www.sqlteam.com /forums/topic.asp?TOPIC_ID=80355  Broken Link for safety
*/

declare @DBFileName sysname
declare @TargetFreeMB int
declare @ShrinkIncrementMB int

-- Set Name of Database file to shrink
-- RUN THIS SCRIPT IN TARGET DB
set @DBFileName = 'ENTERDBNAME'

-- Set Desired file free space in MB after shrink
set @TargetFreeMB = 0

-- Set Increment to shrink file by in MB
set @ShrinkIncrementMB = 1024
)
Clipboard = %myvar%
Sleep, 70
Send ^v


User avatar
mikeyww
Posts: 26885
Joined: 09 Sep 2014, 18:38

Re: Paste multi line long text code snippets with special characters without any lag in pasting the script text

Post by mikeyww » 04 Sep 2020, 04:58

You probably just need to slow it down.

Code: Select all

SetKeyDelay, 30 ; Default delay is 10 ms; decrease or increase if needed; could try 20
SendEvent %myvar%
Interesting information at https://stackoverflow.com/questions/27396116/quickly-send-long-text-in-autohotkey, but I don't think it will help you.

sapnachrishore
Posts: 3
Joined: 03 Sep 2020, 15:24

Re: Paste multi line long text code snippets with special characters without any lag in pasting the script text

Post by sapnachrishore » 04 Sep 2020, 12:16

Thank you very very much mikeyww for your timely help.

I ended up with the following code with [Text] added as it was skipping plus signs and single quotes interpreted sometimes as double quotes.

Code: Select all

SetKeyDelay, 60 ; Default delay is 10 ms; decrease or increase if needed; could try 20
SendEvent {Text} %myvar%
Once again thank you!

User avatar
mikeyww
Posts: 26885
Joined: 09 Sep 2014, 18:38

Re: Paste multi line long text code snippets with special characters without any lag in pasting the script text

Post by mikeyww » 04 Sep 2020, 13:01

Very good. You're right, the characters ^+!#{} require special handling, and {Text} would work for that here.

gleamer1337
Posts: 1
Joined: 18 Jan 2021, 09:28

Re: Paste multi line long text code snippets with special characters without any lag in pasting the script text

Post by gleamer1337 » 18 Jan 2021, 09:56

mikeyww wrote:
04 Sep 2020, 13:01
Very good. You're right, the characters ^+!#{} require special handling, and {Text} would work for that here.
Hello! Can you help with where exactly put {Text} ? If I put it like in the script below - it just pastes 2 symbols - ^v

Code: Select all

;shrink
^8::
myvar =
(
/*
-- Shrink_DB_File.sql
This script is used to shrink a database file in
increments until it reaches a target free space limit.

Run this script in the database with the file to be shrunk.
1. Set @DBFileName to the name of database file to shrink.
2. Set @TargetFreeMB to the desired file free space in MB after shrink.
3. Set @ShrinkIncrementMB to the increment to shrink file by in MB 
4. Run the script

http www.sqlteam.com /forums/topic.asp?TOPIC_ID=80355  Broken Link for safety
*/

declare @DBFileName sysname
declare @TargetFreeMB int
declare @ShrinkIncrementMB int

-- Set Name of Database file to shrink
-- RUN THIS SCRIPT IN TARGET DB
set @DBFileName = 'ENTERDBNAME'

-- Set Desired file free space in MB after shrink
set @TargetFreeMB = 0

-- Set Increment to shrink file by in MB
set @ShrinkIncrementMB = 1024
)
Clipboard = %myvar%
Sleep, 70
Send {Text} ^v

User avatar
mikeyww
Posts: 26885
Joined: 09 Sep 2014, 18:38

Re: Paste multi line long text code snippets with special characters without any lag in pasting the script text

Post by mikeyww » 18 Jan 2021, 11:30

With a continuation block like that, you won't need it. Since you are using ^ as Ctrl, just use Send ^v.

https://www.autohotkey.com/docs/commands/Send.htm#SendText

I don't know a fast way without the paste, except that if you are using an edit field, you can use Control, EditPaste.

User avatar
mixer
Posts: 9
Joined: 25 Nov 2021, 00:46

simple snippets for Ssms.exe

Post by mixer » 01 Dec 2021, 05:28

2021-12-01_20-23-06.png
2021-12-01_20-23-06.png (9.66 KiB) Viewed 1492 times
AutoHotkey_sql.ahk

Code: Select all

CodeEditorPath = %A_AppData%\..\Local\Programs\Microsoft VS Code\Code.exe

BuidMenuFromFile()

#IfWinActive ahk_exe Ssms.exe
    !Enter::
        MouseGetPos, x, y
        MouseMove, x+1, y+1
        menu, SqlMenuFromFile, Show, % x + 10, % y + 10
        return
#IfWinActive

BuidMenuFromFile(){
    ItemCounter := 1
    Loop
    {
        FileReadLine, line, %a_scriptdir%\menu.txt, %A_Index%
        if ErrorLevel
            break
        if (SubStr(LTrim(line), 1 , 1) == "~"){
            menu, SqlMenuFromFile, Add, % SubStr(LTrim(line), 2), MenuHandler
        }
    }
    menu, SqlMenuFromFile, Add
    menu, SqlMenuFromFile, Add, &Edit snippets, MenuEditHandler
    return    

    MenuEditHandler:
        global CodeEditorPath
        Run, %CodeEditorPath% %a_scriptdir%\menu.txt
        return    

    MenuHandler:
        InItem := False
        SetCusrsor := False
        CursorXPos := 0
        CursorYOffset := 0
        OutText := False
        Loop
        {
            FileReadLine, line, %a_scriptdir%\menu.txt, %A_Index%
            if ErrorLevel
                break
            ; прервать если новый item
            if (InItem && SubStr(LTrim(line), 1 , 1) == "~"){
                InItem := False
                Break
            }
            ; начать если нужный нам item
            if (SubStr(LTrim(line), 2) == A_ThisMenuItem){
                InItem := True
                Continue
            }
            ; вывести строку
            if (InItem){
                if (CursorXPos > 0){
                    CursorYOffset := CursorYOffset + 1
                }
                if (CursorXPos == 0){
                    CursorXPos := InStr(line, "%%")
                    if (CursorXPos > 0){
                        SetCusrsor := True
                    }
                }
                line := StrReplace(line, "%%" , "")
                if (OutText){
                    Send {Enter}
                }
                Send {Home}
                SendRaw % line
                OutText :=  True
            }
        }
        ; установка позиции курсора если нужно
        if(SetCusrsor){
            if (CursorYOffset > 0){
                Loop %CursorYOffset% {
                    Send {Up}
                }
            }
            Send {End}{Home}{Home}
            if (CursorXPos > 1){
                CursorXPos := CursorXPos - 1
                Loop %CursorXPos% {
                    Send {Right}
                }
            }
        }
        return    
}
and menu.txt file

Code: Select all

~USE
USE MainDB

~SELECT
SELECT
    %%
FROM
    Simple S

~SELECT FULL
SELECT
    *
FROM
    %% T
WHERE
    T.X = 1

~DECLARE @Temp TABLE
DECLARE @%% TABLE (Id INT)

~UPDATE
UPDATE
    T
SET
    T. =
FROM
    %%

Post Reply

Return to “Ask for Help (v1)”