Skip to content

Instantly share code, notes, and snippets.

@troutcolor
Created June 2, 2018 17:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save troutcolor/f808391eec896ec2505f2535e860ff22 to your computer and use it in GitHub Desktop.
Save troutcolor/f808391eec896ec2505f2535e860ff22 to your computer and use it in GitHub Desktop.
downloads media from WordPress site using AppleScript and the REST API
--needs json helper for apple script Free from mac app store
--https://itunes.apple.com/gb/app/json-helper-for-applescript/id453114608?mt=12
set downloadfolder to POSIX path of (choose folder with prompt "Please select an output folder:")
set pagecountstart to 1
set pagecountmax to 1
set perpage to 10 --MAX =100
set blogurl to "FILL-IN-THE-URL"
if blogurl = "FILL-IN-THE-URL" then
display dialog "You need to edit the script to fill in a url" buttons "OK" default button "OK"
return false
end if
--We could also limit media to a particular type
--http://v2.wp-api.org/reference/media/
--Or filter in various ways
repeat with pagecount from pagecountstart to pagecountmax
tell application "JSON Helper"
set bURL to blogurl & "/wp-json/wp/v2/media/?per_page=" & perpage & "&page=" & pagecount
set thejson to fetch JSON from bURL
set theimgs to {}
set imageCount to count of thejson
repeat with n from 1 to imageCount
set end of theimgs to source_url of item n of thejson
end repeat
end tell
repeat with img in theimgs
set cmd to "curl -L " & img & " > " & downloadfolder & filenamefromurl(img)
do shell script cmd
end repeat
end repeat
on filenamefromurl(theurl)
set s to "url=\"" & theurl & "\"; echo \"${url##*/}\""
return do shell script s
end filenamefromurl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment