Skip to content

Fixing Safari and Spaces

by Nicholas Barnard on August 28th, 2011

For those of you who are used to my usual musings on society, politics, and just general stuff, you’ll can safely skip this entry. This is a completely geeky blog entry, that applies pretty specifically to OS X Lion.


So I don’t talk about programming/scripting much, since well I don’t do much of it, and what I do is usually so specific to my needs, it doesn’t make sense to share it with the world. In this instance however, I think this’ll have a fairly common application.

I’ve actually started using spaces, or as they call it now Mission Control in OS X. I’ve previously fiddled with it, but I’ve never really put it into standard use in my daily work. However, with Lion and its iOS like gestures, I have finally started using it. What I’ve done is put each of my basic organizational tools in its own space, so its easy to four finger swipe to it. My standard setup has five spaces:

  • Dashboard — Which I like in its own space after initially being really annoyed with that setup.
  • My main workspace — I do most of my web browsing, email, chatting, etc from this space
  • Safari, opened to Toodledo — I organize all my tasks and a bunch of other things in this great web application.
  • iCal — I really like this calendaring application. It works quite well for my needs, and Apple hasn’t screwed it up in Lion.
  • Safari, opened to Mobile Me‘s Contacts Page — If I need to find someone’s address or phone number this is where I do it, since Apple completely screwed up the address book in Lion.

So this serves me pretty well, except when:

  1. I’m interacting with another application in my main workspace.
  2. I open a link from that application.
  3. And, I don’t have a non-minimized window of Safari open in my main space.

What I’d like to have happen would be for Safari to open a new window in my main workspace. However, Safari has different ideas, and opens a tab in one of my other spaces with Safari in it, and switches to it. (usually the Toodledo one, although I’m not quite sure how it makes its choice.) Usually, I try not to write any code/scripts, I start with Googling around and see if I can find something that fits my needs.

So, as per my SOP, I went Googling and found a good script by jaysoffian over at MacWorld. Its from the Snow Leopard days, and actually using it has a few quirks that may be Lion specific. It does a great job if there are no instances of Safari in the main workspace, but if there is a minimized window, it believes this is sufficient to open the page in the current space, however Safari opens a tab in one of the other Spaces with Safari in it. So, more Googling for me! I found a script by relishgargler over at MacScripter which determines if a minimized window of the application exists. Since, I only have one space where Safari could be minimized, this works for me.

The final quirk that I came upon was an edge case, that probably qualifies as a bug. If your last interaction with Safari was while it was full screen, it opens the link in a window, but the title and close, minimize, resize, and full size buttons are obscured below the menu bar. I did some fiddling that’ll fix that, however, I wasn’t able to find a way to see if this condition existed, so I just put the fix in for it every time that it could occur, which means that the window will do a little dance frequently. It isn’t ideal, but it works.

So I’ve put the script below as its source. Simply copy and paste, save as an application with the Stay Open option chosen. For those of you who don’t want to muck with AppleScript editor, you can get SafariURLHelper3 from, uh that link. Simply unzip it and drop it somewhere nice on your Mac, I suggest putting it in the utilities folder.

Finally, to make it actually work, go to Safari->Preferences->General, choose select, then find SafariURLHelper3 on your system. And you’re done.

-- SafariURLHelper3, heavily modified from http://hints.macworld.com/article.php?story=20091013114424722 


on open location theURL
	--- Figures out what application is currently in front, given one method of opening a window needs to make Safari Frontmost.
	set CurFront to GetFrontmostApp()
	
	--Identifies if there are any windows of safari in this space, if not it creates a new window	
	tell application "System Events"
		if (count of windows of process "Safari") = 0 then
			tell application "Safari" to make new document
			my FixPositioning(CurFront)
			tell application "Safari" to open location theURL
			return
		end if
	end tell
	
	-- Identifies if there are any minimized windows of Safari.  Might not work properly if you have it minimized in other spaces.  YMMV.
	set MinCheck to 0
	tell application "Safari"
		repeat with i in every window
			tell i
				if miniaturized is true then
					set MinCheck to 1
					exit repeat
				end if
			end tell
		end repeat
	end tell
	
	if MinCheck is not equal to 0 then
		tell application "Safari" to make new document
		my FixPositioning(CurFront)
		tell application "Safari" to open location theURL
		return
	end if
	
	-- If we've not opened the URL above, just hand it off to Safari to open it.
	
	tell application "Safari" to open location theURL
	
end open location

-- From http://hintsforums.macworld.com/archive/index.php/t-22098.html


on GetFrontmostApp()
	repeat
		tell application "System Events"
			try
				set apName to name of first process whose frontmost is true
			on error --because Finder 7.5.1 doesn't list itself...
				if frontmost then
					set apName to "Finder"
				else --just to be on the safe side!
					set apName to ""
				end if
			end try
			if apName ≠ "" then exit repeat
		end tell
	end repeat
	return apName
end GetFrontmostApp

on FixPositioning(CurFront)
	-- Ensures an edge case doesn't cause issues. If you've interacted with Safari full screen in a space it mispositions the window cutting off the title, and Close/Minimize/Resize buttons  
	tell application "System Events"
		tell process "Safari" to set frontmost to true
		tell process "Safari" to click menu item "Zoom" of menu "Window" of menu bar 1
		tell application "Safari" to set the bounds of the first window to {140, 0, 1160, 775}
		tell process CurFront to set frontmost to true
	end tell
	return
end FixPositioning

From → Uncategorized