Frustrating GUI Script...

O man, this is frustrating. And I bet the answer is something really simple too.

My script basically needs to select a text field. Not highlight everything, just basically set its focus to true. I’ve gotten to the point where if I do

set isItFocused to focused of text field 1 --“true” or “false”

but something like this doesn’t work:

set focused of text field 1 to true – System Events got an error: NSInternalScriptError

The UI Element Inspector indicates that the “focused” value is writable, but apparently not in the way I’m trying. Any ideas?


PS: Here’s the full code:

tell application "System Events"
	tell application "Terminal" to activate
	tell process "Terminal"
		tell window "Colors"
			tell group 1
				tell group 1
					tell group 1
						tell group 1
							tell group 1
								tell group 1
									tell list 1
										delay 1
										set focused of text field 1 to true -- error
									end tell
								end tell
							end tell
						end tell
					end tell
				end tell
			end tell
		end tell
	end tell
end tell

How about “click text field 1”?

Are you positive that text field 1 is in scope at the given level?

“click text field 1” doesn’t work… I tried replacing “set focused of text field 1 to true” with:

click text field 1
get focused of text field 1

and I still get “false,” even if I put a delay between the click and get commands. Unless of course the text field was focused to begin with, in which case it returns “true”

When you ask if “text field 1 is in scope at the given level,” are you just asking if all of my tell commands refer to a text field that actually exists? Or is it something else? If it’s the former, I know that I have them lined up correctly because if I switch “set focused of text field 1 to true” to “get value of text field 1,” it correctly returnes the value. Strangeness.

From the script I can’t test what you’re trying to do…I’m just not sure. what is the problem you are trying to solve?

The purpose of the script is to change the color settings of the current Terminal window. Another part of the script which I’ve worked out would open the window settings panel, choose “Colors” from the popup button, and then click one of the options to change the color of. This opens up the “Colors” window, which is the one referred to in the part of the script you see. Another part of the script would choose “HSB Sliders” from the popup menu in the “Colors” window.

None of this is a problem… the problem comes when I try to set my own values for the colors. Initially I tried to set the value of the sliders, but something strange would happen: the indicators would move where they were supposed to, but the color wouldn’t change. Only after clicking on one of the indicators where they were would the color change. And for some reason if I would click on one of the slider indicators after the Terminal activated, the script would work fine… but it would never work if I don’t physically click the slider indicator in the first place. If I would try to mimic the click using System Events, it wouldn’t work either.

Then I tried changing the values of the text fields next to the sliders. Again, I could change the values, but unless I clicked on them the actual color wouldn’t change. If I did a “get value” command after I changed it, it would come up with the value I want, but still wouldn’t affect anything.

So to solve this whole problem, I think that instead of setting in the value of the text fields, I should fill in each of them using keystroke commands and then end with a return, which should give me the desired effect.

But no matter what I try, I can’t set the value of the text fields using keystroke commands. This is all because I can’t focus on any one of the text fields… the keystroke commands don’t know where to go.

Um, the Terminal app is scriptable. Why not use this code?


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

Ah! I totally forgot… I know that those colors are scriptable, but the SELECTION color is not. That’s really the one that I’m trying to work on. I can get the selection color fine… but can’t set it to anything.

So, you’re trying to set to a specific color, one that you know the values for, so you can put those values into the text fields. You can assign color values to the different colors as shown in Jonn’s script. Unfortunately, the color values depend on the color depth setting for your monitor and are RGB values (obviously because that is the color space for monitors). You can find the values, but it would work only for monitors with the same settings.

Open the window setting and Terminal and set the colors to your satisfaction, then

tell application "Terminal"
	set bgColor to background color of window 1
	set bgRed to item 1 of bgColor
	set bgGreen to item 2 of bgColor
	set bgBlue to item 3 of bgColor
	display dialog "The background color is: " & bgRed & "," & bgGreen & "," & bgBlue
end tell

You can do the same for cursor color, normal text color and bold text color. Then just:

set background color of window 1 of Terminal to {red value, green value, blue value}

and so on.
This really depends on your display settings, I don’t know if you can distribute the script.

That works well, but still doesn’t get allow me to get (or apply) the color of the selected text. In the Colors window selected text has it’s own defined color, but isn’t scriptable in any way. Hmmmmm