1

When I am coding my Rails app I am switching back to Safari quite often to refresh my local version of the site to see if my changes are correct. It would be awesome to just hit one of the function keys and that would bring my Safari window to the foreground and manually refresh the page.

I can't seem to find an obvious example of this or anything along these lines fro the typical Google searches etc.

Any idea if this can be done and if so how (at least the Automator part)?

5
  • After Safari is open (and some random page), you can open automator, click record, and then type command-r. Then stop the recording. I'll test that out to make sure it works now. – Flare Cat Jan 21 '16 at 21:10
  • Ok, looks like it works. After the recording, just keep the Finder icon that says Press command R, and that tells the window to come to the front. I then have no idea how to make this into a function key though, someone else my be able to help you out with that. This will just create a .app – Flare Cat Jan 21 '16 at 21:14
  • Sorry for bad grammar, I am busy and thought I'd give you a lead. – Flare Cat Jan 21 '16 at 21:15
  • I pretty much stumbled onto the same answer as you have. I tried adding it to the keyboard app short cuts but no luck so far. I recorded the action of clicking on the tab. It seem to remember the tab name. Works as long as that same tab is open somewhere. – Dan Tappin Jan 21 '16 at 21:17
  • I think it is possible to start a process/app from a function key press, but I have no idea how. – Flare Cat Jan 21 '16 at 21:18
3

Paste this script into script editor and save it as a service. Map the service to a keyboard shortcut.

tell application "Safari"
    activate
    tell current tab of window 1
        do JavaScript "location.reload();"
    end tell
end tell

EDIT

tell application "Safari"
    activate
    tell window 1

        --options
        set myTab to tab 1
        set myTab to first tab whose URL = "https://apple.stackexchange.com/questions/224393/automator-workflow-app-to-refresh-a-given-tab-in-safari-or-even-crome/224431?noredirect=1#comment272178_224431"
        set myTab to first tab whose URL starts with "https://apple.stackexchange.com"

        if current tab is not myTab then set current tab to myTab
        tell myTab to do JavaScript "location.reload();"
    end tell
end tell

EDIT 2 Select your tab and run this script to get its name

tell application "Safari"
    activate
    tell window 1
        get name of current tab
    end tell
end tell

Populate the name in this script:

tell application "Safari"
    activate
    tell window 1
        set myTab to first tab whose name = "insert name here"
        if current tab is not myTab then set current tab to myTab
        tell myTab to do JavaScript "location.reload();"
    end tell
end tell
6
  • Can you specify the name of the tab to make sure the right one gets called? Or the first tab? – Dan Tappin Jan 22 '16 at 1:38
  • Safari got an error: Can’t get tab 1 of window 1 whose URL = apple.stackexchange.com/questions/224393/…. Invalid index. – Dan Tappin Jan 22 '16 at 3:11
  • I would imagine the URL is wrong. Try a simpler URL or starts with ... – adayzdone Jan 22 '16 at 3:27
  • I did (and actually had those two pages open too). It fails no mater which URL I use. – Dan Tappin Jan 22 '16 at 3:42
  • Hmmm.. works now (sort of). I can't do the "starts with" on my URL which is "localhost:3000". That triggers that warning. I got it to work with window 2 tab 1. This will break one day when I open the windows in the other order. – Dan Tappin Jan 22 '16 at 3:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .