How can I use AppleScript and HTML DOM to download images from a webpage?

16 Replies, 973 Views

Hey everyone,

So, I’ve been trying to figure out how to use AppleScript HTML DOM download images from a webpage, but I’m kinda stuck. Like, I know AppleScript can interact with Safari, and I’ve seen some examples of grabbing text, but images? Not so much.

I’m thinking maybe I can use JavaScript via AppleScript to access the DOM and pull image URLs, then download them. But idk, I’m not super confident with the syntax.

Has anyone tried this before? Like, using AppleScript HTML DOM download images? Any tips or code snippets would be *super* helpful.

Also, if there’s a better way to do this, lmk! I’m open to suggestions.

Thanks in advance, y’all!

(btw, sorry if this is a noob question, still learning the ropes here 😅)
Hey! I’ve actually done something similar before. You’re on the right track with using JavaScript via AppleScript to grab image URLs from the DOM. Here’s a quick snippet to get you started:

```applescript
tell application "Safari"
do JavaScript "document.querySelectorAll('img').forEach(img => console.log(img.src));" in document 1
end tell
```

This logs all image URLs to the console. You can then use AppleScript to save them. If you need help with the download part, lmk!

Also, check out this guide on combining AppleScript and JavaScript for DOM manipulation: [link]. Super helpful for stuff like this.
Yo, I feel you! AppleScript HTML DOM download images can be tricky at first. Honestly, I’d recommend using a tool like `Puppeteer` or `BeautifulSoup` if you’re open to alternatives. They’re way more flexible for scraping images.

But if you’re set on AppleScript, try this:

```applescript
tell application "Safari"
set imgURLs to do JavaScript "Array.from(document.images).map(img => img.src);" in document 1
end tell
```

This stores all image URLs in a list. You can loop through and download them using `curl` or something.
Hey! I’ve been down this rabbit hole too. AppleScript HTML DOM download images is doable, but it’s a bit clunky. Here’s what worked for me:

1. Use JavaScript to extract image URLs.
2. Pass those URLs to AppleScript.
3. Use `curl` or `wget` in a shell script to download them.

If you’re stuck on the JS part, this site has a great tutorial: [link]. It’s not AppleScript-specific, but the concepts transfer.

Also, if you’re dealing with dynamic content, make sure the page is fully loaded before running your script.
Hmm, I’ve never tried AppleScript HTML DOM download images, but I’ve used Python for similar tasks. If you’re open to switching, `requests` and `BeautifulSoup` make this super easy.

That said, if you’re committed to AppleScript, try this:

```applescript
tell application "Safari"
set imgList to do JavaScript "Array.from(document.querySelectorAll('img')).map(img => img.src);" in document 1
end tell
```

Then, loop through `imgList` and use `do shell script` to download each image.
Hey! I’ve been working on something similar. AppleScript HTML DOM download images is definitely possible, but it’s not the most efficient way. If you’re okay with a bit of a learning curve, check out `Automator` or `Shortcuts`—they might be easier for this kind of task.

If you’re sticking with AppleScript, here’s a tip: make sure your JavaScript is running after the page is fully loaded. You can use `window.onload` or a delay in AppleScript to handle that.

Also, this site has a great breakdown of DOM manipulation with AppleScript: [link].
Hey there! I’ve been tinkering with AppleScript HTML DOM download images for a while now. It’s a bit of a niche use case, but it’s doable. Here’s a quick script to grab image URLs:

```applescript
tell application "Safari"
set imgURLs to do JavaScript "Array.from(document.images).map(img => img.src);" in document 1
end tell
```

Once you have the URLs, you can use `curl` or `wget` to download them. If you’re dealing with a lot of images, consider batching the downloads to avoid timeouts.

Also, this forum thread has some great tips: [link].
Wow, thanks so much for all the replies, y’all! I didn’t expect so many helpful tips. I tried the JavaScript snippet a few of you shared, and it worked like a charm to grab the image URLs.

I’m still figuring out the download part, but I think I’ll use `curl` like some of you suggested. Also, I checked out the links you shared, and they’re super helpful.

Quick question though—if I’m dealing with a page that loads images dynamically (like lazy loading), do I need to scroll the page first before running the script? Or is there a way to handle that in AppleScript?

Thanks again, everyone! You’ve made this so much easier for me. 😊
Hey! I’ve been in your shoes before. AppleScript HTML DOM download images is a bit of a headache, but it’s possible. Here’s a quick script to get you started:

```applescript
tell application "Safari"
set imgURLs to do JavaScript "Array.from(document.querySelectorAll('img')).map(img => img.src);" in document 1
end tell
```

Then, use `do shell script` to download the images. If you’re stuck, this guide might help: [link].

Also, if you’re open to alternatives, `Puppeteer` is way easier for this kind of thing.



Users browsing this thread: 1 Guest(s)