simple variable problem

i have a script that will change the value of a text field, if it is less than a certain numner. However, it only kestrokes “1”, rather than the original value + 1. Does anyone know why?

 
		set timeValue to value of text field 3 of tab group 1 of group 1 of sheet 1 of window 1
		set timeValue1 to (timeValue + 1)
		if timeValue is less than 21 then
			tell application "System Events"
				tell process "CronniX"
					keystroke timeValue1
				end tell
			end tell
		end if
		


  

I’m a newbie here, and a wee-baby AppleScript coder … but I hope that in trying to work through others’ problems I’ll improve my own knowledge. So please take this all with a grain of salt and as well intentioned, if not based on a whole lot of knowledge.

Disclaimers over …

Could it be that your timeValue is not the type of value to which 1 can be added to make a usable timeValue1?
I don’t know what the value of your “text field 3 of tab group 1 of group 1 of sheet 1 of window 1” is going to be but, given that it’s a text field, might it be a string?
Your script seems to work OK when I replace “value of text field 3 of tab group 1 of group 1 of sheet 1 of window 1” with a number. Maybe you can coerce your “value of text field 3 of tab group 1 of group 1 of sheet 1 of window 1” to be the right sort of entity for adding 1 to … and maybe I’m away off with the pixis :slight_smile:

Cheers
Dougal

Applescript will coerce a string into a number so it might not be that which is causing you trouble. ie…

set x to "3"
x + 3

What could be happening is, and this is just a guess, is that any number over 9 is actually two keystrokes. You can’t have a keystroke that is ‘10’ - it’s actually a keystroke ‘1’ then ‘0’.

You can keystroke more than one character:

tell application "System Events" to keystroke (return & return & "Now is the time for all good men")

To get keystroke to work you need: keystroke timeValue1 as text

Ok, I founf part of the problem. It works great like this:

  tell application "System Events"
	tell process "CronniX"
		set frontmost to true
		set x to value of text field 3 of tab group 1 of group 1 of sheet 1 of window 1
		set timeValue to x + 1
		
		repeat 5 times
			keystroke tab
			delay 0.5
		end repeat
		 
if x is less than 21
		tell application "System Events"
			tell process "CronniX"
				keystroke timeValue as text
			end tell
		end tell
		end if
		
	end tell
end tell

Ok, this is very odd, it does work. But, it will not work if the value of the text box is between 3 and 9. Everything else works. Anyone seen this before?

Don’t know why that specific strangeness, but x is text and 1 is a number, reverse them to get an automatic coercion to number: set timeValue to 1 + x → a number (AppleScript coerces the second to the first left to right when the types don’t match)

Jacques, this works great, but there is one problem. I need to click the “apply” button after this to save the cheange I made. This worked fine with the old script. But, the new one you gave me will not save the changes made. I cant figure it out, but I think it is related to the AXfocused thing.

DOES NOT SAVE

tell application "System Events" to tell process "CronniX"
	set frontmost to true
	tell text field 3 of tab group 1 of group 1 of sheet 1 of window 1
		set x to value
		set value of attribute "AXfocused" to true --set the focus to text field 3
		set timeValue to x + 1
		if x as integer < 21 then
			set value to timeValue as text
		end if
		set value of attribute "AXfocused" to false --set the focus to text field 3
	end tell
end tell

tell application "System Events"
	tell process "CronniX"
		set {x, y} to the position of button "Apply" of sheet 1 of window 1
		tell application "Extra Suites"
			ES move mouse {x + 15, y + 10}
			ES click mouse
		end tell
	end tell
end tell

DOES SAVE

 tell application "System Events" to tell process "CronniX"
	set frontmost to true
	set x to value of text field 3 of tab group 1 of group 1 of sheet 1 of window 1
	set timeValue to x + 1
	
	repeat 5 times
		keystroke tab
		delay 0.5
	end repeat
	if x is less than 21 then
		tell application "System Events"
			tell process "CronniX"
				keystroke timeValue as text
			end tell
		end tell
	end if
end tell


tell application "System Events"
	tell process "CronniX"
		set {x, y} to the position of button "Apply" of sheet 1 of window 1
		tell application "Extra Suites"
			ES move mouse {x + 15, y + 10}
			ES click mouse
		end tell
	end tell
end tell