LoginSignup
9
16

More than 5 years have passed since last update.

EmacsからChromeを操作する

Last updated at Posted at 2018-06-22

開発中はエディタとブラウザを行き来する機会がとても多いです。
そんなとき、カーソルを動かしたりスイッチャーを起動したりしてエディタ・ブラウザを切り替えるのは非常に面倒くさいです。
なので「エディタからブラウザ操作してしまえばいいじゃないか」という発想でやってみました。

開発環境

  • Emacs(Shell Commandを叩ければなんでもOK)
  • Mac(AppleScriptを用いるためMacOSが必要)

何ができるの?

Emacsからフォーカスを外すことなく、ChromeのReloadTab SwitchScrollができます。

Reload

reload.gif

Tab Switch

switch_tab_chrome2.gif

Scroll

scroll_chrome2.gif

どうやるの?

AppleScriptの用意

まず以下のそれぞれのAppleScriptを作成します。
作成したAppleScriptには chmod +x で実行権限を与えておいてください。

Reload

これはこちらを参考にさせていただきました。

ファイル名:reload_chrome

#!/usr/bin/osascript

tell application "Chrome" to tell the active tab of its first window
    reload
end tell

Tab Switch

ファイル名:switch_chrome_tab

#!/usr/bin/osascript

tell application "Google Chrome"
    set tabcount to number of tabs in front window
    set current_tab_index to get active tab index of front window
    if current_tab_index equal to tabcount
        set (active tab index of first window) to 1
    else
        set (active tab index of first window) to current_tab_index + 1
    end if
end tell

Scroll

これはこちらを参考にさせていただきました。

ファイル名: scroll_chrome

#!/usr/bin/osascript

on run argv
    tell application "Google Chrome"
        tell active tab of window 1
            if ("down" = item 1 of argv) then
                execute javascript "var x = document.documentElement.scrollLeft || document.body.scrollLeft; var y = document.documentElement.scrollTop || document.body.scrollTop; y += 100; window.scroll(x, y);"
                return
            else if ("up" = item 1 of argv) then
                execute javascript "var x = document.documentElement.scrollLeft || document.body.scrollLeft; var y = document.documentElement.scrollTop || document.body.scrollTop; y -= 100; window.scroll(x, y);"
                return
            end if
        end tell
    end tell
end run

Emacsの用意

ユーザ定義関数をinit.elに記述してやります。

;; pathを通す
(add-to-list 'exec-path "/path/to/applescript")

;; ChromeをReloadする
(defun my-reload-chrome()
    (interactive)
    (shell-command "reload_chrome"))
(global-set-key (kbd "H-r") 'my-reload-chrome) ;; お好みで

;; ChromeのTabを切り替える
(defun my-switch-chrome-tab()
    (interactive)
    (shell-command "switch_chrome_tab"))
(global-set-key (kbd "H-t") 'my-switch-chrome-tab) ;; お好みで

;; Chromeを下にスクロールする
(defun my-scroll-chrome-down()
  (interactive)
  (shell-command "scroll_chrome down"))
(global-set-key (kbd "H-j") 'my-scroll-chrome-down) ;; お好みで

;; Chromeを上にスクロールする
(defun my-scroll-chrome-up()
  (interactive)
  (shell-command "scroll_chrome up"))
(global-set-key (kbd "H-k") 'my-scroll-chrome-up) ;; お好みで

参考

lawrencejones/refresh.applescript
シマリスkawaii/Google Chromeで閲覧中のページをEmacsからスクロールする ※Mac限定

9
16
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
16