|
|
@@ -0,0 +1,58 @@ |
|
|
(defun mayoff:open-url-in-chrome (url) |
|
|
"Open URL in Google Chrome. I use AppleScript to do several things: |
|
|
1. I tell Chrome to come to the front. If Chrome wasn't launched, this will also launch it. |
|
|
2. If Chrome has no windows open, I tell it to create one. |
|
|
3. If Chrome has a tab showing URL, I tell it to reload the tab, make that tab the active tab in its window, and bring its window to the front. |
|
|
4. If Chrome has no tab showing URL, I tell Chrome to make a new tab (in the front window) showing URL." |
|
|
(when (symbolp url) |
|
|
; User passed a symbol instead of a string. Use the symbol name. |
|
|
(setq url (symbol-name url))) |
|
|
(do-applescript (format " |
|
|
tell application \"Google Chrome\" |
|
|
activate |
|
|
set theUrl to %S |
|
|
|
|
|
if (count every window) = 0 then |
|
|
make new window |
|
|
end if |
|
|
|
|
|
set found to false |
|
|
set theTabIndex to -1 |
|
|
repeat with theWindow in every window |
|
|
set theTabIndex to 0 |
|
|
repeat with theTab in every tab of theWindow |
|
|
set theTabIndex to theTabIndex + 1 |
|
|
if theTab's URL = theUrl then |
|
|
set found to true |
|
|
exit |
|
|
end if |
|
|
end repeat |
|
|
|
|
|
if found then |
|
|
exit repeat |
|
|
end if |
|
|
end repeat |
|
|
|
|
|
if found then |
|
|
tell theTab to reload |
|
|
set theWindow's active tab index to theTabIndex |
|
|
set index of theWindow to 1 |
|
|
else |
|
|
tell window 1 to make new tab with properties {URL:theUrl} |
|
|
end if |
|
|
end tell |
|
|
" url))) |
|
|
|
|
|
(defvar mayoff:open-in-chrome-url nil |
|
|
"*The URL that the mayoff:open-in-chrome function will send to Google Chrome.") |
|
|
|
|
|
(defun mayoff:open-in-chrome (arg) |
|
|
"Open or reload a file in Google Chrome. If you give me a prefix argument, I get Chrome's currently-displayed URL and save it for the future. If you don't give me a prefix argument, I send the previously-saved URL to Chrome for reloading." |
|
|
(interactive "P") |
|
|
(cond |
|
|
(arg (setq mayoff:open-in-chrome-url (do-applescript "tell application \"Google Chrome\" to get window 1's active tab's URL"))) |
|
|
((not mayoff:open-in-chrome-url) (error "You haven't set a URL for me to send to the browser.")) |
|
|
(t (save-buffer) |
|
|
(mayoff:open-url-in-chrome mayoff:open-in-chrome-url)))) |
|
|
|
|
|
(global-set-key (kbd "<f5>") 'mayoff:open-in-chrome) |