PDA

View Full Version : AppleScript - Web Searcher




stustu242
Jun 7, 2011, 07:56 AM
I just started using applescript today and i have produced this so far... a youtube and google searcher...

display dialog "Where will you search from?" buttons {"Google", "Youtube", "Exit"}
set search to text returned of (display dialog "Search Google!" default answer "" buttons {"Search", "Exit"} default button 1)
open location "http://www.google.com.au/search?q=" & search

set search to text returned of (display dialog "Search Youtube!" default answer "" buttons {"Search", "Exit"} default button 1)
open location "http://www.youtube.com/results?search_query=" & search

after trying to get the exit buttons to actually exit the program it kept coming up with error messages so if you know how to fix the exit button and maybe add some cool additions to to make it better that would be awesome!

Thanks! :D



numero
Jun 14, 2011, 10:36 PM
Your initial code didn't handle the case of the user wanting to Exit. You were unconditionally going to Google and then Youtube.

The "Exit" choice from the second and third dialogs should probably be removed or you will have to figure out a way to test for a second "Exit" before going to Google or Youtube.


set choice to button returned of (display dialog "Where will you search from?" buttons {"Google", "Youtube", "Exit"})

if choice = "Google" then

set search to text returned of (display dialog "Search Google!" default answer "" buttons {"Search", "Exit"} default button 1)
open location "http://www.google.com.au/search?q=" & search

else if choice = "Youtube" then

set search to text returned of (display dialog "Search Youtube!" default answer "" buttons {"Search", "Exit"} default button 1)
open location "http://www.youtube.com/results?search_query=" & search

end if

andmr
Jun 15, 2011, 02:56 PM
A few observations:

1) If you don't intend to use the default Cancel button in your dialog, you'll need to specify a button which will act as one, as in this snippet:

set search to text returned of (display dialog "Search Google!" default answer "" buttons {"Search", "Exit"} cancel button "Exit" default button 1)

2) If no text is entered in any of the dialog boxes and the Search button is pressed inadvertently, what then? Without properly scripting for this possibility, the result will be an error and/or the Google or Youtube home page opening without a search term.

3) On my machine (Mac OS 10.4.11), using the open location command outside a Safari tell block would occasionally result in failure if a) Safari was not already open, or b) if Safari was open but hidden (I'm not sure why this would happen only occasionally). Hence, the Safari tell block in the example below.

Example script:

set choice to button returned of (display dialog "Where will you search from?" buttons {"Google", "Youtube", "Exit"} cancel button "Exit")

if choice = "Google" then
set search to text returned of (display dialog "Search Google!" default answer "" buttons {"Search", "Exit"} cancel button "Exit" default button 1)
if search is not "" then
tell application "Safari"
activate
open location "http://www.google.com.au/search?q=" & search
end tell
else
display dialog "No text was entered. Would you like to try again?" buttons {"Yes", "No"} cancel button "No" default button 1
run
end if

else if choice = "Youtube" then
set search to text returned of (display dialog "Search Youtube!" default answer "" buttons {"Search", "Exit"} cancel button "Exit" default button 1)
if search is not "" then
tell application "Safari"
activate
open location "http://www.youtube.com/results?search_query=" & search
end tell
else
display dialog "No text was entered. Would you like to try again?" buttons {"Yes", "No"} cancel button "No" default button 1
run
end if
end if


Hope this helps, good luck.