How to Use AppleScript and HTML DOM to Download Images?

AppleScript is a powerful scripting language built into macOS that enables users to automate a wide variety of tasks across applications. When combined with HTML DOM, it becomes a useful tool for automating interactions with web pages. For example, AppleScript can be used to open a web browser (like Safari), and HTML DOM can help extract image URLs from a webpage, allowing you to automate the downloading process.

The ability to download images in bulk is incredibly useful, whether you’re collecting images for research, archiving, or simply gathering assets for a project. In this guide, we will walk through the steps to automate image downloads from a webpage using AppleScript and HTML DOM. Additionally, we’ll discuss how AppleScript can be used to find duplicate images and delete smaller ones, saving you both time and space.

1. How to Use AppleScript and HTML DOM to Download Images?

Downloading images from a webpage using AppleScript involves several steps:

Step 1: Open the Webpage in Safari

To begin, you first need to open Safari and navigate to the target webpage. This is done using AppleScript, which can interact with Safari and control its actions.

Open the Webpage in Safari

In this snippet:

  • activate: Brings Safari to the foreground if it’s not already open.
  • open location: Opens the specified URL in Safari.
  • delay 5: Waits for 5 seconds to ensure the page has loaded completely before proceeding to the next step.

Step 2: Extract Image URLs Using HTML DOM

Once the webpage is loaded, we can use JavaScript to extract the image URLs from the HTML DOM. The document.images object provides access to all <img> elements on the page, and we can extract their src attributes, which hold the URLs of the images.

extract image urls using html dom

Here:

  • document.images: Collects all <img> elements from the page.
  • .map(img => img.src): Maps each image to its src attribute (the URL).
  • do JavaScript: Executes the JavaScript code within the current tab of Safari.

The result will be a string containing all the image URLs, separated by commas.

Step 3: Process the Image URLs

The image URLs returned by JavaScript will be in a single string, which we can split into individual URLs using AppleScript’s paragraphs command.

set imageUrlsList to paragraphs of imageUrls

This splits the string into a list of individual URLs, making it easier to process each image URL separately.

Step 4: Download the Images

Now that we have a list of image URLs, we can use the curl command to download each image. curl is a command-line tool for transferring data to or from a server, and it’s ideal for downloading files like images.

AppleScript download images

In this section:

  • mkdir -p: Creates a folder on the desktop named “Downloaded Images” if it doesn’t already exist.
  • curl -O: Downloads each image and saves it to the specified folder.

Step 5: (Optional) Find Duplicate Images and Delete Small Images

If you want to clean up your downloaded images by removing duplicates or deleting smaller images, AppleScript can help. To find duplicate images, you can compare the file sizes or hashes of the images after downloading. For small images, you can check the dimensions of the images and delete any that fall below a certain threshold.

Here’s how to delete images below a specific size:

applescript delete specific size images

This script:

  • Scans all files in the “Downloaded Images” folder.
  • Checks if the file size is smaller than the specified threshold (e.g., 100KB).
  • Deletes any images that fall below this size.

For duplicate images, you would need a more complex script that compares hashes (e.g., MD5 or SHA) of the images to identify duplicates.

2. Pros and Cons of Using AppleScript and HTML DOM to Download Images

Pros Cons
  • Automation: Once set up, the script automates the process of downloading images, saving you time and effort.
  • Customizability: You can easily modify the script to download images from specific types of websites, filter out certain images, or delete duplicates.
  • No Need for Third-Party Tools: AppleScript and JavaScript are built into macOS, so you don’t need to install additional software or browser extensions.
  • Limited Error Handling: The script may fail if there are errors in the image URLs or if the page layout changes unexpectedly.
  • Safari-Specific: This method works with Safari, so you’ll need to adapt the script for other browsers (e.g., Google Chrome) if required.
  • Learning Curve: Writing and debugging AppleScript and JavaScript can be challenging for those unfamiliar with programming.

3. Best Bulk Image Downloader for Mac: Imaget

While AppleScript and HTML DOM are powerful tools, they might not be the most efficient option for users who need to download large volumes of images or need a more user-friendly approach. Imaget is an excellent solution for macOS and Windows users who need to download images in bulk.

To download images on a Mac using Imaget, the process is straightforward and user-friendly.

Step 1: Download the macOS version by clicking the download button below and install it, then open Imaget from your Applications folder.

Step 2: Navigate to the webpage where you want to download images from (webpage, Instagram, Pinterest, etc.), then click “Auto Scroll” button to let Imaget auto extract available images from the page.

mac auto scroll to load images

Step 3: Choose preferred image resolution and format, then set other download options such as output type and destination folder for your images.

mac auto scroll to load images

Step 4: Click the Save all button to begin fetching images, then Imaget will download selected images on your Mac within a few seconds.

mac save all images wih imaget

4. Conclusion

Using AppleScript and HTML DOM to download images from webpages is a powerful, customizable solution for automating the process. It provides control over image downloads and offers the ability to delete duplicate or small images, making it a useful tool for more technical users. However, it requires some scripting knowledge and may not be the most efficient for bulk image downloads.

For macOS users looking for an easier, more streamlined solution, Imaget is a highly recommended alternative. It provides a user-friendly interface and powerful features for downloading images in bulk without the need for scripting. Whether you are working on a project or simply gathering images for personal use, Imaget is an excellent tool for macOS users.