Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Script to validate list or URL's based on the HTTP status code
-- Replace 'username' in the path with your own (lines 2 & 3)
set inputFile to "/Users/username/Documents/ListURL.txt"
set outputFile to "/Users/username/Documents/StatusURL.txt"
set txtList to paragraphs of (read inputFile)
set {TID, my text item delimiters} to {my text item delimiters, ";"}
repeat with txtLines in txtList
set {theURL} to text items of txtLines
set theScript to "curl -o /dev/null -s -I -w '%{http_code}' " & theURL
set theStatus to do shell script theScript
open for access outputFile with write permission
-- 2xx Success: status codes which indicate that the server successfully processed the request.
if theStatus = "200" then write "Status code: 200 Success for " & theURL & return to outputFile starting at eof
-- 3xx Redirection: status codes are used for redirection.
if theStatus starts with "3" then write "Status code: 30x Redirection for " & theURL & return to outputFile starting at eof
-- 4xx Request error: an error in the request prevented the server from being able to process it.
if theStatus = "400" then write "Status code: 400 Request error: Bad Request for " & theURL & return to outputFile starting at eof
if theStatus = "401" then write "Status code: 401 Request error: Unauthorized for " & theURL & return to outputFile starting at eof
if theStatus = "403" then write "Status code: 403 Request error: Forbidden for " & theURL & return to outputFile starting at eof
if theStatus = "404" then write "Status code: 404 Request error: Not Found for " & theURL & return to outputFile starting at eof
-- 5xx Server error: status codes which indicate that the server had an internal error when trying to process the request
if theStatus starts with "5" then write "Status code: 50x Server error for " & theURL & return to outputFile starting at eof
-- Note: you can add addional conditions for other status codes (https://httpstatus.io/http-status-codes)
close access outputFile
end repeat
set my text item delimiters to TID
@nrollr

This comment has been minimized.

Copy link
Owner Author

@nrollr nrollr commented Nov 29, 2015

Create a ListURL.txt file in your /Documents/ directory
Add all the URL's you which to validate: one per line and close with ";" as line separator. Example:
http://www.apple.com;
http://www.github.com;

@armamichi

This comment has been minimized.

Copy link

@armamichi armamichi commented Oct 25, 2017

Ciao!
I'm Gianmarco from Turin (Italy) ... i'm newbie in applescript, i hope you can help me. If i copy your script and running it with Indesign CS6 or directly by ScriptDebugger (on Mac Yosemite 10.10.5) i obtain an Error "No such file or directory..." but, if i run it on Applescript, it work!
Can you explain me why?
Thank you in advance.
Have a good day!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment