Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Make Safari open links from applications in the current space Apps
Safari is my default browser, and I have its preferences set to open links from applications in a new tab in the current window. In addition, I use Spaces to segregate my tasks, and I have the 'When switching to an application, switch to a Space with open windows for the application' Spaces' preference unselected.

It is often the case that I have a Safari window open in Space 1 (say), but I've switched to Space 2 to work on another task. I then need to open a URL from an application other than Safari while working on the Space 2 task. If Safari doesn't already have a window open in Space 2, but does have a window in Space 1, Safari opens the URL in a new tab in the window in Space 1. I would rather Safari open a new window in the current Space to handle the URL.

This is doable, but requires a little magic.



As a prerequisite, you'll need to install the RCDefaultApp preference pane.
  1. Open AppleScript Editor and paste in the following code:
  2. Save the document using file format Application, and select the Stay Open checkbox. Name it SafariURLHelper. I have an Applications folder in my home directory in which I saved it.
  3. Using the Finder, navigate to where you saved SafariURLHelper, then Control-click its icon and select Show Package Contents from the contextual menu.
  4. Inside the Contents folder, find Info.plist, and open it in a text editor.
  5. Add the following snippet to the bottom of the file, just before the closing </dict> and </plist> tags:
    <key>CFBundleIdentifier</key>
      <string>com.apple.AppleScript.SafariURLHelper</string>
      <key>CFBundleURLTypes</key>
      <array>
        <dict>
          <key>CFBundleURLName</key>
          <string>SafariURLHelper</string>
          <key>CFBundleURLSchemes</key>
          <array>
            <string>http</string>
            <string>https</string>
          </array>
        </dict>
      </array>
      <key>NSUIElement</key>
      <true/>
    A little bit of explanation. Most of the above text comes from this tip on macosxautomation.com, and just registers this application as something which can handle http and https links. The NSUIElement addition causes SafariURLHelper to be a background (faceless) application.
  6. Save and close Info.plist, then in the Finder, double-click on SafariURLHelper. Nothing will appear to happen. This is fine; you've just launched SafariURLHelper as a hidden background process. More importantly, this registers it as a handler for http and https URLs.
  7. Open the Default Apps System Preferences pane. Click on the URLs tab, then set the default application for both http and https to SafariURLHelper (it should appear in the drop-down automatically).
That's it. Now http and https URLs will be sent to SafariURLHelper. It will check to see if Safari has any windows open in the current space, and if not, it will tell Safari to open one. It then just passes the link along to Safari.
    •    
  • Currently 1.50 / 5
  You rated: 1 / 5 (8 votes cast)
 
[11,208 views]  

Make Safari open links from applications in the current space | 10 comments | Create New Account
Click here to return to the 'Make Safari open links from applications in the current space' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Make Safari open links from applications in the current space
Authored by: tempel on Oct 28, '09 10:16:34AM
I've added an "activate" command to the script, which brings Safari to front instead of leaving the app in front that contained the link.

I replaced the line beginning with "tell application Safari" into these lines:

	tell application "Safari"
		open location theURL
		activate
	end tell
If you had already run the script without this improvement, quit it using "Activity Monitor.app". You can also update the saved Helper app in Script Editor without the need to edit the Info.plist again - our changes are preserved.

[ Reply to This | # ]
Make Safari open links from applications in the current space
Authored by: burgerga123 on Oct 28, '09 12:21:48PM

Hmmm... this doesn't seem to be working for me. I thought at first that it might be that I have Safari set to open links from applications in a new tab. I changed that setting to new window, but it still doesn't seem to be working. The program definitely opens the link and sends it to safari, it just doesn't do it in the current tab. Anyone else having this problem?



[ Reply to This | # ]
Make Safari open links from applications in the current space
Authored by: burgerga123 on Oct 28, '09 12:35:20PM

Well I kind of fixed it. I realized I had Spaces set to always open Safari in Tab 1. I removed this requirement and it now opens it in the current tab. The only problem is it opens it along with a blank window. I think the problem is with the applescript. From what I can tell (I don't know much AS code), it seems like it is telling Safari to open a new window, then open the url, which opens a new window of it's own. I'm assuming the first open window is required to get safari to open the link in the current space. Any ideas?



[ Reply to This | # ]
Make Safari open links from applications in the current space
Authored by: everkleer80 on Oct 28, '09 01:27:24PM

You seem to be referring to different spaces as 'tabs' whereas the OP is referring to the tabs in the Safari window when they say they have Safari's "preferences set to open links from applications in a new tab in the current window." This is a setting in Safari's preferences under "tabs" and it keeps Safari from opening the new window you are seeing. I suspect that you have this setting turned off. If so then I think turning this setting on would be the solution.



[ Reply to This | # ]
Make Safari open links from applications in the current space
Authored by: everkleer80 on Oct 28, '09 03:35:29PM

Sorry, since I was not at home when I wrote that, I tried to tell you where the setting was from memory and, of course, got it wrong. It is actually under General in Preferences and the choices are "in a new window" or "in a new tab in the current window" and jaysoffian had "in a new tab..." selected. I just did a test and if you are using the "in a new window" option then you do not need this hint as everything should already work; this is only needed when Safari is set to open links in an existing window and the existing window happens to be in another space.



[ Reply to This | # ]
Make Safari open links from applications in the current space
Authored by: burgerga123 on Oct 29, '09 01:08:27PM
My bad, I was using the wrong words :) When I said tab I meant space. Anyways, I sort of have this working now. I set Safari to open links in new tabs instead of new windows. So then when the link was clicked the new window would open in the current space and then the link would open in a new tab. This however means that the new safari window had two open tabs, one with the link and one Top Sites tab. I wrote a bit of script to the end of the URLHelper App to make Safari close the first tab, this generally works except when Safari already had an open window, then it closed the first tab anyways, even if it had something open.

So i just worked on it a bit more and modified the if else statement of the code. It's working perfectly now:


on open location theURL
	tell application "System Events"
		if (count of windows of process "Safari") = 0 then
			tell application "Safari" to make new document
			tell application "Safari" to open location theURL
			tell application "Safari" to close first item of tabs of first item of windows
		else
			tell application "Safari" to open location theURL
		end if
	end tell
end open location
Have fun with this. I'm liking it already :)

[ Reply to This | # ]
Make Safari open links from applications in the current space
Authored by: Fivesheep on Nov 05, '09 03:25:41AM
The original version of this script has a bug that it couldn't open the url when safari is not running. below is my version which has it fixed:

on open location theURL
	tell application "System Events"
		try
			if (count of windows of process "Safari") = 0 then
				tell application "Safari" to make new document with properties {URL:theURL}
			else
				tell application "Safari" to open location theURL
			end if
		on error
			tell application "Safari" to set URL of front document to theURL
		end try
		tell application "Safari" to activate
	end tell
end open location
P.S, in order to run this script, you should have "Enable access for assistive devices" enabled on System Preferences->Universal Access

[ Reply to This | # ]
Make Safari open links from applications in the current space
Authored by: rootepiepsilon on Jun 19, '10 11:26:44PM

Dude, hecka easy...check this out..
1. go to the space Safari is opened;
2. open a New Window from the File menu
3. click on the New Window that is now opened and make sure you can move it around
4. while holding it down change the Space by holding control and moving to the desired space with the arrow keys.
5. erase all the stupid software that you downloaded to do this and do not work ;-)
6. I know this doesn't technically open a new window in the Space you currently are but it's an easy way to make up for all the downloading especially if you don't want to get all technical...



[ Reply to This | # ]
Make Safari open links from applications in the current space
Authored by: bumper314 on Sep 26, '13 07:45:40AM

This hint worked well for me on 10.8.5, but it is a bit tricky for the average user.

The big trick I found is in step #2, saving the application to the Desktop first. Make all the edits through step #6, then move the application to the Applications folder. By moving the application, Finder refreshes the launch services database so it's fully aware of the http/https URL scheme this application is registering for.



[ Reply to This | # ]
Make Safari open links from applications in the current space
Authored by: bleak on Oct 30, '13 01:15:58AM

I have a problem when opening links from other Mac apps like Mail, Stickies, etc in Safari 7.0 (OS X 10.9). The links will not open in the last window I was viewing. They open in a window in back of the current window. I usually have four Safari windows open. I have the Safari prefs set to open link in a new tab automatically but not to switch to the tab.

I'm using the Spaces/Mission Control prefs and have a separate desktop for Safari and Mail.

I've been through the wringer with this. Apple says it's a bug and will fix in the latest OS X. Other forums claim that it's one of the pref files but no one can tell me which one and I can't find it. It's not simply a matter of trashing the com.apple.Safari.plist.

Applescript seems to be my only hope.

I've tried this method but it does not work for me. Now, when I click on a link in Mail, Mail deactivates but nothing else happens. It doesn't switch to Safari, open the link in a new tab in ANY window, or do anything. I've gone through in three times with a fine tooth comb but can't get it to work.

Note: I've had this problem from Mountain Lion (10.8.5).



[ Reply to This | # ]