Vitor Pepicon Vitor Pepicon - 3 months ago 14
AppleScript Question

Applescript won't make Safari open a URL at a new window

I'm making a Applescript service that receives selected text and opens a google query with this text. This was the first version of it:

on run {input, parameters}
set myBrowser to http://google.com.br?q=" & input as text
set the_url to "Safari" as text

tell application myBrowser
open location "http://google.com.br?q=" & input
set the bounds of the front window to {100, 22, 800, 1024}
activate
end tell
end run


This one above works well. The problem came when I tried to make the browser open a new page with the query, instead of a new tab. I had to come up with a solution for it not opening two tabs at the new window, as it would happen when the script is triggered and Safari is closed:

on run {input, parameters}
set the_url to "http://google.com.br?q=" & input
set myBrowser to "Safari" as text

set aWindowIsOpen to false
tell application myBrowser
repeat with thisWindow in windows
if (not miniaturized of thisWindow) then
set aWindowIsOpen to true
end if
end repeat

if (aWindowIsOpen) then
make new document with properties {URL:the_url}
set the bounds of the front window to {100, 22, 800, 1024}
activate
else
activate
make new document with properties {URL:the_url}
set the bounds of the front window to {100, 22, 800, 1024}
activate
end if
end tell
end run


So the problem now is that the browser won't open the URL. Any thoughs?

Answer

Use tell application "Safari" instead of tell application myBrowser.


Or, use using terms from application "Safari", like this:

set myBrowser to "Safari"
using terms from application "Safari"
  tell application myBrowser
      make new document with properties {URL:the_url}
  end tell
end using terms from