This worked on a test .csv I made with just image URLās in the first column:
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
set theSpreadsheet to choose file with prompt "Please select your csv file with the image URL's" of type {"csv"} without multiple selections allowed
tell application "Finder"
set downloadsFolder to path to downloads folder
set saveFolder to ((downloadsFolder as text) & "Script Downloaded Images:")
if not (exists saveFolder) then make new folder at downloadsFolder with properties {name:"Script Downloaded Images"}
set saveFolder to POSIX path of saveFolder
end tell
set spreadsheetData to read theSpreadsheet
set dataLines to the paragraphs of spreadsheetData
set theURLs to {}
repeat with aLine in dataLines
set delimitHolder to AppleScript's text item delimiters
set AppleScript's text item delimiters to ","
copy text item 1 of aLine to the end of theURLs
-- may want to insert validation in case it doesn't start with "HTTP" or end with the right extension, etc.
set AppleScript's text item delimiters to delimitHolder
end repeat
set imageCount to the count of theURLs
set missingImages to {}
set downloadedImages to {}
repeat with imageURL in theURLs
set delimitHolder to AppleScript's text item delimiters
set AppleScript's text item delimiters to "/"
set imageName to the last text item of imageURL
set AppleScript's text item delimiters to delimitHolder
set downloadFilePath to saveFolder & imageName as text
set shellOutput to (do shell script "
image=\"" & (contents of imageURL as text) & "\"
response=$(curl --write-out %{http_code} --silent --output /dev/null $image)
if [ \"$response\" == \"200\" ]; then
curl $image -o \"" & downloadFilePath & "\"
echo \"Image downloaded\"
else
echo \"Image does not exist\"
fi
")
if shellOutput is "Image does not exist" then
copy imageURL to the end of missingImages
else
copy imageURL to the end of downloadedImages
end if
end repeat
set missingImageDialogText to ""
if (count of missingImages) > 0 then
set missingImageDialogText to "The following images failed to download:"
repeat with anImage in missingImages
set missingImageDialogText to missingImageDialogText & return & anImage
end repeat
end if
display dialog "The script completed." & return & (count of downloadedImages) & " out of " & imageCount & " images were successfully downloaded to: " & saveFolder & return & missingImageDialogText buttons {"Cancel", "OK"} default button "OK"
Whoops, I see I missed that the csv doesnāt contain the entire URL, that itās just the end of it.
Amended script here:
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
set baseURL to "http://i1.adis.ws/i/jpl/"
set theSpreadsheet to choose file with prompt "Please select your csv file with the image URL's" of type {"csv"} without multiple selections allowed
tell application "Finder"
set downloadsFolder to path to downloads folder
set saveFolder to ((downloadsFolder as text) & "Script Downloaded Images:")
if not (exists saveFolder) then make new folder at downloadsFolder with properties {name:"Script Downloaded Images"}
set saveFolder to POSIX path of saveFolder
end tell
set spreadsheetData to read theSpreadsheet
set dataLines to the paragraphs of spreadsheetData
set theURLs to {}
repeat with aLine in dataLines
set delimitHolder to AppleScript's text item delimiters
set AppleScript's text item delimiters to ","
set newURL to baseURL & text item 1 of aLine
copy newURL to the end of theURLs
set AppleScript's text item delimiters to delimitHolder
end repeat
set imageCount to the count of theURLs
set missingImages to {}
set downloadedImages to {}
repeat with imageURL in theURLs
set delimitHolder to AppleScript's text item delimiters
set AppleScript's text item delimiters to "/"
set imageName to the last text item of imageURL
set AppleScript's text item delimiters to delimitHolder
set downloadFilePath to saveFolder & imageName as text
set shellOutput to (do shell script "
image=\"" & (contents of imageURL as text) & "\"
response=$(curl --write-out %{http_code} --silent --output /dev/null $image)
if [ \"$response\" == \"200\" ]; then
curl $image -o \"" & downloadFilePath & "\"
echo \"Image downloaded\"
else
echo \"Image does not exist\"
fi
")
if shellOutput is "Image does not exist" then
copy imageURL to the end of missingImages
else
copy imageURL to the end of downloadedImages
end if
end repeat
set missingImageDialogText to ""
if (count of missingImages) > 0 then
set missingImageDialogText to "The following images failed to download:"
repeat with anImage in missingImages
set missingImageDialogText to missingImageDialogText & return & anImage
end repeat
end if
display dialog "The script completed." & return & (count of downloadedImages) & " out of " & imageCount & " images were successfully downloaded to: " & saveFolder & return & missingImageDialogText buttons {"Cancel", "OK"} default button "OK"
Just out of curiosity, were you using my script, or ccstoneās?
I think theyāre pretty similar except heās doing everything in the shell and Iām using as much Applescript as possible and just using the shell to call curl to do the actual download.
If you know some Applescript but not much shell, then mine might have some modification/maintainability advantage for you⦠of course, if you know shell better then Applescript, then the opposite would be true.
Thanks for having a look, I am using t.spoons script
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
set baseURL to "http://jpl.a.bigcontent.io/v1/static/"
set theSpreadsheet to choose file with prompt "Please select your csv file with the image URL's" of type {"csv"} without multiple selections allowed
tell application "Finder"
set downloadsFolder to path to downloads folder
set saveFolder to ((downloadsFolder as text) & "Script Downloaded Images:")
if not (exists saveFolder) then make new folder at downloadsFolder with properties {name:"Script Downloaded Images"}
set saveFolder to POSIX path of saveFolder
end tell
set spreadsheetData to read theSpreadsheet
set dataLines to the paragraphs of spreadsheetData
set theURLs to {}
repeat with aLine in dataLines
set delimitHolder to AppleScript's text item delimiters
set AppleScript's text item delimiters to ","
set newURL to baseURL & text item 1 of aLine
copy newURL to the end of theURLs
set AppleScript's text item delimiters to delimitHolder
end repeat
set imageCount to the count of theURLs
set missingImages to {}
set downloadedImages to {}
repeat with imageURL in theURLs
set delimitHolder to AppleScript's text item delimiters
set AppleScript's text item delimiters to "/"
set imageName to the last text item of imageURL
set AppleScript's text item delimiters to delimitHolder
set downloadFilePath to saveFolder & imageName as text
set shellOutput to (do shell script "
image=\"" & (contents of imageURL as text) & "\"
response=$(curl --write-out %{http_code} --silent --output /dev/null $image)
if [ \"$response\" == \"200\" ]; then
curl $image -o \"" & downloadFilePath & "\"
echo \"Image downloaded\"
else
echo \"Image does not exist\"
fi
")
if shellOutput is "Image does not exist" then
copy imageURL to the end of missingImages
else
copy imageURL to the end of downloadedImages
end if
end repeat
set missingImageDialogText to ""
if (count of missingImages) > 0 then
set missingImageDialogText to "The following images failed to download:"
repeat with anImage in missingImages
set missingImageDialogText to missingImageDialogText & return & anImage
end repeat
end if
display dialog "The script completed." & return & (count of downloadedImages) & " out of " & imageCount & " images were successfully downloaded to: " & saveFolder & return & missingImageDialogText buttons {"Cancel", "OK"} default button "OK"
I opened my script from your post, with the base URL modified as you did it, put the URLās you posted (with the base path stripped back off) into my test .csv:
And my result was that 4 out of five downloaded successfully.
Only the first one failed, and it also fails if I put the URL you provided into a browser.
Those weird characters in the first link make me suspect a possible problem with character encoding. That the values in your CSV, unlike all but your first posted value, are getting characters changed between what youāre seeing the CSV and what terminal is actually trying to retrieve. For example, terminal doesnāt use Unicode text.
But thatās just a guess, and I never have to mess with text encoding problems (yet), so Iām really not sure.
But for me, the script is working perfectly with the URLās you posted, aside from the first that doesnāt work in a browser either.
If you want me to look more, you could PM a link to download an actual sample of a CSV youāre using for these, and I can try to dig a little deeper.
Well, this just got hard for me to help you with. I opened the script from your last post, downloaded the .csv you PMād me, ran the script, and got:
The script completed.
146 out of 146 images were successfully downloaded to: ~/Downloads/Script Downloaded Images/
So being unable to reproduce the error, itās hard to troubleshoot.
Are you clearing out the folder between runs? Iām not entirely sure what the shell script does in case of name conflicts with already downloaded files. If clearing out the folder helps, I can easily modify my script to be like ccstoneās and create a new folder for each run.
But I sort of doubt thatās the problem, and Iām afraid Iām short of ideas on what the problem could be. It really does get hard when the error isnāt reproducible.
Do the files download on your computer when you manually put the URL into a browser?
Thanks for having a look into this for me, I cleared all the folders and re ran the script with no luck still getting 0 of 146 images were downloaded. I then copied individual URLs into my browser the first two auto downloaded the third showed the image in the browser.
I connect through a proxy do you think this could be an issue?
Yeah, I donāt know. I double-checked, and it is working fine for me with the base URL set to
It does seem particularly odd that the script doesnāt work for you with that base URL but does work for me with the same, but for other Base URLās it works for both of us.
Weāre both using similar ādo shell scriptā lines with CURL for the actualy downloading, so the results will probably be the same if you use ccstoneās script.
But since what weāre looking at seems to be a weird, inconsistent bug, Iād give his script a shot and see if you get a different result.
Yeah iām confused with this one, I tried CCstones script but again it returned errors, it only brought back the last cell and skipped the rest.
If the script was modified to just read the CSV file with full links in the doc do you think this may work? Basically ditch the base URL function from the script.
Probably clutching at straws, but who knows. Computers can be weird.
Here it is expecting the full URL in the file.
I tested it with a few files you provided using the āhttp://jpl.a.bigcontent.io/v1/static/ā URL beginning and the full URL in the .csv and it worked fine.
Tom.
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
set theSpreadsheet to choose file with prompt "Please select your csv file with the image URL's" of type {"csv"} without multiple selections allowed
tell application "Finder"
set downloadsFolder to path to downloads folder
set saveFolder to ((downloadsFolder as text) & "Script Downloaded Images:")
if not (exists saveFolder) then make new folder at downloadsFolder with properties {name:"Script Downloaded Images"}
set saveFolder to POSIX path of saveFolder
end tell
set spreadsheetData to read theSpreadsheet
set dataLines to the paragraphs of spreadsheetData
set theURLs to {}
set delimitHolder to AppleScript's text item delimiters
repeat with aLine in dataLines
set AppleScript's text item delimiters to ","
copy text item 1 of aLine to the end of theURLs
end repeat
set AppleScript's text item delimiters to delimitHolder
set imageCount to the count of theURLs
set missingImages to {}
set downloadedImages to {}
set delimitHolder to AppleScript's text item delimiters
repeat with imageURL in theURLs
set AppleScript's text item delimiters to "/"
set imageName to the last text item of imageURL
set downloadFilePath to saveFolder & imageName as text
set shellOutput to (do shell script "
image=\"" & (contents of imageURL as text) & "\"
response=$(curl --write-out %{http_code} --silent --output /dev/null $image)
if [ \"$response\" == \"200\" ]; then
curl $image -o \"" & downloadFilePath & "\"
echo \"Image downloaded\"
else
echo \"Image does not exist\"
fi
")
if shellOutput is "Image does not exist" then
copy imageURL to the end of missingImages
else
copy imageURL to the end of downloadedImages
end if
end repeat
set AppleScript's text item delimiters to delimitHolder
set missingImageDialogText to ""
if (count of missingImages) > 0 then
set missingImageDialogText to "The following images failed to download:"
repeat with anImage in missingImages
set missingImageDialogText to missingImageDialogText & return & anImage
end repeat
end if
display dialog "The script completed." & return & (count of downloadedImages) & " out of " & imageCount & " images were successfully downloaded to: " & saveFolder & return & missingImageDialogText buttons {"Cancel", "OK"} default button "OK"
Just to try to help troubleshoot⦠hereās a version that still uses the base file path, but it also logs a plain text file of all the URLās it was trying to load to the downloaded images folder.
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
set baseURL to "http://jpl.a.bigcontent.io/v1/static/"
set theSpreadsheet to choose file with prompt "Please select your csv file with the image URL's" of type {"csv"} without multiple selections allowed
tell application "Finder"
set downloadsFolder to path to downloads folder
set saveFolder to ((downloadsFolder as text) & "Script Downloaded Images:")
if not (exists saveFolder) then make new folder at downloadsFolder with properties {name:"Script Downloaded Images"}
set saveFolder to POSIX path of saveFolder
end tell
set spreadsheetData to read theSpreadsheet
set dataLines to the paragraphs of spreadsheetData
set theURLs to {}
set delimitHolder to AppleScript's text item delimiters
set dataForFile to ""
repeat with aLine in dataLines
set AppleScript's text item delimiters to ","
set newURL to baseURL & text item 1 of aLine
copy newURL to the end of theURLs
set dataForFile to dataForFile & newURL & return
end repeat
set dataSavePath to POSIX file (saveFolder & "URLs as Text.txt")
try
close access file dataSavePath
end try
set fileReference to open for access (dataSavePath) with write permission
set eof fileReference to 0
write dataForFile to fileReference
close access fileReference
set AppleScript's text item delimiters to delimitHolder
set imageCount to the count of theURLs
set missingImages to {}
set downloadedImages to {}
set delimitHolder to AppleScript's text item delimiters
repeat with imageURL in theURLs
set AppleScript's text item delimiters to "/"
set imageName to the last text item of imageURL
set downloadFilePath to saveFolder & imageName as text
set shellOutput to (do shell script "
image=\"" & (contents of imageURL as text) & "\"
response=$(curl --write-out %{http_code} --silent --output /dev/null $image)
if [ \"$response\" == \"200\" ]; then
curl $image -o \"" & downloadFilePath & "\"
echo \"Image downloaded\"
else
echo \"Image does not exist\"
fi
")
if shellOutput is "Image does not exist" then
copy imageURL to the end of missingImages
else
copy imageURL to the end of downloadedImages
end if
end repeat
set AppleScript's text item delimiters to delimitHolder
set missingImageDialogText to ""
if (count of missingImages) > 0 then
set missingImageDialogText to "The following images failed to download:"
repeat with anImage in missingImages
set missingImageDialogText to missingImageDialogText & return & anImage
end repeat
end if
display dialog "The script completed." & return & (count of downloadedImages) & " out of " & imageCount & " images were successfully downloaded to: " & saveFolder & return & missingImageDialogText buttons {"Cancel", "OK"} default button "OK"
Take a look and see if the URLās look right in the text file. Maybe post part of the text file to the forum or PM me another dropbox link to what it saved so we can take a look, maybe itāll give me something to go on.
Another possible step is for me to simplify the CURL line and return terminal errors to Applescript.
Actually, come to think of itā¦
Please open terminal and at the command prompt paste:
and hit enter and then post the response in Terminal here. Maybe there will be a CURL error that can help zoom us in.