Created
September 1, 2011 04:49
-
-
Save mayoff/1185476 to your computer and use it in GitHub Desktop.
Emacs Lisp to reload a URL in Chrome (using AppleScript)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is great, thanks for publishing it!
AFAICT, the URL should be specified without the "http://", when I initially set the URL that way, it always opened a new tab, presumably because "theTab's URL" doesn't include it, is that correct?
In your blog describing all this, you mention that you can set the URL via C-u F-5, but I don't believe you have a keybinding for that...
Thanks again