hide a browser while still automating search action?

I am not very experienced writing applescripts and I want to know if it is possible to automatically open a webpage, enter a command, and save the result, without actually seeing the actions take place. To be more specific I want to open the google image searchpage and enter a random word from a given list and save one of the resulting pictures. Has someone written a very similar script? Thanks.

Alright, here’s a script to do this. I had to make assumptions. For one, I assumed that you wanted to download the image associated with the first hit. For another, I didn’t know how you wanted to enter the words to search for. Feel free to clarify.

Anyway, the best way to handle your request is to script the shell to do it, and in this case I’ll use perl. Here’s the perl script that will handle most of the chore:

if(`curl -A "Mozilla/4.0" "http://images.google.com/images?q=whatever&ie=UTF-8&oe=UTF-8&hl=en&btnG=Google+Search"` =~ /imgurl=([^&]+)/){chdir "/tmp/"; `curl -A "Mozilla/4.0" -O $1`}else{die}

The ‘-A “Mozilla/4.0”’ part fools Google into allowing us to download the files we need via the command line tool ‘curl’. Google’s user agreement doesn’t allow automated searches of its database, and its site considers requests from ‘curl’ to the sort of automation it doesn’t allow.

We’ll use ‘curl’ to download the two files we’ll need in the course of the script. The ‘curl’ command and its parameters are enclosed in backticks (`) because (as I read Arthur Knapp’s explanation one time, and liked) it’s perl’s version of ‘do shell script’. It runs a shell tool and returns the result in place.

The ‘=~’ part is an operator that AppleScript doesn’t have which says, “Does the regular expression on the right side match the text on the left side?” and returns true or false depending.

In the ‘curl’ command, ‘whatever’ is our search term. In the final script, we’ll be able to substitute in whatever search term we want.

This part: ‘/imgurl=([^&]+)/’ of the perl script is the regular expression we’re trying to match with. The enclosing slashes set off the regular expression. ‘imgurl=’ is literal text in the downloaded source of the search results, the first instance of which is right next to, and to the left of, the image URL we want to obtain. The parentheses around ‘([^&]+)’ allow us to save this part of the expression, the URL we’re looking for, so we can use it in a moment. This part: ‘[^&]+’ says keep on matching any characters until you get to a ‘&’ character.

We use the ‘chdir’ command to specify the folder we want to download the image to. If the ‘if’ block is true (i.e., our regular expression matches some text in the search results), then we execute a couple perl commands. We follow the ‘chdir’ command with a semi-colon because that’s how you separate multiple commands in perl.

Now we call ‘curl’ a second time to download the found image. The ‘$1’ is where our regular expression saved the matched text between the parentheses.

If Google doesn’t return any results for the search terms, perl throws an error with ‘die’. The error handling here is rudimentary because I don’t know perl, but it should be good enough.

Here’s the final AppleScript:

set AppleScript's text item delimiters to "+"
set findTerm to (words of text returned of (display dialog "Get image for what search term(s):" default answer "")) as string

set destFold to POSIX path of (choose folder)

set command to "if(`curl -A "Mozilla/4.0" "http://images.google.com/images?q=" & ¬
	findTerm & ¬
	"&ie=UTF-8&oe=UTF-8&hl=en&btnG=Google+Search"` =~ /imgurl=([^&]+)/){chdir "" & ¬
	destFold & ¬
	""; `curl -A "Mozilla/4.0" -O $1`}else{die}"

	do shell script "perl -e " & quoted form of command
on error number 255
	display dialog "No results returned for: " & findTerm
end try

See that the perl command is now put together dynamically with pieces returned from AS mixed in. See that the perl script has not been escaped for AS string rules.

Now notice that the perl script–all one line–is escaped for the shell using the ‘quoted form’ property of strings before it is executed.

Note that if no results are obtained, the error dialog will have pluses (+) embedded in multiple word search terms, because I didn’t want to bother cleaning it up, but I did want to report what happened.

That was very helpful. Thanks.