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.
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
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 (&).
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
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 "
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
set javascriptCode to "with (document.forms[2]) {
elements[1].value='applescript';
submit();
}"
tell application "Safari"
do JavaScript javascriptCode in document 1
end tell
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!
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?
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.
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 "
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