开发者

Mac: reloading document in Chrome or Firefox?

开发者 https://www.devze.com 2022-12-17 07:12 出处:网络
How can I tell Chrome or Firefox to reload the document in the top window?Here\'s what I\'m using for Safari:

How can I tell Chrome or Firefox to reload the document in the top window? Here's what I'm using for Safari:

osascript -e '
    tell application "Safari"
      activate
      do JavaScript "history.go(0)" in document 1开发者_JAVA技巧
    end tell
'


Here's the code for Chrome:

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

Or more concisely:

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


I do not think Firefox or Chrome have special Applescript support, but you can send the keystrokes (Cmd-R) to refresh the page:

tell application "Firefox"
    activate
    tell application "System Events" to keystroke "r" using command down
end tell


Here's another way to do it in Safari without using JavaScript:

tell application "Safari"
    tell its first document
        set its URL to (get its URL)
    end tell
end tell


Automator

  • Open Automator and choose a New Document
  • Choose Service

Mac: reloading document in Chrome or Firefox?

  • Set Service receives to no input
  • Choose Run AppleScript action from the action list.
  • Paste the following code in the script:

 

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

Mac: reloading document in Chrome or Firefox?

  • Save the the service, for example, using the name Chrome Refresh

System Preferences

  • Open System Preferences > Keyboard
  • In the Shortcuts tab, choose Services
  • Assign a new shortcut

Mac: reloading document in Chrome or Firefox?


The following answers above work well but using them result in DevTools refreshing in a new tab if it was the last tab/window in focus. I don't want DevTools to refresh in a new tab, I just want the first tab to refresh regardless of last focus/active and this worked well for me. Leaving for someone searching for this use case as well.

tell application "Google Chrome"
  activate
  tell application "System Events"
    tell process "Google Chrome"
      keystroke "r" using {command down, shift down}
    end tell
  end tell
end tell
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号