Download an image on a page in chrome

Hello;

Sorry for the noob question – but I have written a macro that fills out a form field and clicks submit. Pretty awesome.

After submit the page reloads with a PNG graph. The file name is random.

What’s the best way to download the PNG from the web page.

Sorry if this is a FAQ – I did look but I could not find a solution that made sense to me…

Thanks,
Mark

Hey Mark,

What web-browser?

Is the URL of the page a direct link to the .png file?

Or is the .png embedded in the page code?

Can you post an example URL?

-Chris

Hey Chris.

The web browser is Chrome.

The URL to the image is in the page source. The pages contains an image specified by an HTML image tag. The file name (URL to the image) is different every time the page loads.

I can’t post the example – it’s behind a corporate firewall, but it’s the simplest case you can imagine.

Thanks!
Mark

Hey Mark,

Make sure Chrome is open to the page in question.

Try running this AppleScript from the Script Editor.app.

Look in the Result pane (found in menu View > Show Result).

You should bet 1 or more PNG links returned.

If that works then we’ll continue from that point.

-Chris

---------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2015/05/14 21:11
# dMod: 2016/06/04 18:48
# Appl: Google Chrome
# Task: Get PNG Links from Front Page
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Google_Chrome, @Extract, @Links
---------------------------------------------------------------------------------

set regexStr to ".*\\.png"
set linkList to _chrome_links(regexStr, "*", "href")

if linkList ≠ {} then
  set AppleScript's text item delimiters to linefeed
  return (linkList as text)
end if

---------------------------------------------------------------------------------
--» HANDLERS
---------------------------------------------------------------------------------
on _chrome_links(regexStr, tagName, tagType)
  set javascriptStr to "function in_array (array, item) {
  for (var i=0; i < array.length; i++) {
    if (array[i] == item ) {
      return true;}}
  return false;}
  var a_tags = document.getElementsByTagName('" & tagName & "');
  var href_array = new Array();
  var reg = new RegExp(/" & regexStr & "/i);
  for (var i=0; i < a_tags.length; i++) {
    var href = a_tags[i]." & tagType & ";
    if (reg.test(href)) {
      if (!in_array(href_array, href)) {
        href_array.push(href);}}}

  document.title = href_array;

"
  tell application "Google Chrome"
    tell front window
      tell active tab
        set chromeLinks to execute javascript javascriptStr
      end tell
    end tell
  end tell
end _chrome_links
---------------------------------------------------------------------------------
2 Likes