Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

sammich

macrumors 601
Original poster
Sep 26, 2006
4,306
268
Sarcasmville.
Well, I decided to make this little script for Safari. Just for the lazy people. I posted this in another thread, but decided to make a thread as well.

All this does is close Safari and reopen it with the pages you had from before. Plus a handy, if poorly implemented little message at the end of it all. This isn't the most elegant way to do it, but it's the best I can do with my knowledge of AS and google :D You'll need to open "Script Editor" (spotlight it), copy and paste the code below, and then press run. You can save it as a standalone app from within Script Editor if you like.

Modify, share, enjoy.

Code:
-- will only work with "Safari" and "Webkit"
set brwsr to "Safari"

set before_mem to -1
if my isRunning(brwsr) then
	set before_mem to my getProcMB(brwsr)
end if

tell application "System Events" to set open_apps to (get name of every application process)
if open_apps contains brwsr then
	tell application brwsr to activate
	tell application brwsr to quit
end if
tell application "System Events" to set open_apps to (get name of every application process)
set counter to 1
repeat while open_apps contains brwsr
	delay 0.3
	tell application "System Events" to set open_apps to (get name of every application process)
	set counter to (counter + 1)
	if (counter) is greater than 30 then
		display dialog "Safari is taking too long to close, script will end now"
		break
	end if
end repeat
tell application "System Events" to set open_apps to (get name of every application process)
tell application brwsr to activate

tell application brwsr to activate
tell application "System Events"
	tell process brwsr
		tell menu bar 1
			tell menu bar item "File"
				tell menu "File"
					click menu item "Close Window"
				end tell
			end tell
			delay 0.1
			tell menu bar item "History"
				tell menu "History"
					click menu item "Reopen All Windows from Last Session"
				end tell
			end tell
		end tell
	end tell
end tell

--
-- below is adapted from code by "NovaScotian" from http://forums.macosxhints.com/archive/index.php/t-64782.html
--

if my isRunning(brwsr) then
	set after_mem to my getProcMB(brwsr)
end if

if before_mem is not equal to -1 then
	tell application "Safari" to display dialog "You just freed up " & (before_mem - after_mem) & "MB of RAM. Nice work! :D"
end if
on isRunning(appName)
	tell application "System Events" to if exists process appName then return true
	return false
end isRunning

to getProcMB(procName) -- as string
	try
		return ((last word of (do shell script "/bin/ps -xc -o command,rss | grep " & procName)) as integer) / 1024 -- result of a division will be real
	end try
end getProcMB
 
Sorry, but isn't this already in Safari?

edit: ok I see you have this in there already... so what advantage do I get from using the Applescript instead of just clicking on it myself?
 

Attachments

  • Picture 2.png
    Picture 2.png
    70.5 KB · Views: 81
Sorry, but isn't this already in Safari?

Yeah, but you kinda need Safari to quit and reopen. I only wrote it because I was too lazy to actually do the clicking needed to do all of that.

Okay, yes, the script above doesn't do very much, it simply does stuff you would otherwise need to click with. I'm simply giving people a way to easily claim back RAM usage from Safari.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.