通过terminal命令使用浏览器自动重新加载html文件

Str*_*mer 7 html unix macos bash terminal

我正在尝试做一些小的HTML网页内容,并且发现每当我保存文件时能够看到通过浏览器查看的html文件的实时更新非常有用.我知道可能有IDE为你做这个,如果你推荐任何,请做,但我想制作一个脚本,只需在浏览器中打开文件,同时关闭该文件的版本已打开,以便我可以继续在vim中完成所有编程.

我知道我可以在Mac OSX中使用以下内容,将文件打开到我当前浏览器的新选项卡中:

open foo.html
Run Code Online (Sandbox Code Playgroud)

但是,如果我在定时循环中发生这种情况,或者每次我在vim中编写(:w),我的浏览器都会填满新标签.有没有办法在打开新标签时关闭旧标签?或者有没有更好的方法,我没有考虑过?如果我可以继续在终端使用vim,那将是非常优选的.

提前致谢.

cod*_*ody 12

您可以使用AppleScript重新加载选项卡.见Benjie的答案这个问题,
使用osascript从shell脚本调用的AppleScript.你会得到这样的东西:

osascript -e 'tell application "Google Chrome" to tell the active tab of its first window to reload'
Run Code Online (Sandbox Code Playgroud)

或者,您可以使用类似下面的内容关闭所有以前的标签:

tell application "Google Chrome"
    set windowList to every tab of every window whose URL starts with "http://stackoverflow.com"
    repeat with tabList in windowList
        repeat with thisTab in tabList
            close thisTab
        end repeat
    end repeat
end tell
Run Code Online (Sandbox Code Playgroud)


Lri*_*Lri 3

如果已经有 的选项卡foo.htmlopen foo.html则应在 Safari 中聚焦该选项卡。对于 Chrome,你可能会使用这样的东西:

\n\n
set u to "http://t.co/"\ntell application "Google Chrome"\n    repeat with w in windows\n        set i to 0\n        repeat with t in tabs of w\n            set i to i + 1\n            if URL of t is u then\n                set active tab index of w to i\n                set index of w to 1\n                tell t to reload\n                activate\n                return\n            end if\n        end repeat\n    end repeat\n    open location u\n    activate\nend tell\n
Run Code Online (Sandbox Code Playgroud)\n\n

我刚刚将 \xe2\x8c\x98R 分配open "$TM_FILEPATH" -a Safaritext.html。我还启用了在切换到另一个应用程序时保存文档,因此它基本上执行了编辑-保存-切换应用程序-刷新周期的最后三个步骤。

\n\n

其他选项:

\n\n\n