Thanks for being a part of WWDC25!

How did we do? We’d love to know your thoughts on this year’s conference. Take the survey here

Apple Script to Automate Web Page Plot Data

Can someone please help me: I do not have the brain space (85yo) to figure out an Apple Script or Java Script app to do this simple task. I have spent a few hours each day, over several days, and have made zero progress on such an apparently simple task. I wish to create an Automator App for the macOS Safari browser that will schedule (via a Calendar Event) the download of the 48hr data behind the hourly Fuel Mix Plot Data from the AEMO Web Site, every Monday, Wednesday, Friday and Sunday.

Here is the link to the AEMO web site: AEMO, Energy Systems, Electricity, National Electricity Market (NEM), Data (NEM),Data Dashboard https://www.aemo.com.au/energy-systems/electricity/national-electricity-market-nem/data-nem/data-dashboard-nem

The 48 hour hourly Fuel Mix data is found by selecting the "Fuel Mix" button (which by default will display the NEM Current Trend). The 48 hour trend is displayed by tapping on the small "Current" pulldown menu, and selecting "48 hrs". The 48hr Data is down loaded by selecting the small circular button just to the right of the pulldown menu.

a) AEMO Web Site: https://www.aemo.com.au/energy-systems/electricity/national-electricity-market-nem/data-nem/data-dashboard-nem

b) Main Menu, and underlying html,

c) Fuel Mix menu, Pulldown list, DownLoad button, and underlying html,

I am familiar with C++ and have built Xcode Apps, and used Excel Macros extensively in the past.

Thank you. Robert.

Answered by DTS Engineer in 842094022

To do this sort of thing I'd use Safari's do JavaScript AppleScript interface together with jQuery. Here's a rough outline of how to get setup with the right stuff and a basic example. I'll leave the rest to you.

A. the do JavaScript AppleScript command is disabled in modern versions of Safari to prevent folks from accidentally running AppleScripts that run JavaScripts in their browser windows by mistake so you'll need to turn that on. To do so, first go to the Advanced settings in Safari and turn Safari's developer mode. In Safari 18.5 you can do so by checking the "Show features for web developers" checkbox. This will add a new set of settings to the Safari settings called "Develop". Go in there and turn on the "Allow JavaScript from Apple Events" checkbox. This will enable Safari's do JavaScript AppleScript command.

B. Get a copy of the most recent version of jQuery from https://jquery.com and save it on disk somewhere. It should be a single file and the compressed version is all on one line. jQuery is a simple library for looking up and finding stuff on web pages (You could, of course, do that all yourself with JavaScript and DOM commands, but jQuery saves a lot of that trouble). Use that to find and click on your button.

C. In your AppleScript, tell Safari to load your webpage into a window:

on SafariActiveWindowAndTab()
	tell application "Safari" to return {id of item 1 of windows, index of current tab of item 1 of windows}
end SafariActiveWindowAndTab

on RunJavaScript(windowID, tabNo, theJavaScript)
	try
		tell application "Safari"
			do JavaScript theJavaScript in tab tabNo of (window id windowID)
		end tell
	on error message number errorNo
		error "error in RunJavaScript: " & message number errorNo
	end try
end RunJavaScript


set {windowID, tabNo} to SafariActiveWindowAndTab()

RunJavaScript(windowID, tabNo, "window.location = 'https://your.webpage.here';")

D. then, load the jQuery code into the webpage's JavaScript context:

to LoadFileAsString(theFile)
	set src to open for access (theFile as alias)
	set thedata to read src as string
	close access src
	return thedata
end LoadFileAsString

set theFile to wherever you saved your copy of jQuery on disk
set jQueryCode to LoadFileAsString(theFile)
RunJavaScript(windowID, tabNo, jQueryCode)

E. finally, use jQuery to find and click on your download button:

set button_clicking_javascript to "
	var the_result = ''
	try {
	
			/* this is the part that does the actual work */
		$( 'button.visualization-icon-button' ).click()
		/* this reads: find the 'button' element with class 'visualization-icon-button' and click on it. */
		
		the_result = 'ok'
	} catch (e) {
		the_result = 'error - ' + e
	}
	the_result
"

set status to RunJavaScript(windowID, tabNo, button_clicking_javascript)

F. And that's all there is to it. Those are the basic tools to get you started.

comments:

I have wrapped the JavaScript in a try block that returns a result to the calling AppleScript. It doesn't catch syntax errors (use the JavaScript Console - accessible in Safari's Develop menu for that) but it makes debugging runtime errors in the JavaScript much easier.

In the above I use jQuery to click on the download button. That's a very simple example. I imagine it'll take some iteration to get it right, but you should be able to do something similar to select the menu you're interested in accessing on the JavaScript side using jQuery commands.

AppleScript uses "double quotes" for strings but JavaScript can use either. To prevent conflicts and to avoid having to escape characters I always use single quotes in the JavaScripts when using this technique.

To do this sort of thing I'd use Safari's do JavaScript AppleScript interface together with jQuery. Here's a rough outline of how to get setup with the right stuff and a basic example. I'll leave the rest to you.

A. the do JavaScript AppleScript command is disabled in modern versions of Safari to prevent folks from accidentally running AppleScripts that run JavaScripts in their browser windows by mistake so you'll need to turn that on. To do so, first go to the Advanced settings in Safari and turn Safari's developer mode. In Safari 18.5 you can do so by checking the "Show features for web developers" checkbox. This will add a new set of settings to the Safari settings called "Develop". Go in there and turn on the "Allow JavaScript from Apple Events" checkbox. This will enable Safari's do JavaScript AppleScript command.

B. Get a copy of the most recent version of jQuery from https://jquery.com and save it on disk somewhere. It should be a single file and the compressed version is all on one line. jQuery is a simple library for looking up and finding stuff on web pages (You could, of course, do that all yourself with JavaScript and DOM commands, but jQuery saves a lot of that trouble). Use that to find and click on your button.

C. In your AppleScript, tell Safari to load your webpage into a window:

on SafariActiveWindowAndTab()
	tell application "Safari" to return {id of item 1 of windows, index of current tab of item 1 of windows}
end SafariActiveWindowAndTab

on RunJavaScript(windowID, tabNo, theJavaScript)
	try
		tell application "Safari"
			do JavaScript theJavaScript in tab tabNo of (window id windowID)
		end tell
	on error message number errorNo
		error "error in RunJavaScript: " & message number errorNo
	end try
end RunJavaScript


set {windowID, tabNo} to SafariActiveWindowAndTab()

RunJavaScript(windowID, tabNo, "window.location = 'https://your.webpage.here';")

D. then, load the jQuery code into the webpage's JavaScript context:

to LoadFileAsString(theFile)
	set src to open for access (theFile as alias)
	set thedata to read src as string
	close access src
	return thedata
end LoadFileAsString

set theFile to wherever you saved your copy of jQuery on disk
set jQueryCode to LoadFileAsString(theFile)
RunJavaScript(windowID, tabNo, jQueryCode)

E. finally, use jQuery to find and click on your download button:

set button_clicking_javascript to "
	var the_result = ''
	try {
	
			/* this is the part that does the actual work */
		$( 'button.visualization-icon-button' ).click()
		/* this reads: find the 'button' element with class 'visualization-icon-button' and click on it. */
		
		the_result = 'ok'
	} catch (e) {
		the_result = 'error - ' + e
	}
	the_result
"

set status to RunJavaScript(windowID, tabNo, button_clicking_javascript)

F. And that's all there is to it. Those are the basic tools to get you started.

comments:

I have wrapped the JavaScript in a try block that returns a result to the calling AppleScript. It doesn't catch syntax errors (use the JavaScript Console - accessible in Safari's Develop menu for that) but it makes debugging runtime errors in the JavaScript much easier.

In the above I use jQuery to click on the download button. That's a very simple example. I imagine it'll take some iteration to get it right, but you should be able to do something similar to select the menu you're interested in accessing on the JavaScript side using jQuery commands.

AppleScript uses "double quotes" for strings but JavaScript can use either. To prevent conflicts and to avoid having to escape characters I always use single quotes in the JavaScripts when using this technique.

Thank you DTS Engineer, Over the next few days, I will dig deeper into your guideline.

I discovered that I can copy and paste the JavaScript into Safari Console while the page is open to test the download button code.

Thank you, I will let our know how I get on... Robert.

Apple Script to Automate Web Page Plot Data
 
 
Q