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

Automatically zoom new pages in Safari Web Browsers
Safari has a nice feature to zoom in (or out) on a webpage, which is great for large monitors. Unfortunately, you're not able to set a default zoom level for new pages. CSS hacks exist, but zooming via CSS doesn't seem to work as well as the native zoom function in Safari. The AppleScript in this hint zooms in on any new web page using Safari's own zoom function.

The script is quite a hack -- it checks every tenth of a second to see if the currently active tab isn't zoomed in. If it's not zoomed in, the script activates the corresponding keyboard shortcuts to zoom the page. You can edit the script to change how often the zoom-check should be made, and what level of zoom should be made.
repeat
  try -- we have to try, since safari might not be running, and theUrl might not be defined
    if frontmost of application "Safari" then
      tell application "Safari"
        set theUrl to URL of current tab of window 1 -- window 1 is apparently always the active window in Safari
      end tell
      if theUrl is not "" then -- this will ignore new tabs where a page is not yet loaded
        tell application "System Events"
          tell process "Safari"
            -- these may differ according to the OS X language
            set activeMenuItem to enabled of menu item "Actual Size" of menu "View" of menu bar item "View" of menu bar 1
          end tell
          if not activeMenuItem then
            -- here we fire off the key shortcuts 
            -- these may differ according to the OS X language
            keystroke "+" using command down
            keystroke "+" using command down
          end if
        end tell
      end if
    end if
  end try
  delay 0.1 -- how many seconds to wait until next check
end repeat
With a delay of 100ms, the scripts gobbles 1% of the CPU time on my system. One idea is to make an application from the script, and launch that instead of launching the script directly from the AppleScript Editor. Any suggestions for improvement are welcome!

[robg adds: I tested this in 10.6.2 on a MacBook Pro, and the script used about 5% of the CPU. It did, however, work as described.]
    •    
  • Currently 2.42 / 5
  You rated: 3 / 5 (24 votes cast)
 
[12,869 views]  

Automatically zoom new pages in Safari | 7 comments | Create New Account
Click here to return to the 'Automatically zoom new pages in Safari' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Automatically zoom new pages in Safari
Authored by: asmeurer on Jan 20, '10 08:17:05AM

Zooming is so easy on a MacBook Pro (just pinch), that it is hardly a nuisance. I also have the command-0 shortcut memorized, which will reset the zoom level.



[ Reply to This | # ]
Sorry too hard...better solution is
Authored by: MacTripper on Jan 20, '10 08:20:40AM

Firefox with the NoSquint ad-on remembers your zoom levels per site, as some sites need more or less zoom than others. Also the entire site is zoomed, not just the text, so it's not messing up the layout.

Other great ad-ons I have found: Betterprivacy, Fastestfox, Flagfox, Ghostery, Morning Coffee, No Script, Ad Block Plus, RequestPolicy and GoogleSharing. All are auto-updated every time you run Firefox, so you have the latest versions.

I love Apple, but they are bested by Firefox in the browser wars.



[ Reply to This | # ]
Automatically zoom new pages in Safari
Authored by: trosberg on Jan 21, '10 10:39:38AM
The best way to write recurring actions in AppleScript is to use "idle" block. Not actually tested fully, this hint written as an idle app follows. Also two test handlers are separated for readability. Save as an application, "Stay open" chosen in save dialog. When run, it tests and functions every 1 second. Can someone using English as UI language test it? Cheers, Tuomas Rosberg
global appPath, previousURL

on run
  set appPath to (path to application "Safari")
end run
----------
on idle
  if SafariRunningTest() is true then
    set p to (path to frontmost application)
    if p is not appPath then -- Safari is not the frontmost application
      -- idle on!
    else -- Safari is the frontmost app
      tell application "Safari"
        set currentURL to URL of current tab of window 1 -- window 1 is apparently always the active window in Safari
      end tell
      if currentURL is not "" then -- this will ignore new tabs where a page is not yet loaded
        if currentURL is not previousURL then -- check, if we have already zoomed in this tab
          if ActualSizeChecked() is false then
            -- here we fire off the key shortcuts 
            -- these may differ according to the OS X language
            tell application "System Events"
              keystroke "+" using command down
              keystroke "+" using command down
            end tell
            set previousURL to currentURL
          end if
        end if
      end if
    end if
  end if
  return 1 -- to repeat every 1 second
end idle
------
on SafariRunningTest()
  tell application "System Events"
    return exists of process "Safari"
  end tell
end SafariRunningTest
------
on ActualSizeChecked()
  tell application "System Events"
    tell process "Safari"
      -- these differ according to language chosen in System Prefs
      return enabled of menu item "Actual Size" of menu "View" of menu bar item "View" of menu bar 1
    end tell
  end tell
end ActualSizeChecked


[ Reply to This | # ]
Automatically zoom new pages in Safari
Authored by: grav on Jan 22, '10 05:43:42AM
Very nice!
It complains about previousURL not being defined, but inserting
set previousURL to ""
in the run-handler fixes this.

[ Reply to This | # ]
Automatically zoom new pages in Safari
Authored by: grav on Jan 22, '10 07:01:39AM
Another thing, we have to wrap the
if currentURL is not "" then
...
end if
in a try-statement, since currentURL won't be defined, if we just opened a new tab and haven't typed a URL yet. This wasn't obvious from my original script.
I'll post the corrected script in the root of the comments.

[ Reply to This | # ]
Automatically zoom new pages in Safari
Authored by: grav on Jan 22, '10 07:06:03AM

Script improved by trosberg. A couple of bugfixes by grav.

Save as application and check Stay Open


global appPath, previousURL

on run
	set appPath to (path to application "Safari")
	set previousURL to ""
end run
----------
on idle
	if SafariRunningTest() is true then
		set p to (path to frontmost application)
		if p is not appPath then -- Safari is not the frontmost application
			-- idle on!
		else -- Safari is the frontmost app
			tell application "Safari"
				set currentURL to URL of current tab of window 1 -- window 1 is apparently always the active window in Safari
			end tell
			try
				if currentURL is not "" then -- this will ignore new tabs where a page is not yet loaded
					if currentURL is not previousURL then -- check, if we have already zoomed in this tab
						if ActualSizeChecked() is false then
							-- here we fire off the key shortcuts 
							-- these may differ according to the OS X language
							tell application "System Events"
								keystroke "+" using command down
								--							keystroke "+" using command down
							end tell
							set previousURL to currentURL
						end if
					end if
				end if
			end try
		end if
	end if
	return 1 -- to repeat every 1 second
end idle
------
on SafariRunningTest()
	tell application "System Events"
		return exists of process "Safari"
	end tell
end SafariRunningTest
------
on ActualSizeChecked()
	tell application "System Events"
		tell process "Safari"
			-- these differ according to language chosen in System Prefs
			return enabled of menu item "Actual Size" of menu "View" of menu bar item "View" of menu bar 1
		end tell
	end tell
end ActualSizeChecked


[ Reply to This | # ]
Automatically zoom new pages in Safari
Authored by: Maxikubik on Mar 07, '11 12:31:45PM

There is a Safari plugin for this called AllPagesZoom: http://sidetree.com/extensions.html



[ Reply to This | # ]