adrian_30

Q: 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 !

 

Untitled-1.jpg

null, OS X Yosemite (10.10.5)

Posted on Jul 1, 2016 6:41 AM

Close

Q: Download pictures from a url and then rename the pictures

  • All replies
  • Helpful answers

  • by VikingOSX,

    VikingOSX VikingOSX Jul 1, 2016 7:15 AM in response to adrian_30
    Level 7 (20,819 points)
    Mac OS X
    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/servlet/JiveServlet/showImage/2-30384132-729230/Un titled-1.jpg"

    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

  • by adrian_30,

    adrian_30 adrian_30 Jul 1, 2016 7:19 AM in response to adrian_30
    Level 1 (8 points)
    Mac OS X
    Jul 1, 2016 7:19 AM in response to adrian_30

    Hey Viking you are amazing !!! 

     

    It workes like charm ! But I need to download and rename a lot of files, so it would be awesome if I can do it from a list, or more files at a time.

     

    Please guide me to the right path

     

    Greetings from Germany !

  • by Camelot,Helpful

    Camelot Camelot Jul 1, 2016 2:23 PM in response to adrian_30
    Level 8 (47,285 points)
    Mac OS X
    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 AppleScript version "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.

  • by VikingOSX,Solvedanswer

    VikingOSX VikingOSX Jul 1, 2016 2:22 PM in response to adrian_30
    Level 7 (20,819 points)
    Mac OS X
    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/servlet/JiveServlet/downloadImage/2-30384132-72923 0/1097-900/Untitled-1.jpg"

    -o foo.jpg

    https://discussions.apple.com/servlet/JiveServlet/downloadImage/2-30384132-72923 0/1097-900/Untitled-1.jpg

    -o foobar.jpg

    https://discussions.apple.com/servlet/JiveServlet/downloadImage/2-30384132-72923 0/1097-900/Untitled-1.jpg

    -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 (exists folder outdir) is true then make new folder at loc with 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

  • by adrian_30,

    adrian_30 adrian_30 Jul 1, 2016 2:25 PM in response to VikingOSX
    Level 1 (8 points)
    Mac OS X
    Jul 1, 2016 2:25 PM in response to VikingOSX

    Viking,

     

    I have no idea how to thank you ! You saved me so much time !

     

    Thank you a lot !

  • by adrian_30,

    adrian_30 adrian_30 Jul 1, 2016 3:00 PM in response to Camelot
    Level 1 (8 points)
    Mac OS X
    Jul 1, 2016 3:00 PM in response to Camelot

    Camelot, the result looks like this :""curl -s -o /Users/blablabla/Desktop/ ""

     

    both in numbers and excel, using applescript version 2.8.1 , capitan, 10.11.5

     

    please help me

  • by Camelot,

    Camelot Camelot Jul 1, 2016 10:07 PM in response to adrian_30
    Level 8 (47,285 points)
    Mac OS X
    Jul 1, 2016 10:07 PM in response to adrian_30

    Oh, I left an 'echo' in my do shell script as part of my testing (I didn't want to actually download anything). Just remove that and you should be fine.

  • by adrian_30,

    adrian_30 adrian_30 Jul 4, 2016 3:18 AM in response to Camelot
    Level 1 (8 points)
    Mac OS X
    Jul 4, 2016 3:18 AM in response to Camelot

    You are THE MAN ! It works so smooth and it does everything I use do to by hand for hours !!!!

     

    Just one more question, how to save the files to a specific folder instead of the destkop?  I tried to change the path, but no succes.

     

    thank you and happy 4'th of July !

  • by adrian_30,

    adrian_30 adrian_30 Jul 6, 2016 12:51 AM in response to adrian_30
    Level 1 (8 points)
    Mac OS X
    Jul 6, 2016 12:51 AM in response to adrian_30

    Hey Camelot,

     

    still didn't figure it out how to save the files to a specific folder, instead of saving them to the desktop.

     

    Thank you for your time !

  • by VikingOSX,

    VikingOSX VikingOSX Jul 6, 2016 4:04 AM in response to adrian_30
    Level 7 (20,819 points)
    Mac OS X
    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.

    Screen Shot 2016-07-06 at 7.01.08 AM.jpg

  • by adrian_30,

    adrian_30 adrian_30 Jul 14, 2016 4:47 AM in response to VikingOSX
    Level 1 (8 points)
    Mac OS X
    Jul 14, 2016 4:47 AM in response to VikingOSX

    Thank you so much Viking ! 

     

    I was just wondering if there's a way to put this Applescript code into a nice Objective C app for mac ? So it's nicer and cleaner (German way of thinking)

     

    Let me know when in Germany I owe you a good beer

  • by VikingOSX,

    VikingOSX VikingOSX Jul 14, 2016 5:36 AM in response to adrian_30
    Level 7 (20,819 points)
    Mac OS X
    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.