URL Access scripting

I just tried to resurrect an old script, and it’s broken because URL access scripting is dead.

I need a simple tool that lets me download jpg files using their URL.

Any suggestions?

You can use ASObjC:

use AppleScript version "2.5" -- macOS 10.11 or later
use framework "Foundation"
use scripting additions

set theURL to current application's |NSURL|'s URLWithString:"https://forum.latenightsw.com/uploads/default/optimized/1X/1e7f2ed7fb94bd744bd472a3c9dc6a0106e92ba8_2_180x180.png"
-- build new destination URL, for example:
set theName to theURL's |lastPathComponent|()
set destURL to current application's |NSURL|'s fileURLWithPath:theName relativeToURL:(path to desktop)
-- download
set theData to current application's NSData's dataWithContentsOfURL:theURL
-- write file
theData's writeToURL:destURL atomically:true
2 Likes

Hey Shane, that looks great!
Would that work for any file type?
Like a PDF or zip file?

Hi Shane.

I’ve been avoiding using dataWithContentsOfURL: for downloading files as the Xcode documentation seems to warn against it. I presume the recommended NSURLSession alternative leaves the machine free in the event of delays on the network, but I don’t think it’s usable from ASObjC. Do you have any comment on this? I’ve been using ‘curl’.

use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"
use scripting additions

set theURL to "https://forum.latenightsw.com/uploads/default/optimized/1X/1e7f2ed7fb94bd744bd472a3c9dc6a0106e92ba8_2_180x180.png"
-- build new destination path, for example:
set theName to (current application's NSString's stringWithString:theURL)'s |lastPathComponent|()
set destPath to POSIX path of (path to desktop) & theName
-- download and write to file
do shell script ("curl " & quoted form of theURL & " > " & quoted form of destPath)

Sure — type shouldn’t matter.

You’re opening a can of worms :roll_eyes:

Calling curl via do shell script is still blocking the script — that’s what do shell script does. In an ideal world we’d use NSURLSession and a screenful of code, but I haven’t had much success with it.

You can still use NSURLConnection, even though it’s deprecated. Something like this, although I haven’t included any error handling:

use AppleScript version "2.5" -- macOS 10.11 or later
use framework "Foundation"
use scripting additions

property destURL : missing value
property jobDone : false

set theURL to current application's |NSURL|'s URLWithString:"https://forum.latenightsw.com/uploads/default/optimized/1X/1e7f2ed7fb94bd744bd472a3c9dc6a0106e92ba8_2_180x180.png"
-- build new destination URL, for example:
set theName to theURL's |lastPathComponent|()
set destURL to current application's |NSURL|'s fileURLWithPath:theName relativeToURL:(path to desktop)
-- download
set theNSURLRequest to current application's NSURLRequest's alloc()'s initWithURL:theURL cachePolicy:(current application's NSURLRequestReloadIgnoringCacheData) timeoutInterval:1
set theNSURLConnection to current application's NSURLConnection's alloc()'s initWithRequest:theNSURLRequest delegate:me startImmediately:true
repeat while jobDone is false
	delay 0.1
end repeat

on connection:anNSURLConnection didReceiveData:theData
	theData's writeToURL:destURL atomically:true
end connection:didReceiveData:

on connection:anNSURLConnection didFailWithError:anNSError
	-- handle the error
end connection:didFailWithError:

on connectionDidFinishLoading:anNSURLConnection
	set my jobDone to true
end connectionDidFinishLoading:

My feeling is that if we give up every method that’s as disparaged as dataWithContentsOfURL:, we might as well just give up.

Sorry. :smile: But thanks for the insights. :slight_smile:

Oh! When did NSURLSession stop being compatible ?

BTW, if anyone is taking requests for libraries, one that would replace all the functions of URL Access Scripting would be a hit around here.

Thanks, Shane, this worked like a charm!

Here’s the story, my daughter sent me an email saying she was fed up with PhotoBucket and wanted to download all her photos and close the account. But she had a couple thousand, going back 10 years and more, many were the only versions she had.

None of PB’s automated ways of getting the photos was working and she’d have to go through and click download for each one.

So I looked at the download page with the javascript inspector, found the URL for the actual image, and the consistent path scheme. I was able to appleScript making a new path to the image from the path to the images’s display page then used Shane’s snippet to download all 2k+ photos. Now not only is the problem solved, but she has higher res versions that if she had manually downloaded.

Thanks Shane! And thanks from my daughter too!

The world has moved on since URL Access Scripting. For anything more complex than simple file downloads, you’re probably better off with curl.

Never mind. It appears to be working in MacOS 10.15.5.

I know you already have your solution but thought I’d share the simple curl we’ve used in a couple of scripts:

use AppleScript version “2.4” – Yosemite (10.10) or later
use scripting additions

on run
set fileName to “MV5BOGZhM2FhNTItODAzNi00YjA0LWEyN2UtNjJlYWQzYzU1MDg5L2ltYWdlL2ltYWdlXkEyXkFqcGdeQXVyMTQxNzMzNDI@.V1_SY1000_CR0,0,671,1000_AL.jpg”

set theURL to “https://m.media-amazon.com/images/M/” & fileName
set theDest to “~/Desktop/” & fileName

do shell script "curl -A/–user-agent " & theURL & " >> " & theDest
end run

1 Like