Jump to content

WhatsApp Web - Document attachment script


Recommended Posts

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 by rounak
Link to comment

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

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 by rounak
Link to comment
  • 4 years later...
  • 2 months later...

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...