rounak Posted September 5, 2016 Share Posted September 5, 2016 (edited) tell application "Safari" activate delay 1 my clickClassName("icon icon-clip", 0) delay 1 tell application "System Events" key code 125 delay 0.5 key code 125 delay 0.5 keystroke return end tell end tell to clickClassName(theClassName, elementnum) tell application "Safari" do JavaScript "document.getElementsByClassName('" & theClassName & "')[" & elementnum & "].click();" in document 1 end tell end clickClassName WhatsApp tab name changes to (<no_of_new_messages>)WhatsApp. However URL stays same. Anyway, to get hold of this tab without getting all tabs URL. I think someone could write a regex for me. Also, if anyone 1. has other ideas to get this done or 2. has additional info like selecting a particular contact without using the "Search" field on the web page, 3. check if the attachment icon, is already clicked before running this script, 4. (unrelated) has the skills to get to the "Type a message" box and perform a file drop by mouse dragging from Alfred's window (assuming that Alfred's window bounds and location are fixed) 5. has any other useful info please guide me. Thanks. Edited September 5, 2016 by rounak Link to comment
deanishe Posted September 5, 2016 Share Posted September 5, 2016 I don't think there's any way around looping through all of Safari's windows and tabs. Best you can do, AFAIK, is something like this: -- Return first WhatsApp tab on whatsAppTab() tell application "Safari" repeat with tabList in (every tab of every window whose name ends with "WhatsApp") if (count of tabList) > 0 then return first item of tabList end if end repeat end tell end whatsAppTab Link to comment
rounak Posted September 6, 2016 Author Share Posted September 6, 2016 (edited) Thanks. Assuming javascript does nothing if it does not find the element to click, I will skip the keystroke steps in my script when that happens. For that I will have to find what javascript returns when it finds nothing. I will get to work on that. Edited September 6, 2016 by rounak Link to comment
deanishe Posted September 6, 2016 Share Posted September 6, 2016 document.getElementsByClassName() returns an empty array if nothing is found, so your JavaScript will throw an error. Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now