Applescript has a long and somewhat storied history with Apple. It dates back to OS 'Classic', and has been intermittently updated and forgotten over the decades. Although elements of AppleScript can be seen in Swift, and Objective-C to a degree, AppleScript shares very little in common syntactically with either.
Particularly with the advent of Swift, AppleScript has fallen to the wayside, although it is still extremely useful in certain situations, such as automation, or an interface with Bash and the Apple GUI.
Let's say you want to run more advanced tasks, like clicking a button or link on a webpage.
Building from the previous gist on AppleScript, create an AppleScript file with the following function:
to safariVisitWebPage(theWebPage)
tell application "Safari"
activate
set URL of document 1 to theWebPage
end tell
end safariVisitWebPage
safariVisitWebPage("https://www.ncbi.nlm.nih.gov/pubmed/30530595")
To tell Safari to click on a link via AppleScript, you can direct AppleScript to use javascript:
do JavaScript "document.getElementById('someXMLTagID').click();" in document 1
The above line of code directs AppleScript to tell Javascript to find an element by its ID, and then execute the click() function, or perform a user click.
A few variants of this command are listed below. All are workable functions that can be called via the syntax described above, but note that functions cannot be called from within a tell statement. You will also need to first find the appropriate identifier for the element, such as its ID, Name, Tag, or ClassName. For all identifiers except ID, you will need to provide the index of where it appears in the xml code for the webpage.
to clickID(theId) -- creates function in AppleScript
tell application "Safari"
do JavaScript "document.getElementById('" & theId & "').click();" in document 1 -- Javascript code to click elementId
end tell
end clickID
to clickName(theName, elenum)
tell application "Safari"
do JavaScript "document.getElementsByName('" & theName & "')[" & elenum & "].click();" in document 1
end tell
end clickName
to clickClassName(theClassName, elenum)
tell application "Safari"
do JavaScript "document.getElementsByClassName('" & theClassName & "')[" & elenum & "].click();" in document 1
end tell
end clickClassName
to clickTagName(theTagName, elenum)
tell application "Safari"
do JavaScript "document.getElementsByTagName('" & theTagName & "')[" & elenum & "].click();" in document 1
end tell
end clickTagName
do JavaScript "document.getElementById('" & theId & "').click();" in document 1 -- Javascript code to click elementId
If we wanted to open Safari, then click on a link on a webpage, here is one way to do it:
to openSafariClickLink(urlName, linkName)
tell application "Safari"
activate
set URL of document 1 to urlName
end tell
delay 4
tell application "Safari"
activate
do JavaScript "document.getElementById('" & linkName & "').click();" in document 1
end tell
end openSafariClickLink
openSafariClickLink('https://www.ncbi.nlm.nih.gov/pubmed/30841849', 'EntrezSystem2.PEntrez.PubMed.Pubmed_ResultsPanel.Pubmed_DisplayBar.SendToSubmit')
This script will open Safari, navigate to the webpage indicated, and then click the citation button. You'll notice that a window will immediately appear, asking what you would like to call the download. This also triggered an Event from Safari, and the window will stay open until you respond by clicking "Save" or "Cancel".
However, if you wanted to do this automatically, you'd need to figure out a way to bypass this Event, or use a different program. The latter option will be described next, using Chrome.
global webPageVar
global ncbiClickName
on run argv
set webPageVar to item 1 of argv
set ncbiClickName to item 2 of argv
set elenum to 0
tell application "Google Chrome"
activate
open location webPageVar
delay 1
activate
end tell
delay 4
tell application "Google Chrome"
activate
execute front window's active tab javascript "document.getElementsByName('" & ncbiClickName & "')[" & elenum & "].click();"
end tell
delay 4
end run
This script is designed to be run via Terminal (Bash), using this command:
osascript aboveScript.scpt 'https://www.ncbi.nlm.nih.gov/pubmed/30841849' 'EntrezSystem2.PEntrez.PubMed.Pubmed_ResultsPanel.Pubmed_DisplayBar.SendToSubmit'
First, the argv line
on run argv
set webPageVar to item 1 of argv
set ncbiClickName to item 2 of argv
tells the AppleScript to look for arguments via commandline, then the next 2 lines assign global variables to these 2 items. If there are less than two arguments provided, then AppleScript will ungracefully crash. If there are more than 2 arguments provided, then AppleScript will use the first two and ignore the others. The elenum variable provides an example of how to initialize and assign a variable.
The next lines of code:
tell application "Google Chrome"
activate
open location webPageVar
delay 1
activate
end tell
direct AppleScript to open Chrome, activate it, and then navigate to the webpage provided; note the difference in syntax for Chrome.
Then, Chrome will grab the element provided via commandline arguments, which will automatically download the citation on that page.
tell application "Google Chrome"
activate
execute front window's active tab javascript "document.getElementsByName('" & ncbiClickName & "')[" & elenum & "].click();"
end tell
You can run this same commands described above via commandline in Terminal, because the osascript command will recognize newlines, but this approach is not recommended at all.
I don't know if things have changed in later OSX versions, but I can't manage to click on the button "START" in http://www.tabatatimer.com/. I have followed your tips. The ID of the button is "timerStartButton". It does not return any errors, it just does not click.