Search script

Some places, such as Ebay, do their search in the main location bar and it is easy to see how you would form the search remotely.

For instance, for a search of AARDVARK, Ebay creates the search:

http://search.ebay.com/search/search.dll?cgiurl=http%3A%2F%2Fcgi.ebay.com%2Fws%2F&krd=1&from=R8&MfcISAPICommand=GetResult&ht=1&SortProperty=MetaEndSort&query=AARDVARK

Knowing this stuff makes it easy to call these searches from an applescript.

However, how do you get applescript to do searches on sites that have a ‘search’ text field which is embedded as these do not throw up the ‘formula’ needed to get the search to happen. This forum for instance would be one such place.

For instance, how would you get an applescript to do a search for AARDVARK on these forums?

I would imagine that the script would have to find the search box text entry form, fill it with the appropriate search criteria, and then create a return. Or something.

I’m trying to create a script that searches a whole bunch of sites for what you’re looking for and opens each site up in a different safari tab. It works fine, I just can’t get it to search these other pages.

Any ideas?

I guess all I’m asking is how to fill in tables on a webpage from Applescript.

I want applescript to open up, say google, type AARDARK in the search field, and then press ‘go’. Is that possible?

Is there any documentation anywhere on this?

The download command from the URL Access Scripting dictionary contains a FORM DATA parameter that is the equivalent of you filling in a form and hitting the submit button.

download: Downloads a specified URL to a specified file
	download  string  -- URL to download from
		to  file specification  -- the downloaded file location
		[replacing  yes/no]  -- whether to replace an existing file (default: no)
		[unpacking  boolean]  -- whether to decode or decompress the downloaded item (default: true)
		[progress  boolean]  -- whether to display a modal progress dialog (default: false)
		[form data  string]  -- form data
		[directory listing  boolean]  -- get a directory listing? (default: false)
		[download directory  boolean]  -- download as an entire directory (default: false)
		[authentication  boolean]  -- show authentication dialog (default: false)
	[Result:   file specification]  -- the downloaded file

Where do I find details of all that stuff? I can’t find it anywhere.

Please give me a link to relevant articles if you could.

thanks!


Or, if you have time… :lol:

Here’s the google web site source. What script would I write to open this in Safari, stick the word AARDVARK in the search field and set it off?

(Yes I know there is a much easier way of querying google, but this is an excercise in filling in forms on web pages)

Google
Google

Web Images Groups Directory News


Advanced Search
Preferences
Language Tools


Advertise with Us - Business Solutions - Services & Tools - Jobs, Press, & Help

©2003 Google - Searching 3,083,324,652 web pages

The URL Access dictionary can be opened from the Script Editor application File Menu > Open Dictionary

Performing a POST Event

Here’s an example of using the Form Data parameter to perform a POST event. This example will query the United States Postal website to retrieve a listing for a supplied Zipcode. The resulting HTML is written to disk as a file and opened in the Help Browser.

set the action_URL to "http://www.usps.gov/cgi-bin/zip4/ctystzip2"
 
 repeat
 display dialog "City State / ZIP Code Associations" & return & return & ¬
 "If you enter a city and state (for example: Memphis TN ), " & ¬
 "this script will display any associated ZIP Codes." & return & return & ¬
 "If you enter a ZIP Code (for example: 94116 ), " & ¬
 "it will display any associated cities." default answer "Cupertino CA"
 set the user_entry to the text returned of the result
 if the user_entry is not "" then exit repeat
 end repeat
 
 set the form_data to "ctystzip=" & user_entry
 set the query_results_file to (path to desktop folder as text) & "Query Results"
 
 try
 with timeout of 300 seconds -- try connecting for 5 minutes
 tell application "URL Access Scripting"
 download the action_URL to file ¬
 the query_results_file form data form_data replacing yes with progress
 end tell
 end timeout
 tell application "Finder"
 set the creator type of the query_results_file to "hbwr"
 open the query_results_file
 end tell
 on error the error_message number error_number
 if the error_number is not -128 then
 tell application (path to frontmost application as text)
 beep
 display dialog the error_message buttons {"Cancel"} default button 1
 end tell
 end if
 end try

NOTE: if a form has multiple inputs, join them using the ampersand character (&).

“city=charlottesville&county=albemarle&state=virginia”

Check this:
http://bbs.applescript.net/viewtopic.php?p=14246#14246

Here’s how it works in IE (thanks jj)

tell application “Internet Explorer”
do script “with (document.forms[2]) { // third form on the page
elements[1].value=‘applescript’; // set value of search field
submit(); // submit the form = click button “Search News”
}”
end tell

Simply replacing do script with do Javascript doesn’t seem to work though in Safari

As always, the answer was simple, so here it is :

set temp to display dialog “What are you looking for?” default answer “gear”
set gear to text returned of temp

tell application “System Events”
activate
end tell

tell application “Safari”
activate
open location “http://gearslutz.com/board/search.php3?s=
delay 2
end tell

tell application “System Events”
tell process “Safari”

	try
		
		tell window "The Gearslutz Forums Search Page"
			
			tell text field 1 of scroll area 1 of group 1 of window "The Gearslutz Forums Search Page" of application process "Safari"
				keystroke "	"
				keystroke gear
				
				keystroke " 

"

			end tell
		end tell
	end try
	
	
end tell

end tell

ok i was trying to make my own google script moddeld after yurs when i ran into a problem

it says cant get text returned … i want the answer of the dialog to be entered ito the search formual URL

set temp to display dialog "What are you looking for?" default answer "Mac OSX"
set Search to text returned
tell application "System Events"
	activate
end tell

tell application "Safari"
	activate
	open location "http://www.google.com/search?hl=en&lr=&ie=ISO-8859-1&q=" & Search & "&btnG=Google+Search"
	delay 2
end tell



end

All you need to do is add “of result” after the line that reads “set Search to text returned”

set Search to text returned of result

Here is Safari´s version:

set javascriptCode to "with (document.forms[2]) {
	elements[1].value='applescript'; 
	submit(); 
}"

tell application "Safari"
	do JavaScript javascriptCode in document 1
end tell

OK I’m getting there, thanks for your help people :smiley:

On I go…

Is there an ‘search engine’ app style applescript/project builder app out there that I could open up to take a look at?

I’m having grand ideas now about instead of having my search simply open up all these locations in Safari tabs, actually show them in the app using a list sorted by relevance.

I’m not sure how easy this would be. Perhaps I should stay clear of it. Although from looking at the way project builder lets you make lists so easily perhaps it’s not that difficult…

All help appreciated! Thanks for you help so far, wouldn’t be anywhere otherwise!

Oh, another thing about javascripting safari

On some of the searches of forum based web pages I’m setting up, there is not only an input text field for your search, but also a dropdown menu to chose category, defaulting on the first one whichever that may be.

How would I set a drop down menu to the relevant selection before the search ran?

Do I have to know the actual names of the selections or are the drop downs accessed by number?

thanks

This is a javascript question. If you’re really interested in applescript+javascript, you can take a look to this:
http://www.macscripter.net/scriptbuilders/category.php?search=msie+library
Though it is intended to work with Explorer, you know that Safari’s syntax is a mirror. Be careful with some handlers, such as “bookmark”, since this version was thought to work with IE 5-Classic.

Cheers.

Ok, so this works now :stuck_out_tongue:

set temp to display dialog “What are you looking for?” default answer “gear”
set gear to text returned of temp

tell application “System Events”
activate
end tell

tell application “Safari”
activate
open location “http://gearslutz.com/board/search.php3?s=
delay 2
end tell

tell application “System Events”
tell process “Safari”

         try 

                   tell window "The Gearslutz Forums Search Page" 

                     tell text field 1 of scroll area 1 of group 1 of window "The Gearslutz Forums Search Page" of application process "Safari" 
                                     keystroke "       " 
                                     keystroke gear 

                                  keystroke "  

"

                           end tell 
                     end tell 
             end try 


  end tell 

end tell

So, I ‘tab’ into the search field and then type a space

NEXT QUESTION lol

erm

Ok, there is a drop down menu on that page to search different threads of the forum. It’s always set to the first forum there is.

How on earth am I going to set it to whatever one I want? Perhaps I need to know the exact name, perhaps I can access it via a number or something.

It’s the last hurdle before searching nirvana can take place.

Thanks for you previous help!

Or…(I’ve been thinking - oops - dangerous)

A way to do all of this without opeing the browser until…

well, hang on

Could I :

a: Download the web page into a variable (the search page)
b: doctor the variable (page) so that it incorporates the search data (with my script)
c: "post’ it back into the browser and THEN hit return/submit

Is that possible?

for instance

download the_search_page
change the_search_page to put the correct criteria inside
post the_search_page into safari
submit

kind of, you now what i mean?