0

I am opening a url from my OS x app. I am doing it as below

[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:urlString]];

I am doing it on a button action. But whenever I press the button a new tab is opened for me. Can I prevent it in anyways? What I need to achieve is, refresh the tab if it was already opened, rather that opening a new tab for the url. Can it be achieved?

Thanks in advance!

Advaith
  • 1,087
  • 3
  • 12
  • 31

3 Answers3

0

Try calling applescript from your cocoa application, something like this:

NSAppleScript *run = [[NSAppleScript alloc] initWithSource:@"tell application \"Safari\" to do Javascript \"location.reload(true)\" in current tab of window 1"];
[run executeAndReturnError:nil];

The script assumes that the url to refresh is opened in the current tab of the first Safari's window. I have not tried it.

Michele Percich
  • 1,872
  • 2
  • 14
  • 20
  • sorry, besides the interesting approach to use AppleScript, I fear it is a really bad advice to wildly refresh any current tab you're finding. What if the current tab of window(1) is not the one you are thinking it is, but (e.g.) a shopping basket, a banking page or a looooong form which is just about to be sent? – auco Jun 08 '13 at 11:50
0

This is an issue of the browser you're previewing and not of your application. Usually, when you open the same URL in a browser, that has this URL already opened, it will detect this and refresh the corresponding window or tab.

You can easily test this by dragging a bookmark to your desktop; then double-click it to open in your default browser. Safari for example, will refresh properly.

So:

  • either fix your code by making sure you are not changing your URL
  • try another browser and don't fix bugs of other software in your code with bogus Apple scripts. Because, I really don't want any apps to refresh my current tab (what if you're reloading not your html document, but my shopping basket that will get lost on a refresh?)
  • or if you're using a script, make sure that you absolutely got the right tab before attempting to refresh

Edit: checked Crome and FF, they open new Tabs indeed...

auco
  • 9,329
  • 4
  • 47
  • 54
-1

IF you arent sandboxed, you could use applescript

NSString *s = @"tell application \"Safari\"\
    activate\
end tell\
\
tell application \"System Events\"\
    tell process \"Safari\"\
        keystroke \"r\" using {command down}\
    end tell\
end tell";

NSAppleScript *run = [[NSAppleScript alloc] initWithSource:script];
[run executeAndReturnError:nil];
Daij-Djan
  • 49,552
  • 17
  • 113
  • 135