Read first line from clipboard and convert it into 2 letter code Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
sammmsational
Posts: 5
Joined: 17 Aug 2022, 06:27

Read first line from clipboard and convert it into 2 letter code

Post by sammmsational » 18 Aug 2022, 02:56

Hey folks,
I'm trying to make a script that pulls text from the clipboard, reads the first line and converts it into a set two letter code.

If it doesn't match one of the specified ones, I'd like to display an InputBox that lets the user enter the two letter code themselves.

My current code is this, but it's not working for some reason and I can't for the life of me figure out why.
I've also tried to solve this with switch and assocArrays, but that didn't work either. I'd be happy to accept solutions using those, since if-loops aren't the most beautiful code afterall.

Code: Select all


F8::

ArtText := Clipboard

cat_array := StrSplit(ArtText, "`n")

if InStr(cat_array[1], case1)
{
	ArtCat := C1
}

if InStr(cat_array[1], case2)
{
	ArtCat := C2
}

if InStr(cat_array[1], case3)
{
	ArtCat := C3
}

if InStr(cat_array[1], case4)
{
	ArtCat := C4
}

if (ArtCat = "")
{
	InputBox, ArtCat, Unknown category, Unknown category`, please enter the two letter code.,,200,160
}

MsgBox % ArtCat


BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Read first line from clipboard and convert it into 2 letter code

Post by BoBo » 18 Aug 2022, 03:07

Code: Select all

F8::

ArtText := Clipboard

cat_array := StrSplit(ArtText, "`n")

Loop % cat_array.Count()
      ArtCat := InStr(cat_array[1], "case" A_Index) ? "C" A_Index : ""
if (ArtCat = "") {
      InputBox, ArtCat, Unknown category, Unknown category`, please enter the two letter code.,,200,160
      ; two letter code validation
      }
MsgBox % ArtCat
Not tested.

sammmsational
Posts: 5
Joined: 17 Aug 2022, 06:27

Re: Read first line from clipboard and convert it into 2 letter code

Post by sammmsational » 18 Aug 2022, 03:15

Thanks for the answer, though that won't work for my problem.
I blocked out the original cases for privacy reasons, they aren't incremental in my project.

Code: Select all

if InStr(cat_array[1], Spain) {
	ArtCat := ES
}

if InStr(cat_array[1], Italy) {
	ArtCat := IT
}

if InStr(cat_array[1], Germany) {
	ArtCat := DE
}

if InStr(cat_array[1], France) {
	ArtCat := FR
}
I've replaced them with something resembling my application now.

Sorry, should have made that clear from the beginning.

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Read first line from clipboard and convert it into 2 letter code

Post by BoBo » 18 Aug 2022, 03:27

Simply replace the Loop with a for-Loop that is processing an associative array case := {"Spain":"ES",…}
…and yes, be aware that a function like InStr() expects an expression'ist "string" declaration
…same goes for ArtCat := "ES"


BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Read first line from clipboard and convert it into 2 letter code  Topic is solved

Post by BoBo » 18 Aug 2022, 06:49

@sammmsational - JFYI

Code: Select all

#SingleInstance Force
case := {"Spain":"ES","Germany":"DE","Italy":"IT","France":"FR"}

F8::
for name, code in case
   ArtCat .= InStr(StrSplit(clipboard, "`n").1, name) ? code : ""
if (ArtCat = "")
   InputBox, ArtCat, Unknown category, Please enter the two-letter country code.,,200,160
MsgBox % ArtCat
ArtCat:=""
return

F1::clipboard:="France"
F2::clipboard:="Spain"
F3::clipboard:="Italy"
F4::clipboard:="Germany"
Tested.

Post Reply

Return to “Ask for Help (v1)”