Scripting Safari for Basic Control with AppleScript and Javascript

Need some advice on Javascript execution. So I have some macros I made in Keyboard Maestro that do a series of filling fields and clicking links and what-not for some stuff at work. I want to be able to re-create these macros as straight AppleScript so I can share them with some peeps at work but I am very inefficient with Javascript. I looked around all over but had a difficult time finding the answers I was looking for.

The main things I’m looking to do is to do something like:
set safari field blah to variable blah
Click safari link blah
Set safari checkbox to blah
etc

Any tips?

This is mostly a JavaScript question. For that, I’d suggest posting your question in either Stackoverflow.com, or the KM forum.

It is easy to inject JavaScript into either the Chrome or Safari browsers using AppleScript or JXA. If you need help with that let us know.

Well after some snooping I found a wonderful set of tutorials on cubemg.com that break down the incorporation of Javascript with AppleScript for basic Safari control perfectly and now I am scripting Safari like crazy. I wish I could give the author more props but I could only find his name as Samuel.

The aspects that originally confounded me were how to identify what Javascript elements to look for after inspecting an element on a website (i.e. class, ID, name, tag), then how to use that found element within AppleScript to perform actions or get input.

So if anyone else has similar questions I definitely recommend looking these over:





2 Likes

Kevin, thanks for sharing. These tutorials look very helpful.

BTW, for anyone who wants to do all of this using Chrome, usually the JavaScript is the same, you just need to slightly modify the AppleScript to tell application "Google Chrome", like this:

      set pScriptStr to "your JavaScript code"

      tell application "Google Chrome"
        set jsScriptResults to execute (active tab of window 1) javascript pScriptStr
      end tell

To make it easy to execute the JavaScript in either Safari or Chrome, I have written this handler:

--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
on doJavaScript(pBrowserName, pScriptStr)
  --–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
  (*  VER: 1.1    2017-11-25
      PURPOSE:  Run JavaScript in the Designated Browser (Safari OR Chrome)
      PARAMETERS:
        β€’ pBrowserName    |  text  | Name of brower in which to run JavaScript
            β€’ IF this parameter is empty "", then the FrontMost App will 
                be determined and used, IF it is a browser (else an error)
        β€’ pScriptStr      | text  | JavaScript text to be run
        
      RETURNS:  Results of the JavaScript
      
      AUTHOR:  JMichaelTX
    ## REQUIRES:  Either Google Chrome or Safari
  ----------------------------------------------------------------
  *)
  
  local jsScriptResults, frontAppName
  
  if (pBrowserName = "") then -- get FrontMost App Name
    tell application "System Events"
      set pBrowserName to item 1 of (get name of processes whose frontmost is true)
    end tell
  end if
  
  try
    
    set jsScriptResults to "TBD"
    
    if pBrowserName = "Safari" then
      tell application "Safari"
        set jsScriptResults to do JavaScript pScriptStr in front document
      end tell
      
    else if pBrowserName contains "Chrome" then
      tell application "Google Chrome"
        set jsScriptResults to execute (active tab of window 1) javascript pScriptStr
      end tell
      
    else
      error ("ERROR:  Invalid Broswer Name: " & pBrowserName Β¬
        & return & "Script MUST BE run with the FrontMost app as either \"Safari\" OR \"Chrome\"")
    end if
    
  on error e
    error "Error in handler doJavaScript()" & return & return & e
    
  end try
  
  return jsScriptResults
  
end doJavaScript
--~~~~~~~~~~~~~~~~ END of Handler doJavaScript ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~