First of all I would like to say that Im using AppleScript Studio. I am trying to create an application that allows you to easily search a major search engine by entering the search into a text field and when you click the go button the default browser will search the website for that information. In the script below I use google as my search engine. Heres what I have now:
on changed theObject -- Text Field
set searchURL to the contents of text field "google" of window "main"
tell window "main"
set contents of text field "status" to ("Search For: " & searchURL)
on clicked theObject --Button
open location ("http://www.google.com/search?hl=en&q=" & searchURL)
when I run the application Internet Explorer launches and loads the page “http://www.google.com/search?hl=en&q=” without the text that searchURL was set to. Is there a special way that I need to set up the script so that I can use searchURL in my script? Please Help
open location (("http://www.google.com/search?hl=en&q=" & searchURL) as string)
I have run into this problem before.
The reason you may be having problems is because the variable “searchURL” is not defined by the time you get to the “on clicked” handler. Unless the variable is a global variable, it is only good inside the handler it was created in. By making the variables properties (placing them at the top of the script by themselves like in the example) before any handler is called, they are accessible at any time.
I don’t know why you wouldn’t be getting an error message saying that the variable searchURL is not defined is beyond me. I always get an error.
Initialize the variables first by making them properties.
Separate the searchURL variable into two different variables (baseURL, and keyword)
baseURL is always the same, but keyword changes everytime the text field is changed.
When the button is clicked, the two are combined into searchURL first (they must be combined before the “open location” command…I don’t know why, it just does!). Then, open location.
on changed theObject
tell window "main"
set keyword to the contents of text field "google"
set contents of text field "status" to "Search For:"&(keyword as text)
on clicked theObject
set searchURL to (baseURL as text)&(keyword as text)
open location (searchURL as text)
This should work!!!