Download images

I have a database with 362 records with a field «Photo» containing a URL “http://www.example.com/images/food/315MainImage.jpg”.

I would like to:

  • download all «Photo« fields to a folder “Assets/images”

  • change all field data to “Assets/images/315MainImage.jpg”

I thought maybe using curl -O. Any help would be wonderful!

You could use loadurl or urltask( to load the image into a variable. Then you would use filesave to save it as a file.

To change the Photo field

Photo = "Assets/images"+Photo["-/",-1]

The urltask( function can download directly to a file – you don’t need to load it into a variable and save. Here’s an example from the documentation:

urltask("http://www.somesite.com/somepage.html","file","~/Documents/Test/Page.txt")

Keep in mind that the urltask( performs the download in the background, so the file won’t be available immediately.

In older versions of Panorama, functions like loadurl( were actually implemented using curl. But in Panorama X that is no longer true – it uses Apple’s network code. You could use curl if you really wanted to, but I would stick to Panorama’s functions unless you have some really specialized need that only curl can fullfill (doesn’t sound like that is the case in your application).

Thanks Dave, works perfect!

Could you share the logic of ["-/",-1] and where in the help manual I could find more info?

When I run this Procedure:

local imageurl
imageurl = Photo
taskid = (urltask(imageurl,“file”,"~/Desktop"))

It seems to work with no errors (“ok” in the Procedure window) but the photo is not downloaded.

When I step through the Procedure it gets to taskid( and this error occurs:

Error(Syntax error) in urltask(source code

Photo["-/",-1]

The field “Photo” contains an URL. The text funnel takes out the last part of the URL (the file name) from the last slash to the last character. Text funnels are well documented in the Pan X Help.

This specifies the folder where you want the file to be saved, but it doesn’t specify the name that should be given to the file. If you want it to have the same name it had on the server, you could write it like this.

taskid = (urltask(imageurl,"file","~/Desktop"+imageurl["-/",-1]))

So if the url was http://mydomain.com/sunset.jpg, then the expression

"~/Desktop"+imageurl["-/",-1] will return

~/Desktop/sunset.jpg

A longer formula, that is probably easier to understand, would be

 taskid = (urltask(imageurl,"file","~/Desktop/"+urlfilename(imageurl)))

I’ve tried both formulas that Dave suggested and still get an error “Error(Syntax error) in urltask(source code”.

I even set up a new database with 1 field “Photo” which contains “http://cdip.ucsd.edu/recent/model_images/sb_channel.png

Still no luck??

I tried it with your example image, and got the same result. This code does work though

Local imageurl, theImage, taskid
imageurl = Photo
theImage = loadurl(imageurl)
filesave "~/Desktop/"+urlfilename(imageurl), theImage

This also works.

Local imageurl, theImage, taskid
imageurl = Photo
taskid = urltask(imageurl, "var", "theImage",
"code", {filesave "~/Desktop/Test.png", theImage})

but if I use either the text funnel, or urlfilename( formula to name the file in that last example, no file is saved. There isn’t any error message. There just isn’t any result.

I figured out the problem here. The urltask( function has a parameter for code to be executed after the download is complete. However, you didn’t need any code, so you didn’t specify any code. Unfortunately, this resulted in an error. You can fix this by adding an empty code parameter, like this (be sure to scroll to the right to see the empty CODE parameter):

local taskid
taskid = urltask("http://cdip.ucsd.edu/recent/model_images/sb_channel.png","file","~/Desktop/testimage.png","CODE","")

This works perfectly.

I have fixed the bug, so in the next version it will work correctly even if the code parameter is omitted.

I think this is because you were referencing the local variable. The code inside the “CODE” parameter cannot access local variables in this procedure – the code is running later, after the file is downloaded (it could be 30 seconds later if it is a big image), and the original procedure is no longer hanging around. You could, however, use the local variable to generate part of the code.

"code",{filesave }+quoted(urlfilename(imageurl),theImage

But in this case, don’t do that, use the “FILE” parameter. Local variables are ok in the “FILE” parameter, because those are evaluated immediately, before the image is downloaded. (To make sure, I just tested, and a local variable does work.)