Get "copied" text Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Translating-IT

Get "copied" text  Topic is solved

04 Apr 2014, 06:33

Hi,

Am new to this and set up this very first script to check/replace text when it is selected and CRTL+ALT+b is pressed:

^!b::
array := "z.B.+z. B.|z.b.+z. B.|z. B.+z. B.|z. b.+z. B.|zb+z. B.|zB+z. B." ; |+
oCB := ClipboardAll ; old Clipboard
SendInput, ^c
acbb := 0
Loop, parse, array, |
{
StringSplit, MyArray, A_LoopField, +
if (Clipboard = MyArray1)
;ClipBoard := %MyArray2%
msgbox %MyArray1%
acbb := 1
break
}
msgbox %Clipboard%
if acbb = 1
SendInput, %MyArray2%
else
SendInput, ^v
;ClipBoard := "A"
sleep = 250
ClipBoard := oCB ; restore ClipBoard
return

The problem is that apparently I don't get the selected text correctly. The message box only says Clipboard and the if clause does not work properly either.
How can I compare the selected text with the array?
Did I miss something?

Thanks in advance,
Pascal
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Get "copied" text

04 Apr 2014, 12:43

See the comments I've added:

Code: Select all

^!b::

;"array" is not an actual array. It is a variable named array which contains a string
array := "z.B.+z. B.|z.b.+z. B.|z. B.+z. B.|z. b.+z. B.|zb+z. B.|zB+z. B." ; |+

oCB := ClipboardAll ; old Clipboard
SendInput, ^c
acbb := 0
Loop, parse, array, |
{
	StringSplit, MyArray, A_LoopField, +
	MsgBox, % "Loop number: " A_Index "`n" MyArray1 "`n" MyArray2
	
	; "=" is case insensitive, "==" is case sensitive
	if (Clipboard == MyArray1)	; perhaps you meant to include curly braces around the body of the "if" statement?
	{
		;ClipBoard := %MyArray2%	;you probably don't need the % around MyArray2. (Unless you want the contents of MyArray2 to be interpreted as the name of the variable.)
		msgbox, 1 %MyArray1%
		acbb := 1
		break
	}
}
msgbox, 2 %Clipboard%
if (acbb = 1)
	SendInput, %MyArray2%
else
	SendInput, ^v
;ClipBoard := "A"
sleep 250	;no "=" required
ClipBoard := oCB ; restore ClipBoard
return
Alternatively here are two more approaches:

Code: Select all

MyArray := [["z.B.", "z. B."], ["z.b.", "z. B."], ["z. B.", "z. B."], ["z. b.", "z. B."], ["zb", "z. B."], ["zB", "z. B."]]

^!b::
SavedClip := ClipboardAll
Send, ^c
Copied := Clipboard
Clipboard := SavedClip
for k, v in MyArray
{
	;MsgBox, % k "`n" v[1] "`n" v[2]
	if (Copied == v[1])
	{
		SendInput, % v[2]
		return
	}
}
SendInput, % Copied
return

Code: Select all

MyArray2 := {"z.b.": 1, "z. b.": 1, "zb": 1}

^!b::
SavedClip := ClipboardAll
Send, ^c
Copied := Clipboard
Clipboard := SavedClip
if (MyArray2[Copied])
	SendInput, z. B.
else
	SendInput, % Copied
return
Last edited by kon on 04 Apr 2014, 15:05, edited 1 time in total.
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Get "copied" text

04 Apr 2014, 14:05

Here is my approach.

I used ComObjCreate("Scripting.Dictionary") to get around the limitation that normal object keys are not case sensitive.

Code: Select all

Text := ComObjCreate("Scripting.Dictionary")

Text.item("z.B.") := "z. B."
Text.item("z.b.") := "z. B."
Text.item("z. B.") := "z. B."
Text.item("z. b.") := "z. B."
Text.item("zb") := "z. B."
Text.item("zB") := "z. B."

^!b::
	oCB := ClipboardAll
	Send ^c
	if Text.item(Clipboard)
	{
		Clipboard := Text.item(Clipboard)
		Send ^v
	}
	Clipboard := oCB
return
You might need some ClipWaits or Sleeps in there but it worked without them for me.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
Guest

Re: Get "copied" text

05 Apr 2014, 03:07

@ kon

Your first one works more or less at least until it gets one where it has to trigger. If you try it on several items eg. z.a. z.b. z.a. it will not work after the 2nd one. It seems the oldclipboard is kept and the if checks against the then correct z. B. and does not change anything anymore.

Your first alternative will not work when I try it and the second one cannot be used as the z. B. issue was just one example of many for testing purposes. There will be many more errors to be checked like u.v.m. -> u. v. m. and many more but you couldn't know that without me telling you. ;)

@ FanaticGuru

I like your approach with the dictionary but unfortunately the script does not trigger the if clause at all.

Thanks,
Pascal
Translating-IT

Re: Get "copied" text

05 Apr 2014, 03:41

@ FanaticGuru

ok, got yours to work. I just needed to move the dictionary creation part so that it now looks like this:


^!b::
Text := ComObjCreate("Scripting.Dictionary")

Text.item("z.B.") := "z. B."
Text.item("z.b.") := "z. B."
Text.item("z. B.") := "z. B."
Text.item("z. b.") := "z. B."
Text.item("zb") := "z. B."
Text.item("zB") := "z. B."

oCB := ClipboardAll
Send ^c
if Text.item(Clipboard)
{
Clipboard := Text.item(Clipboard)
Send ^v
}
Clipboard := oCB
return

it also works in any case, no matter how many times the shortcut is used.
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Get "copied" text

07 Apr 2014, 12:38

Translating-IT wrote:@ FanaticGuru

ok, got yours to work. I just needed to move the dictionary creation part so that it now looks like this:


^!b::
Text := ComObjCreate("Scripting.Dictionary")

Text.item("z.B.") := "z. B."
Text.item("z.b.") := "z. B."
Text.item("z. B.") := "z. B."
Text.item("z. b.") := "z. B."
Text.item("zb") := "z. B."
Text.item("zB") := "z. B."

oCB := ClipboardAll
Send ^c
if Text.item(Clipboard)
{
Clipboard := Text.item(Clipboard)
Send ^v
}
Clipboard := oCB
return

it also works in any case, no matter how many times the shortcut is used.
My script as posted works fine for me. Having the initialization of "Text" within the hotkey is inefficient as this only needs to be done once when the script is first run (not ever time the hotkey is pressed) and should be included in the Auto-execute section of the script.

http://www.autohotkey.com/docs/Scripts.htm#auto

Which means there can be no Return, Exit, hotkey/hotstring label above the "Text" initialization.

Putting it within the hotkey will work especially with only a few items but if that list of replacements is going to grow significantly I would move it to the Auto-execute section and probably devise a easier way to get the information in the object than literal declarations of each item. Maybe a text file with a search and replace string on each line separated by a tab.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 283 guests