Controlling invisible windows

Hey all,
I’m trying to write a script that toggles an option in my System Preferences, without having to actually bring the preferences app up on screen.
The problem I’m having is that as soon as I " set visible of window 1 to false", the script is no longer able to recognize interface items like "button 1 of window ‘universal access’.

Is there some workaround handy? If it helps any, my script is designed to toggle Zoom on and off, and my script is below.

Thank you for any help!


tell application "System Preferences"
	activate
	set current pane to pane "com.apple.preference.universalaccess"
	--set visible of window 1 to false
end tell


tell application "System Events"
	tell application process "System Preferences"
		set zoominess to the title of button 1 of tab group of window "universal access" as string
		if zoominess is "Turn On Zoom" then
			click button 1 of tab group of window "universal access"
			delay 1
			tell application "System Events"
				keystroke "+" using {command down, option down}
			end tell
		else
			click button 1 of tab group of window "universal access"
			return 2
		end if
	end tell
end tell

Hi,

What about this:

tell application “System Events”
keystroke “*” with {command down, option down}
delay 1
keystroke “+” with {command down, option down}
end tell

gl,

Because… that’s a really good idea?
I love when people point out elegant solutions that are right under my nose. You’re just telling Applescript to do it the same way anyone else would do it. Thanks for simplifying my life!

The only problem is that it’s one-way, rather than working as a toggle, but that shouldn’t be too hard to change. I’ll try experimenting with having it return some status variable, like “zoomed” or something, so I can do a simple if-then statement.

As an aside, I’ve tied this script into the Speakable Items system, which completely rocks! My brother, for whom I’m writing this thing, has some difficulties speaking. He can’t say, for instance, “zoom in” and “zoom out” clearly enough for the computer to distinguish between them, but he’s found that he can say “zoom zoom” (the speech system likes having more than one syllable) consistantly, and use that to toggle.

My only last hurdle was that each time he zoomed it would pop the system preferences up in front of whatever he was trying to zoom in to, but your suggestion answers that problem.

Thanks again for your help!

Here’s how you make subroutines out of the code. Note that on Mac OS X 10.3 (which I’m running) you use “using” instead of “with”:

Jon


[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]

Hey thanks a lot for that code!

It’s very elegant, and it does the trick.

You guys rock!

Ok, so don’t laugh. I got it down to 5 lines of code. 2 steps, really:


tell application "System Events"
	keystroke "*" using {command down, option down}
	delay 0.5
	keystroke "+" using {command down, option down}
end tell

I realized that I don’t need to store a global variable or toggle or detect anything.

If the zoom feature is turned off when the script is runthen the first key stroke turns it on and the second keystroke zooms in. If it’s already zoomed in when the script runs, then the first keystroke turns off the zoom feature (thereby zooming back out) and the second keystroke does nothing.
Super simple.

I compiled the script as an app, made a 32bit icon of a magnifying glass (with an alpha channel to make it look even cooler), and dropped the thing on the doc. Now my brother has essentially a one click zoom feature that looks as sexy as any other part of OS X, and I have an app that I can donate to the accessability community. This has been a really great introduction to applescript.

Thanks again for your help!

Hi,

Glad it worked. If you want it to work faster, you could use a reopen run handler. This way, the script won’t keep opening and closing whenever it’s run. You just run it the first time and it stays open and can be run again. You save it as a stay open application. Something like this:

on run
tell application “System Events”
keystroke “*” with {command down, option down}
delay 1
keystroke “+” with {command down, option down}
end tell
end run
on reopen
run
end reopen

I think what Jon is showing you is that say you want to zoom in twice or three times depending on what you say like zoom zoom would zoom in twice. You could use a handler something like this:

on zoom_in(num_times)
repeat num_times
tell app “System Events” to keystroke “+” with {command down, option down}
delay 1
end repeat
return
end zoom_in

Then, you could call it in your script with:

set num_zooms to 2
tell app “System Events” to keystroke “*” with {command down, option down}
delay 1
zoom_in(num_zooms)

gl,