GUI Scripting of WeChat

I don’t know if anybody here has the Desktop version of WeChat installed, but I’m trying to do some GUI scripting on it.

When I start up the app, it would list a bunch of conversations, each indicated by the row.

I’m trying to get AppleScript to click on UI element 1 of row 2. (When the app starts up, UI element 1 of row 1 is highlighted by default).

The response from Event Log is → missing value and the click does not get registered in the app.

Of course, if I click manually in the app to the second conversation in the list, or UI element 1 of row 2, it works.

So I can’t get a click to register. Any ideas?


tell application "System Events"
	tell process "WeChat"
		tell window 1
			tell splitter group 1
				tell scroll area 1
					tell table 1
						tell row 5
							tell UI element 1
								click
							end tell
						end tell
					end tell
				end tell
			end tell
		end tell
	end tell
end tell

Try giving focus to the desired UI element before clicking it. I do not have this application to test this:


tell application "System Events" to tell process "WeChat"
	set frontmost to true
	tell window 1 to tell splitter group 1 to tell scroll area 1 to tell table 1
		tell row 5 to tell UI element 1
			set focused to true
			click
		end tell
	end tell
end tell

I also do not use WeChat, so I am unable to do any investigating. But most often with table elements, it will be the row element that you will want to target, as that is the functional container that houses the compositional elements within that row, which are usually inert/static, showing e.g. the person’s name, a preview of the message text, and so on.

Here’s what I would try:

tell application id "com.apple.systemevents"
		set message to my messageWithRowIndex:3 -- row 4
		select the message -- You don't need to, but you can
		if (click the message) = the message then return activate my application id "com.tencent.xinWeChat"

		-- If the row UI element isn't clickable, then this searches through its
		-- child elements to pick out all the ones that are potentially clickable
		-- and return a list of references back to you.
		tell the message to set children to [it] & the entire contents
		repeat with child in children
				if not (the child's action "AXPress" exists) ¬
					then set the child's contents to no
		end repeat
	
		return the children's specifiers
end tell

on messageWithRowIndex:i
		tell application id "com.apple.systemevents"
				tell (the first process whose bundle identifier is ¬
						"com.tencent.xinWeChat") to tell window 1 ¬
						to tell splitter group 1's scroll area 1's ¬
						table 1's row (i + 1) to if it exists then ¬
						return a reference to it
		end tell
end messageWithRowIndex:

Thank you,

I’ve managed to get WeChat to search for a specific conversation and then type something into the text entry box, and then hit the enter key to send.

The next step to complete this automation would be to schedule a daily time for this script to run, though I’m not familiar with how to do that. A quick Google glance points me in the direction of having to use launchd right? I would have to go through a tutorial on launchd to figure out how to do that.

Below is the script that I have so far, and it works for finding a specific conversation and, then sending a message into that conversation, but no daily scheduling yet.



set conversationName to “Search String for name of conversation or group”
set textToEnter to “Text to be entered into conversation”

tell application "System Events"
	tell process "WeChat"
		tell window 1
			tell splitter group 1
				tell scroll area 1
					tell table 1
						repeat with i from 1 to count of rows
							tell row i
								tell UI element 1
									if the value of static text 1 is conversationName then
										set targetRow to i
										exit repeat
									end if
								end tell
							end tell
						end repeat
					end tell
				end tell
			end tell
		end tell
	end tell
end tell


set message to my messageWithRowIndex:targetRow
select the message -- You don't need to, but you can
tell application "WeChat"
	activate
end tell
tell application "System Events" # Grabs the text from the text entry box
	tell process "WeChat"
		tell window 1
			tell splitter group 1
				tell splitter group 1
					tell scroll area 2
						set value of text area 1 to textToEnter
						keystroke return
					end tell
				end tell
			end tell
		end tell
	end tell
end tell

on messageWithRowIndex:i
	tell application id "com.apple.systemevents"
		tell (the first process whose bundle identifier is ¬
			"com.tencent.xinWeChat") to tell window 1 ¬
			to tell splitter group 1's scroll area 1's ¬
			table 1's row (i) to if it exists then ¬
			return a reference to it
	end tell
end messageWithRowIndex:


You should check out UIBrowser which can help you figure out exactly which windows/frames/elements you want.

It even provides some direct AppleScript code generation and even direct
Importing to Script Debugger

https://pfiddlesoft.com/uibrowser/compare.html

This is new and haven’t checked it out yet:

http://www.interface-inspector.com/

Fast way to get UI elements is to use entire contents
Now you have a document in TextEdit and could search for the UI element you want.

OP maybe do not know some UI elements is only visible after a click. It also means the index of
UI elements could change. It could also mean GUI Scripting that do not use localised string will not work for other users machine if the language is different. If the wrong UI element is active it could also brake the script.

my getUIElements:"WeChat"

on getUIElements:_appName
	set theContent to ""
	tell application "System Events" to tell application process _appName
		set frontmost to true
		set theContent to its entire contents
	end tell
	
	tell application "Script Editor"
		tell front document
			set theLog to its event log
			tell application "TextEdit" to make new document with properties {text:theLog}
		end tell
	end tell
end getUIElements: