Applescript to open current Firefox 4 tab in Google Chrome

1.7k views Asked by At

I'm attempting to write an applescript for Apptivate to open the current Firefox 4 or Safari tab in Google Chrome. The Safari section works and the if statement steps in to Firefox but it doesn't copy the URL.

Here is my applescript and it doesn't work:

tell application "System Events"
set myApp to name of first application process whose frontmost is true
if myApp is "firefox-bin" then
    tell application "Firefox" to set theURL to the «class curl» of frontmost of window 1
    tell application "Google Chrome"
        activate
        tell application "System Events" to keystroke "t" using {command down}
        set URL of last tab of window 1 to theURL
    end tell
else if myApp is "Safari" then
    tell application "Safari" to set theURL to the URL in document 1
    tell application "Google Chrome"
        activate
        tell application "System Events" to keystroke "t" using {command down}
        set URL of last tab of window 1 to theURL
    end tell
end if
end tell

What am I missing?

2

There are 2 answers

0
Fábio Perez On

This might be tricky, but you can use keyboard shortcuts to copy and open the URL you want in Chrome. Check the code:

set backupClipboard to the clipboard

on ApplicationIsRunning(appName)
    tell application "System Events" to set appNameIsRunning to exists (processes where name is appName)
    return appNameIsRunning
end ApplicationIsRunning

on copyURL(appName)
    tell application appName to activate
    tell application "System Events"
        keystroke "lc" using command down
    end tell
    delay 0.2 -- to make sure keystroke will hit cmd+l & cmd+c
end copyURL

if ApplicationIsRunning("firefox-bin") then
    copyURL("Firefox")
else if ApplicationIsRunning("Safari") then
    copyURL("Safari")
else
    return
end if

tell application "Google Chrome"
    open location (the clipboard)
    activate
end tell

set the clipboard to backupClipboard

This script will first save your current clipboard, then it will check which browser is open. You can define the priority of browsers by sorting the if statement.

This is the same as pressing cmd+L (select url) and cmd+C (copy) on your browser.

Also, you can adapt the code to support other browsers.

(ApplicationIsRunning function from: http://vgable.com/blog/2009/04/24/how-to-check-if-an-application-is-running-with-applescript/)

0
creaky coder On

I am running OS 10.4.11 on a PPC, I seem to find problems with the script with things like 'foo tab'. I think the issue is i have such an old device that the scripting won't recognize these newer commands. Maybe it is me.

Anyways, I wanted to be able to trigger a copy of a URL from Firefox to Safari using my Magic Mouse, running the magicmouse.jp driver. I got it to work using the following:

tell application "Firefox" to activate

tell application "System Events"

keystroke "lc" using command down

end tell



tell application "Safari" to activate

delay 1

tell application "System Events" to keystroke "l" using {command down}

--delay 0.2

tell application "System Events" to keystroke "v" using {command down}

tell application "System Events" to keystroke return

##############

cheers.

P.S. As a side note, some research on this brings up a number of people who disable flash in Safari and then trigger a copied URL to open within Chrome, which apparently must be a safer container, for what it's worth.