How to move/hover mouse (not click) over a button on Safari by using Applescript

applescriptjavascriptmousesafari

Can anybody tell me how to write an Applescript that can move or hover mouse over a Button on Safari? I've successfully made an Applescript that can click to the button but I don't know how to make the mouse just move there, not clicking. Because I need to download some images from a website that only appear when you hover your mouse over that button (that button can't be clicked).

The website I want to get information (HTML Element) when execute the script:
https://shopee.vn/Bút-line-đi-nét-chuyên-nghiệp-SAKURA-PIGMA-MICRON-12-size-(0.03-Brush)-BÁN-LẺ-i.22061868.886461468

My sucessfull click-to-the-button Applescript (This click to the second image on the left):

tell application "Safari"
    do JavaScript "document.getElementsByClassName('_3ZDC1p')[2].click();" in document 1
end tell

or:

to clickClassName(theClassName, elementnum)
    tell application "Safari"
        do JavaScript "document.getElementsByClassName('" & theClassName & "')[" & elementnum & "].click();" in document 1  
    end tell
end clickClassName
clickClassName("_3ZDC1p", 2)

So can you tell me how to hover mouse over the button (but not clicking) in a similar way of script like these above? Notice the Button "1.0 Graphic" in the website above is faded, that means it can't be clicked but once you hover your mouse to it, the image on the left appear, i want to get that image so bad. The url of that image only appear when I hover the mouse cursor over that button "1.0 Graphic". I already know how to make script downloading image from URL. I just need to have a script that can do hovering mouse to that button ("1.0 Graphic") so the image can be seen to grab the url. Thanks you!

Best Answer

I do not know of a way using basic vanilla AppleScript, by itself, to move the mouse cursor; however there are third-party utilities that can do it, one being cliclick; however, you also need to be able to tell it where to move it and that has its own issues.

On a different track, looking at the web page of the URL in your OP, I see that looking at the HTML to the target element, e.g. "document.getElementsByClassName('_3ZDC1p')[2].innerHTML;" there is a URL for the image.

Why not get the HTML, parse it to get the URL, and download it directly to your system?

The following example AppleScript code does just that:

--  # Get the HTML for the target element.

tell application "Safari" to ¬
    tell front document to ¬
        set innerHTML to do JavaScript ¬
            "document.getElementsByClassName('_3ZDC1p')[2].innerHTML;"

--  # Parse 'innerHTML' to get the URL.

set text item delimiters to {"(", ")"}
set theURL to second text item of innerHTML
set text item delimiters to {}

--  # Set the pathfilename for the saved image,
--  # e.g,: /Users/me/Pictures/1591893975.jpeg
--  # Name is based on seconds since Epoch.

set saveToFileName to POSIX path of ¬
    (path to pictures folder) & ¬
    (do shell script "date +%s")

--  # Define the commands necessary to accomplish
--  # the download base on the OP's URL example.

set shellCMD to "curl -ks " & theURL's quoted form & " -o " & saveToFileName's quoted form & ¬
    "; mv -n " & saveToFileName's quoted form & space & saveToFileName's quoted form & ¬
    ".$(file " & saveToFileName's quoted form & " | awk -F ': | ' '{print tolower($2)}')"

--  # Download the file.

do shell script shellCMD
  • Note: This example AppleScript code is based solely on the HTML of the target element and what's necessary to automate the process once the target web page is available and is the frontmost Safari window.

Note: The example AppleScript code is just that and does not contain any error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.