Download pictures from a url and then rename the pictures

Hey guys,


I just discovered the power of Applescript, pretty amazing what you could do with it. I'm learning a lot, but still need your help.


I need an applescript that would download pictures from a url and rename them with a new name. (see picture attached)


I found something like this on Windows, for excel script, but I need something like this for MAC.


Thank you in advance for your time !


User uploaded file

null-OTHER, OS X Yosemite (10.10.5)

Posted on Jul 1, 2016 6:41 AM

Reply
13 replies

Jul 1, 2016 7:15 AM in response to adrian_30

The following AppleScript will download the file and rename it in one command. If the download was successful, a dialog will show the new filename and report success. The example downloads and renames your posted image.


set urlPath to "https://discussions.apple.com/content/attachment/729230040"

set newname to POSIX path of ((path to desktop) as text) & "foo.jpg"


set status to (do shell script "curl -s -o " & newname & space & urlPath's quoted form)


if status is "" then

display dialog "File: " & newname & return & " File Successfully Downloaded."

else

display alert "Download of specified URL image failed." giving up after 10

end if

return

Jul 1, 2016 2:23 PM in response to adrian_30

The only change you need to make is how you find the filenames/URLs... then just add a loop.


I don't have Excel here, but this Numbers.app example should give you an idea:


use AppleScriptversion "2.4" -- Yosemite (10.10) or later

use scripting additions


tell application "Numbers"

set filenames to value of every cell of range "A1:A8" of table 1 of sheet 1 of document 1

set URLs to value of every cell of range "B1:B8" of table 1 of sheet 1 of document 1

end tell


repeat with i from 1 to count URLs

if (item i of filenames is not missing value) and (item i of URLs is not missing value) then

set thisFname to quoted form of (POSIX path of ((path to desktop) as text) & item i of filenames)

set thisUrl to quoted form of item i of URLs


set status to (do shell script "echo curl -s -o " & thisFname & space & thisUrl)


end if

end repeat

You can edit the cell ranges (... range "A1:A8"...) near the beginning to reflect where the data is in your spreadsheet. There is only minimal error checking (e.g. it checks against an empty cell, but doesn't validate whether the URL or filename is valid).


I think Excel should be pretty similar, except you might not need the '... of table 1' part since Excel doesn't go to that level.

Jul 1, 2016 2:22 PM in response to adrian_30

Build a text file that looks like the following, where the URL is on a separate line, and the renamed file is on the second line, of each URL couplet. This structure is key to the proper functioning of the AppleScript. I called this file url_list.txt, and put it on my Desktop. Tested on OS X 10.11.5.


"https://discussions.apple.com/content/attachment/729230040"

-o foo.jpg

https://discussions.apple.com/content/attachment/729230040

-o foobar.jpg

https://discussions.apple.com/content/attachment/729230040

-o bar.jpg


AppleScript


-- Prompt for a text file containing a list of URLs and the output filenames for each URL

-- Process the input list and write files to designated output folder

-- xargs -P2 uses two process threads on the list

-- -n3 there are three items per URL couplet 1) URL, 2) -o and 3) filename

property loc : ((path to desktop) as text)

property outdir : POSIX path of loc & "curlyFiles" as text

property foldername : "curlyFiles"

set url_list to POSIX path of (choose file with prompt "Choose URL list file for cURL")


tell application "Finder"

if not (existsfolderoutdir) is true then makenewfolderatlocwith properties {name:foldername}

end tell


set status to (do shell script "(cd " & outdir & " && xargs -P2 -n3 <" & url_list's quoted form & " curl -sL)")


if not status is "" then

display alert "Processing of URL list: " & url_list & " has failed." giving up after 10

end if

return

Jul 6, 2016 4:04 AM in response to adrian_30

In the Script Editor's File menu, select Open Dictionary… and then scroll down until you can select System Events. Click on the Disk-Folder-File Suite in System Events, and under Application, you will see the various names for folder locations.


You already know about the Desktop folder (~/Desktop). Here are some others. Notice that a trailing ':' is used for sub-folders. Click the following examples to enlarge.

User uploaded file

Jul 14, 2016 5:36 AM in response to adrian_30

The shortest path to a double-clickable application is to take Camelot's code, and save as an AppleScript application on your Desktop from the Script Editor.


Yes, with some work, the AppleScript code can be marsupialized into an Objective-C application, and executed. You would likely incorporate NSTask to submit the cURL request.


Here is a Run AppleScript from a Cocoa Application post on stackoverflow. I have done other NSAppleScript work in a PyObjC application.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Download pictures from a url and then rename the pictures

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.