Help on how to make the Stepper work

Help!

I am trying to make a simple test to create an hour stepper but I could not see to make it work. Can someone who knows stepper coding take a look on the codes below and enlighten me on what might be wrong or missing?

Thanks a lot. javascript:emoticon(‘:D’)

Here are the codes:

–The name of my main window is “timeWindow”

on launched theObject
set visible of window “timeWindow” to true
end launched
–=================
on action theObject
set theText to contents of theObject
tell window “timeWindow”
if theObject is equal to text field “hourDisplay” then
set contents of stepper “hourStepper” to theText as integer
set contents of text field “hourDisplay” to theText
end if
end tell
end action
–================
on clicked theObject
tell window “timeWindow”
if the name of theObject is stepper “hourStepper” then
set theValue to the contents of stepper “hourStepper” as integer
set contents of text field “hourDisplay” to theValue
end if
end tell
end clicked
–================
on awake from nib theObject
set visible of window “timeWindow” to true
end awake from nib
–=================
–==end of the script :smiley: :smiley:

I don’t see the value of either your ‘on launched’ or ‘awake from nib’ handlers. First, they’re doing the same thing, so one can be dispensed with. And second, they are both doing something that can be set in IB and not messed with in your script. Just check the ‘visible at launch time’ checkbox in the IB info pane for the ‘timeWindow’ window and it will automatically be visible at launch time.

Then, get rid of the ‘on action’ handler for the text field and replace it with an ‘on changed’ connection. This, along with the ‘changed’ handler below will keep track of any manual updates to the text field and update the stepper in real time. Replace the clicked handler with the one below, and make sure your stepper, text field and window are all connected to their respective handlers. I also used a property to contain the value of the current hour. This serves as the default hour value, and also remembers the current hour value until you set it again, even if the window is closed and re-opened. Make these changes or modify to suit your needs…and you should see some magic. 8)

property hourValue : 1

on clicked theObject --> Connect to the stepper
	if name of theObject is "hourStepper" then
		tell window "timeWindow"
			set theStepperValue to ((contents of stepper "hourStepper") as integer)
			set contents of text field "hourDisplay" to theStepperValue
			set hourValue to theStepperValue
		end tell
	end if
end clicked

on will open theObject --> Connect to the window
	if name of theObject is "timeWindow" then
		(* Set the values of the stepper/text field to your default value *)
		tell window "timeWindow"
			set contents of stepper "hourStepper" to (hourValue as integer)
			set contents of text field "hourDisplay" to (hourValue as integer)
		end tell
	end if
end will open

on changed theObject --> Connect to the text field
	if name of theObject is "hourDisplay" then
		try
			tell window "timeWindow"
				(* Save current stepper value to replant if error*)
				set theStepperValue to ((contents of stepper "hourStepper") as integer)

				(* Get the new stepper value *)
				set theNewStepperValue to (contents of text field "hourDisplay")

				(* Try to set the stepper and property values *)
				set contents of stepper "hourStepper" to (theNewStepperValue as integer)
				set hourValue to (theNewStepperValue as integer)
			end tell
		on error
			display dialog "Numbers only...Duh!"

			(* Set the text field and peoperty values back to the pre-error value *)
			tell window "timeWindow"
				set contents of text field "hourDisplay" to theStepperValue
				set hourValue to theStepperValue
			end tell
		end try
	end if
end changed

Also, you can’t reference an object like…

It’s name is not {stepper “myStepper”}… it’s just “myStepper”. You must reference it in one of the following ways…

if name of theObject is "myStepper"
-- OR --
if theObject is stepper "myStepper" of window "theWindow"
-- OR --
set theObjectName to (name of theObject)
if theObjectName is "myStepper" then

Cheers…
j

thanks a lot j for the suggestion and advise to get rid of unnecessary coding; just leave things to the IB. i never thought to do it that way. that would certainly keep things cleaner and leaner for sure.

will try your suggested codes and will let you know.

archseed

dear j,

i pasted your entire code in my small stepper project, making sure that all window names, applescript names, etc remain the same.

javascript:emoticon(‘:oops:’) sorry, but it still doesn’t work. i don’t know where the problem lies. just to be sure i got things right from your reply let me tell you what i did.

here’s the rundown:
–pasted your entire code, erasing mine in the process… the nib, on launch also gone.

Here are the IB settings that I had when I compiled the test project. Here are the various IB settings.

timeWindow: window setting “will open” is set; script is checked and linked to “will open” handler

stepper “hourStepper”: action/clicked activated; linked to the script

text field "hourDisplay: action/action and editing/changed set; linked to the script also

Other IB settings for the timeWindow in case there’s something afoot are as follows:

The following check buttons are enabled: one shot, deferred, visible at launch, textured. There should not be any problem with “textured” but I am not familiar with “one shot” and “deferred”. Also, in the backing, “buffered” is checked by default.

Could you kindly try your script using the settings that I have given above and see if it works. There may be some settings that are not necessay or I have set something else that’s not needed at all.

Thanks.

PS. I saw some suggestion from others to physically link the stepper to the text field manually by doing a “control-drag” between the two items in the window and then selecting the option to take only the integer values. As suggested, this has to be mutually done between items. After doing it, still no event could be seen.

BTW, the text field “hourDisplay” does not even register the default setting of 1 as you gave. So, the problem must be at the initialization of the text field and the stepper to the default hourValue of 1 or even before that. This is where I am looking at. What do you think?

Thanks for your patience. It’s a virtue.

archseed

The only thing I see that’s not mentioned is pasting…

property hourValue : 1

…into the top of your script. Otherwise everything else looks good and none of the window settings alarms me. Make sure once more that your connections are good and that your names all exist and are all correct. I didn’t see any reason to make any direct cocoa connections, as applescript is already checking for integers in the code I provided (as long as you insert an integer in the property above). If you can’t figure it, send me your email address in a PM and I’ll send you my test project. Or you can send me your project file and I’ll take a look.

Good luck…
j

dear j,

thanks again for the quick reply.

i had the property declaration for the “hourValue” as you suggested. nothing missing in the script and the connections but still it would not work.

when i started from scratch, built a new project and recompiled it based on your suggestions, everything worked perfectly. now, it does give me the ‘magic’ that you told me about.

i must have messed up my project real bad for your suggestion to work. sorry but geez! appreciate your advise very much.

more power to you!

archseed :stuck_out_tongue: