4

So, I decided a while ago to run a Flash-free install of Safari. It's been really great, Safari seems much more stable overall and I barely miss it.

But every once in a while I stumble upon a site that requires flash (mostly YouTube videos, which I thought wouldn't be a problem given that Google was supposed to be switching everything over to HTML5), so for those cases I have an Appescript that runs when triggered through an Alfred Workflow (with a hotkey). The script is as such:

tell application "Safari"

    set theURL to URL of current tab of window 1

end tell

tell application "Google Chrome"

    if windows ≠ {} then
        make new tab at the end of window 1 with properties {URL:theURL}
    else
        make new window
        set URL of active tab to theURL
    end if
    activate

end tell

tell application "Safari"

    close current tab of window 1

end tell

This usually runs perfectly, but I run into a weird problem whenever Chrome doesn't have an open window in the background. In these cases, Chrome will open a new (blank) window, and the script will stop right there. The third Tell statement,

 tell application "Safari"

    close current tab of window 1

end tell

Will never run, leaving me with an open chrome window and an open safari window as well. It is only after I press the hotkey a second time, after the Chrome window is already open, that the Script will run it's course and open the page in chrome/close the tab in safari.

Any ideas? I'm really confused by the fact the third tell statement won't run at all. If the Chrome window opened up blank but the safari tab still closed I would guess it was a simple error in the if/else statement, but the fact that the script seems to terminate is really stumping me on this one.

  • You do know that Safari has a built in function for this, right? Develop->Open Page With->Google Chrome. You can assign hotkey for this action in system preferences – Sergei Jun 25 '14 at 19:32
  • Yea, but this is actually part of a larger script that lets me do the same in both directions; Safari to Chrome and Chrome to Safari. Also, unlike the script the method you mention won't close the Safari window that's being opened in chrome. – Carlos Jul 1 '15 at 16:10
2

Just because Safari and Chrome are running, doesn't mean that they have a window open. You can either test for open windows as Mark points out or simply issue the reopen command which opens a new window if one does not exist.

tell application "Google Chrome"
    if not (exists window 1) then reopen
    -- insert your code 
end tell

tell application "Safari"
    if not (exists document 1) then reopen
    -- insert your code 
end tell

EDIT

tell application "Safari"
    if not (exists document 1) then return
    set theURL to URL of current tab of window 1
    close current tab of window 1
end tell

tell application "Google Chrome"
    if not (exists window 1) then reopen
    set URL of active tab of window 1 to theURL
end tell
  • A down vote without an explanation? – adayzdone Feb 9 '14 at 15:33
  • @adayzone, The "correct method is" to do what in relation to this question? – markhunte Feb 9 '14 at 15:43
  • Hi Mark. The correct method of ensuring either Chrome or Safari has a window open to prevent the errors Carlos is getting, is to issue the reopen command. If it exists, it leaves the current window as is, if not, it opens a new one – adayzdone Feb 9 '14 at 15:45
  • 1
    Yeah, I'm not sure what to do with this :/. It may be obvious to someone with a bit more knowledge of Applescript, but to me personally this answer actually leaves me with more questions xD (on a side note, you've actually answered some of my questions before, so thanks for that!) – Carlos Feb 9 '14 at 15:45
  • Ahhh I see now, you're referring specifically to the use of 'reopen'... I see where you're getting at. – Carlos Feb 9 '14 at 15:47
0

Alfred may be shielding you from an error that occurs if Chrome is running but does not have any windows open.

Google Chrome got an error: Can’t set active tab to "http.....".

Change the lines

  make new window
  set URL of active tab to theURL

to

set theWindow to make new window

set URL of active tab of theWindow to theURL
  • Ah, so it was an Alfred thing? I thought this might have been the case at first, but the whole last-tell-not-executing thing made me think it was something to do specifically. with Applescript. Your fixed worked a treat though! Thanks! – Carlos Feb 9 '14 at 15:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .