I would like to see if a file would be online with Applescript.
My goal is to generate a URL of an txt file online and cURL the data from it.
Is this possible?
I would type in “hello” and check if mysite.com/search/hello.txt exists and then if it does, download the data and display it, else it just would say it didn’t have any results.
If a site can’t find a specified file, it generates a 404 Error (Not Found) or a 301 Error (Redirected) most of the time. So you can check if the returned date contains one of those numbers to define if the file exists.
property sites : {"http://www.google.be/file.txt", "http://www.bbc.com/file.txt", "http://www.yahoo.com/file.txt", "http://developerief2.site11.com/file.txt"}
repeat with i in sites
checkExists(i)
end repeat
on checkExists(siteURL)
set siteSource to do shell script "curl " & quoted form of siteURL
if siteSource contains "404" or siteSource contains "301" then
display dialog (siteURL & ": " & return & return & "Not Found")
return missing value
else
return siteSource
end if
end checkExists
This of course doesn’t work if your file contains one of these numbers like the page you are reading right now: http://macscripter.net/viewtopic.php?pid=125977#p125977
Hope it helps,
ief2
Yeah, good idea.
"
404 Not Found
Not Found
The requested URL /example/example was not found on this server.
Additionally, a 404 Not Found
error was encountered while trying to use an ErrorDocument to handle the request.
Apache/1.3.33 Server at [url=http://www.mactweaks.info]www.mactweaks.info[/url] Port 80
" is what appears when there is no file found on my site.
THANK YOOOOUUUUU! 
OK, Your idea is wonderful!
Wait!
What is wrong here, it won’t return false! I don’t want to use “contains”
display dialog "What would you like to search?" default answer "President Obama" buttons {"OK"} default button 1
checkExists(text returned of result)
on checkExists(search)
set failure to "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n<HTML><HEAD>\n<TITLE>404 Not Found</TITLE>\n</HEAD><BODY>\n<H1>Not Found</H1>\nThe requested URL /search/" & search & ".txt was not found on this server.<P>\n<P>Additionally, a 404 Not Found\nerror was encountered while trying to use an ErrorDocument to handle the request.\n<HR>\n<ADDRESS>Apache/1.3.33 Server at [url=http://www.mactweaks.info]www.mactweaks.info[/url] Port 80</ADDRESS>\n</BODY></HTML>"
display dialog en(search)
set siteSource to do shell script "curl " & quoted form of "http://www.mactweaks.info/search/" & en(search) & ".txt"
if siteSource is failure then
display dialog ("Your search produced no search results with this name.")
return false
else
return siteSource
end if
end checkExists
on en(URLs)
try
return do shell script "/usr/bin/perl -e 'use URI::Escape; print uri_escape(\"" & URLs & "\")'"
on error err
display dialog "An error has occured." & return & return & err buttons {"OK"}
return false
end try
end en
I have no idea why it returns false, but comparing a list of each others paragraphs seams to work:
set l to every paragraph of (do shell script "curl [url=http://www.mactweaks.info/search/President%20Obama.txt)]http://www.mactweaks.info/search/President%20Obama.txt")[/url]
set a to every paragraph of "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">
<HTML><HEAD>
<TITLE>404 Not Found</TITLE>
</HEAD><BODY>
<H1>Not Found</H1>
The requested URL /search/President Obama.txt was not found on this server.<P>
<P>Additionally, a 404 Not Found
error was encountered while trying to use an ErrorDocument to handle the request.
<HR>
<ADDRESS>Apache/1.3.33 Server at [url=http://www.mactweaks.info]www.mactweaks.info[/url] Port 80</ADDRESS>
</BODY></HTML>"
display dialog "String: " & ((do shell script "curl [url=http://www.mactweaks.info/search/President%20Obama.txt)]http://www.mactweaks.info/search/President%20Obama.txt")[/url] is (a as string) as string)
display dialog "List: " & (l is a) as string
Very strange,
ief2
HUH? I have this:
display dialog "What would you like to search?" default answer "President Obama" buttons {"OK"} default button 1
checkExists(text returned of result)
on checkExists(search)
set failure to paragraphs of "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\r<HTML><HEAD>\r<TITLE>404 Not Found</TITLE>\r</HEAD><BODY>\r<H1>Not Found</H1>\rThe requested URL /search/" & search & ".txt was not found on this server.<P>\r<P>Additionally, a 404 Not Found\rerror was encountered while trying to use an ErrorDocument to handle the request.\r<HR>\r<ADDRESS>Apache/1.3.33 Server at [url=http://www.mactweaks.info]www.mactweaks.info[/url] Port 80</ADDRESS>\r</BODY></HTML>"
display dialog en(search)
set siteSource to do shell script "curl " & quoted form of "http://www.mactweaks.info/search/" & search & ".txt"
if paragraphs of siteSource is failure then
display dialog ("Your search produced no search results with this name.")
return false
else
return (siteSource as string)
end if
end checkExists
on en(URLs)
try
return do shell script "/usr/bin/perl -e 'use URI::Escape; print uri_escape(\"" & URLs & "\")'"
on error err
display dialog "An error has occured." & return & return & err buttons {"OK"}
return false
end try
end en
OK, now it works:
set answer to text returned of (display dialog "What would you like to search?\n(type \"Help\" for help!)" default answer "President Obama" buttons {"OK"} default button 1)
set search to checkExists(answer)
if search is not false then
set AppleScript's text item delimiters to "</iframe></noscript></object></layer></span></div></table></body></html><!-- adsok -->\r<script language='javascript' src='https://a12.alphagodaddy.com/hosting_ads/gd01.js'></script>"
set search to text item 1 of search
display dialog "The result of \"" & answer & "\" returns:\n\t\n\t" & search
end if
on checkExists(search)
set failure to "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\r<HTML><HEAD>\r<TITLE>404 Not Found</TITLE>\r</HEAD><BODY>\r<H1>Not Found</H1>\rThe requested URL /search/" & search & ".txt was not found on this server.<P>\r<P>Additionally, a 404 Not Found\rerror was encountered while trying to use an ErrorDocument to handle the request.\r<HR>\r<ADDRESS>Apache/1.3.33 Server at [url=http://www.mactweaks.info]www.mactweaks.info[/url] Port 80</ADDRESS>\r</BODY></HTML>"
set siteSource to do shell script "curl " & quoted form of "http://www.mactweaks.info/search/" & en(search) & ".txt"
if siteSource is failure then
display dialog ("Your search produced no search results with this name.")
return false
else
return (siteSource as string)
end if
end checkExists
on en(URLs)
try
return do shell script "/usr/bin/perl -e 'use URI::Escape; print uri_escape(\"" & URLs & "\")'"
on error err
display dialog "An error has occured." & return & return & err buttons {"OK"}
return false
end try
end en
First coercing the failure into a string and of course using your en() handler in the curl does the trick for me:
display dialog "What would you like to search?" default answer "President Obama" buttons {"OK"} default button 1
checkExists(text returned of result)
on checkExists(search)
set failure to paragraphs of (("<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">
<HTML><HEAD>
<TITLE>404 Not Found</TITLE>
</HEAD><BODY>
<H1>Not Found</H1>
The requested URL /search/" & search & ".txt was not found on this server.<P>
<P>Additionally, a 404 Not Found
error was encountered while trying to use an ErrorDocument to handle the request.
<HR>
<ADDRESS>Apache/1.3.33 Server at [url=http://www.mactweaks.info]www.mactweaks.info[/url] Port 80</ADDRESS>
</BODY></HTML>") as string)
display dialog en(search)
set siteSource to do shell script "curl " & quoted form of "http://www.mactweaks.info/search/" & en(search) & ".txt"
if (paragraphs of siteSource) is failure then
display dialog ("Your search produced no search results with this name.")
return false
else
return (siteSource as string)
end if
end checkExists
on en(URLs)
try
return do shell script "/usr/bin/perl -e 'use URI::Escape; print uri_escape(\"" & URLs & "\")'"
on error err
display dialog "An error has occured." & return & return & err buttons {"OK"}
return false
end try
end en
Hope it works,
ief2
Oh, I improved too much to change now. Here it is:
set AppleScript's text item delimiters to "</iframe></noscript></object></layer></span></div></table></body></html><!-- adsok -->\r<script language='javascript' src='https://a12.alphagodaddy.com/hosting_ads/gd01.js'></script>"
set choose to do shell script "curl [url=http://www.mactweaks.info/search/search.data]http://www.mactweaks.info/search/search.data"[/url]
set choose to text item 1 of choose
set default to do shell script "curl [url=http://www.mactweaks.info/search/current.data]http://www.mactweaks.info/search/current.data"[/url]
set default to text item 1 of default
set default to remove_return(default)
repeat
set answer to text returned of (display dialog "What would you like to search?\n(type \"Help\" for help! You must search with Capital letters. Ex. \"United States\")" & return & choose default answer default)
set search to check(answer)
if search is not false then
set search to text item 1 of search
display dialog "The result of \"" & answer & "\" returns:\n\t\n" & search
end if
end repeat
on check(search)
if search starts with "the " then
set T to AppleScript's text item delimiters
set AppleScript's text item delimiters to "The "
set search to text item 2 of search
set AppleScript's text item delimiters to T
end if
set failure to "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\r<HTML><HEAD>\r<TITLE>404 Not Found</TITLE>\r</HEAD><BODY>\r<H1>Not Found</H1>\rThe requested URL /search/" & search & ".txt was not found on this server.<P>\r<P>Additionally, a 404 Not Found\rerror was encountered while trying to use an ErrorDocument to handle the request.\r<HR>\r<ADDRESS>Apache/1.3.33 Server at [url=http://www.mactweaks.info]www.mactweaks.info[/url] Port 80</ADDRESS>\r</BODY></HTML>"
try
set siteSource to do shell script "curl " & quoted form of "http://www.mactweaks.info/search/" & en(search) & ".txt"
on error e
if e = "\rcurl: (6) Couldn't resolve host '[url=http://www.mactweaks.info]www.mactweaks.info[/url]'" then
delay 5
try
set siteSource to do shell script "curl " & quoted form of "http://www.mactweaks.info/search/" & en(search) & ".txt"
on error e
display dialog "An error had occured:\n\t\n\t" & e
end try
end if
end try
if siteSource is failure then
display dialog ("Your search produced no search results with this name. Try with capital letters. Ex. \"Obama\", not \"obama.\"")
return false
else
return (siteSource as string)
end if
end check
on en(URLs)
try
return do shell script "/usr/bin/perl -e 'use URI::Escape; print uri_escape(\"" & URLs & "\")'"
on error err
display dialog "An error has occured." & return & return & err buttons {"OK"}
return false
end try
end en
on remove_return(i)
set T to AppleScript's text item delimiters
set AppleScript's text item delimiters to return
set returner to text item 2 of i
set AppleScript's text item delimiters to T
return returner
end remove_return
I’m not planning to do anything anymore.
I’m not going to add to the database that I HAVE to build. NOOOO!
I’m ending it.