Need Help with Script for Cleaning up JSON Files for pasting

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
cws
Posts: 27
Joined: 02 Oct 2017, 18:11

Need Help with Script for Cleaning up JSON Files for pasting

Post by cws » 05 Oct 2022, 17:11

I find myself needing to remove the quotes, commas and leading spaces from JSON files for pasting into excel over and over. I am trying to do it with autohotkey but I am really just a hack and can't quite get the leading spaces to be removed. Also, something about my script is causing some instability with my computer where the keyboard starts doing unpredictable things sometimes after I execute the script. Usually CTRL+ALT+DEL solves the instability, but I think this is just further evidence I don't actually know what I am doing :oops:

I've mashed up some old scripts that seemed to work well with some other stuff I am trying to pull from various old forums etc and it just won't do what I want, the leading spaces are staying. Typically this is many lines of JSON - up to 100 or more at times.

Code: Select all

!+t::

SplashTextOn ,,,Working,
ClipWait, 1
Sleep, -1
if !ErrorLevel 
{
     original := Clipboard 
     stripped := StrReplace(original, """")
	 stripped2 := StrReplace(stripped, ",")
	 AutoTrim, On
	 stripped3 = %stripped2%
	 Clipboard := stripped3
}
SplashTextOff
Return

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

Re: Need Help with Script for Cleaning up JSON Files for pasting

Post by mikeyww » 06 Oct 2022, 06:30

Clear the clipboard before setting it and ClipWaiting.

Code: Select all

!+t::
Clipboard =
Send ^c
ClipWait, 0
If !ErrorLevel {
 stripped  := StrReplace(Clipboard, """")
 stripped  := StrReplace(stripped , ",")
 Clipboard := Trim(stripped)
 SoundBeep, 1500
} Else MsgBox, 48, Error, An error occurred while waiting for the clipboard.
Return

cws
Posts: 27
Joined: 02 Oct 2017, 18:11

Re: Need Help with Script for Cleaning up JSON Files for pasting

Post by cws » 06 Oct 2022, 11:46

Thanks, that looks helpful. However, Trim does not seem to work, the only line that gets trimmed is the first line and all subsequent lines still have leading spaces

Also, it seems like you strip the clipboard and then send CTRL+C, so the window with the selected text still needs to be in focus when the hotkey is sent. That's OK but not ideal.

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

Re: Need Help with Script for Cleaning up JSON Files for pasting

Post by mikeyww » 06 Oct 2022, 11:54

OK.

Code: Select all

!+t::
Clipboard := RegExReplace(Clipboard, "m`a)(,|""|^ +)")
SoundBeep, 1500
Return

cws
Posts: 27
Joined: 02 Oct 2017, 18:11

Re: Need Help with Script for Cleaning up JSON Files for pasting

Post by cws » 10 Oct 2022, 18:03

Ok that regex command worked, thanks!

final script-

Code: Select all

!+g::

SplashTextOn ,,,Working,
ClipWait, 0
Sleep, -1
If !ErrorLevel {
 Clipboard := RegExReplace(Clipboard, "m`a)(,|""|^ +)")
} Else MsgBox, 48, Error, An error occurred while waiting for the clipboard.
SplashTextOff
Run, C:\Windows\Notepad.exe
WinWaitActive, Notepad
ControlClick, ,Notepad,,,, NA
Send ^v
Return

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

Re: Need Help with Script for Cleaning up JSON Files for pasting

Post by mikeyww » 10 Oct 2022, 19:54

Another option is below.

Code: Select all

!+g::
Run, notepad
WinWaitActive, ahk_exe notepad.exe,, 4
If ErrorLevel
 MsgBox, 48, Error, An error occurred while waiting for the window.
Else ControlSetText, Edit1, % RegExReplace(Clipboard, "m`a)(,|""|^ +)")
Return

Post Reply

Return to “Ask for Help (v1)”