Clipboard parsing Topic is solved

Ask gaming related questions (AHK v1.1 and older)
Guest

Clipboard parsing

09 Dec 2017, 22:30

I'm new to the forums so first I'd just like to say hi and that it seems like a great community around here!

Anyway, I'm stumped on parsing text from a webpage to get data, specifically copying all text on the page and trying to perform math on a number found from the parsing. Here's some code for example:

Code: Select all

autoCraft(craftSkill) {
	if(craftSkill == "Weapon") {
		itemType = 14
	} else if(craftSkill == "Armor") {
		itemType = 20
	} else if(craftSkill == "Bow") {
		itemType = 12
	} else if(craftSkill == "Spell") {
		itemType = 17
	} else if(craftSkill == "Relic") {
		itemType = 11
	} else {
		return
	}
	sendText("/cr") ; Displays Tradeskills Ranks
	craftLevel := findText(craftSkill . "crafting:")
	item := craftLevel / 10
}

findText(string) {
	Send ^a^c
	page := clipboard
	Loop parse,page,`n%A_Tab%,%A_Space% 
	{
		line := A_LoopField
		if(InStr(line,string)) {
			prefix := StrLen(string) + 1
			StringTrimLeft line,line,prefix
			StringReplace line,line,`,,,All
			break
		}
	}
	; To deselect text
	WinGetPos,,,Xmax,Ymax,A
	Ycenter := Ymax/2
	Send, {ALTDOWN}
	ControlClick, x10 y%Ycenter%, A
	Send, {ALTUP}
	return line
}
And here is a clipping from text on the page:

Trade Skills Rank: Taskmate
Weaponcrafting: 88
Armorcrafting: 400
Bowcrafting: 11
Spellcrafting: 18
Reliccrafting: 4
Chantcrafting: 4
Metalworking: 400
Leatherworking: 400
Woodworking: 82
Glassworking: 19
Gemworking: 22
Runeworking: 6

It seems intermittent whether or not craftLevel will resolve to show 88 or some other odd text or nothing at all. And when it does resolve correctly I can never get the division to work properly either. What am I doing wrong?
User avatar
evilC
Posts: 4823
Joined: 27 Feb 2014, 12:30

Re: Clipboard parsing

11 Dec 2017, 05:41

I found zero intermittent behavior. findText("Weaponcrafting") always returns 88.
FYI, you probably need a check in findText, as if you search for something that is not present, it returns the last line.

Code: Select all

autoCraft(craftSkill) {
	if(craftSkill == "Weapon") {
		itemType = 14
	} else if(craftSkill == "Armor") {
		itemType = 20
	} else if(craftSkill == "Bow") {
		itemType = 12
	} else if(craftSkill == "Spell") {
		itemType = 17
	} else if(craftSkill == "Relic") {
		itemType = 11
	} else {
		return
	}
	sendText("/cr") ; Displays Tradeskills Ranks
	craftLevel := findText(craftSkill . "crafting:")
	if (craftLevel == "")
		return
	item := craftLevel / 10
}

findText(string) {
	Send ^a^c
	page := clipboard
	Loop parse,page,`n%A_Tab%,%A_Space% 
	{
		line := A_LoopField
		if(InStr(line,string)) {
			prefix := StrLen(string) + 1
			StringTrimLeft line,line,prefix
			StringReplace line,line,`,,,All
			break
		}
	}
	if (!prefix)
		return
	; To deselect text
	WinGetPos,,,Xmax,Ymax,A
	Ycenter := Ymax/2
	Send, {ALTDOWN}
	ControlClick, x10 y%Ycenter%, A
	Send, {ALTUP}
	return line
}
User avatar
evilC
Posts: 4823
Joined: 27 Feb 2014, 12:30

Re: Clipboard parsing  Topic is solved

11 Dec 2017, 05:45

Also, there is a more efficient way to do this:

Code: Select all

autoCraft(craftSkill) {
	if(craftSkill == "Weapon") {
		itemType = 14
	} else if(craftSkill == "Armor") {
		itemType = 20
	} else if(craftSkill == "Bow") {
		itemType = 12
	} else if(craftSkill == "Spell") {
		itemType = 17
	} else if(craftSkill == "Relic") {
		itemType = 11
	} else {
		return
	}

Code: Select all

autoCraft(craftSkill) {
	static lookups := {Weapon: 14, Armor: 20, Bow: 12, Spell: 17, Relic: 11}
	if (lookups.HasKey(craftSkill))
		itemType := lookups[craftSkill]
	else
		return
	[...]

Return to “Gaming Help (v1)”

Who is online

Users browsing this forum: No registered users and 50 guests