Scriptable Timer / Stopwatch

Hello.

I don’t know if this helps at this stage, but I mocked up a state diagram for you.

Imagine the different dialogs between the states, or notifications for that matter. Maybe one scheme that would help you, is that you also keep an internal state, and then you “query the state”, and do the action directly, or you use a "clock object that I do, and simply passes the button over to it, so that you have all the logic in the clock object. That is at least ways to do it, that should be fairly simple, yet provide a good overview of what happens at all times.

I have a simpler model, than you with respect to “split time”: The current state is stopped, when the user don’t cancel a dialog, that way, I always stop the correct “timer”, the other was stopped before the last one got activate. This is however not the same as “split seconds” on a stopwatch, where you take an “intermediary time”.

By the way, you should really google Patek Phillipe Grandmaster video. :slight_smile:

Hi McUsr,

I don’t think like you guys, but go step by step. That may be why I’m so slow. Different learning styles. But, thanks anyway. I got up to this point now in the step by step approach:

script AppDelegate
    
    -- IBOutlets
    property theWindow : missing value
    
    property parent : class "NSObject"
    property myNotification : missing value
    property notificationCount: 0
    property splitPauseSetting: "split"
    property startStopState: "Start"
    
    on applicationWillFinishLaunching_(aNotification)
        -- initialize
        return
    end applicationWillFinishLaunching_
    
    -- reopen handler
    on applicationShouldHandleReopen_hasVisibleWindows_(theApplication, aFlag)
        my applicationDidFinishLaunching_(missing value)
        return NO
    end applicationShouldHandleReopen_hasVisibleWindows_
    
    -- main
    on applicationDidFinishLaunching_(aNotification)
        -- set the delegate to script
        current application's NSUserNotificationCenter's defaultUserNotificationCenter's setDelegate_(me)
        -- get current date and target date
        set sentDate to current application's NSDate's |date|()
        set sentDateAsString to my formatDate_(sentDate,1,2)
        set numSeconds to 5
        set targetDate to current application's NSDate's dateWithTimeInterval_sinceDate_(numSeconds,sentDate)
        -- make user info NSDictionary
        set notificationCount to notificationCount + 1
        set nsDict to current application's NSDictionary's dictionaryWithObjectsAndKeys_(notificationCount, "notificationIndex", sentDateAsString, "sentDate", "value3", "key3", missing value)
        -- send notification to NSUserNotificationCenter
        my sendNotification_("Stopwatch2","Click here to toggle split/pause.","Current setting: " & splitPauseSetting,startStopState,"Cancel",targetDate,"Boing",nsDict)
        say "Notification sent."
        return
    end applciationDidFinishLaunching_
    --
    
    -- format NSDate to string using styles
    -- 0 = none, 1 = short, 2 = med, 3 = long, 4 = full
    on formatDate_(aNSDate, aDateStyle, aTimeStyle)
        set myFormatter to current application's NSDateFormatter's alloc()'s init()
        myFormatter's setDateStyle_(aDateStyle)
        myFormatter's setTimeStyle_(aTimeStyle)
        set formattedDate to myFormatter's stringFromDate_(aNSDate)
        return formattedDate
    end formatDate_
    
    -- method for sending a notification
    on sendNotification_(aTitle, aSubtitle, aMessage, aActionButtonTitle, aOtherButtonTitle, aDeliveryDate, aSound, aDict)
        -- make the notification
        set myNotification to current application's NSUserNotification's alloc()'s init()
        set myNotification's title to aTitle
        set myNotification's subtitle to aSubtitle
        set myNotification's informativeText to aMessage
        set myNotification's actionButtonTitle to aActionButtonTitle
        set myNotification's otherButtonTitle to aOtherButtonTitle
        set myNotification's deliveryDate to aDeliveryDate
        set myNotification's soundName to aSound
        set myNotification's userInfo to aDict
        --set myNotification's hasReplyButton to true
        -- schedule the notification
        current application's NSUserNotificationCenter's defaultUserNotificationCenter's scheduleNotification_(myNotification)
        return
    end sendNotification_
    
    -- delegate instance methods
    -- force presentation for when application process is frontmost
    on userNotificationCenter_shouldPresentNotification_(aCenter, aNotification)
        return yes
    end userNotificationCenter_shouldPresentNotification_
    
    -- deliver
    on userNotificationCenter_didDeliverNotification_(aCenter, aNotification)
        say "Notification Delivered"
        return
    end userNotificationCenter_didDeliverNotification_
    
    -- user activation
    on userNotificationCenter_didActivateNotification_(aCenter, aNotification)
        say "Notification Activated"
        -- 0    none (does not work)
        -- 1    contents clicked
        -- 2    action button clicked
        -- 3    additional button clicked
        -- Gets user entry from notification's text field
        set userActivationType to (aNotification's activationType) as integer
        -- note: eliminated option 0 because there was no way to get reply besides monitoring notification window
        -- use action button 2 to get reply for close or quit
        if userActivationType is 1 then
            say "contents clicked"
            my contentsClicked_(aNotification)
            else if userActivationType is 2 then
            say "action button clicked"
            my actionButtonClicked_(aNotification)
            else if userActivationType is 3 then -- userActivationType is 3
            say "user activation type is 3"
            my replyButtonClicked_(aNotification)
            else -- if userActivationType is 4 then -- never happens as it is never 0 or 4
            beep 3
        end if
        return userActivationType
    end userNotificationCenter_didActivateNotification_
    
    -- new plan for close button:
    --  keep the application open for 5 minutes
    --  after 5 minutes, check if there are scheduled notificaitons and delivered notifications
    --  if no then quit
    --  otherwise, there are either scheduled notificaitons or delivered notifications
    
    -- user subroutines
    
    -- quick toggle
    -- need to change to boolean toggle
    on contentsClicked_(aNotification)
        -- toggle split/pause variable
        if startStopState is "Start" then
            if splitPauseSetting is "split" then
                set splitPauseSetting to "pause"
            else
                set splitPauseSetting to "split"
            end if
        else -- startStopState is "Stop"
            if splitPauseSetting is "split" then
                say "splitting"
            else
                say "pausing"
            end if
        end if
        -- check state of start/stop button
        -- if state is start, then remove this notification and resend with new split/pause setting
        
        return
    end contentsClicked_
    
    -- gets user reply in text field
    -- not used in stopwatch
    -- later used for egg timer
    on replyButtonClicked_(aNotification)
        -- set userTextEntry to (aNotification's response) as text
        set userTextEntry to (aNotification's response()'s |string|()) as text
        return true
    end replyButtonClicked_
    
    -- action button state is "stop" or "start"
    on actionButtonClicked_(aNotification)
        -- delete the notification and send a new one (or some other action)
        if startStopState is "Start" then
            -- first toggle startStopeState
            set startStopState to "Stop"
        else -- user chose stop
            say "user chose stop"
        -- delete notification from notification center
        (*
        current application's NSUserNotificationCenter's defaultUserNotificationCenter's removeDeliveredNotification_(aNotification)
         *)
        end if
        -- send a new notification
        my applicationDidFinishLaunching_(missing value)
        return
    end actionButtonClicked_
    
    --
    
    -- quit app if user presses close button
    on applicationShouldTerminateAfterLastWindowClosed_()
        return true
    end applicationShouldTerminateAfterLastWindowClosed_
    
    -- quit
    on applicationShouldTerminate_(sender)
        -- Insert code here to do any housekeeping before your application quits
        return current application's NSTerminateNow
    end applicationShouldTerminate_

end script

Iv’e got the action button to change to Stop! That’s a big hurdle for me.

Once in a while I do have these streaks of genius though. I think it happened one day when I was young and riding skateboard on these cliffs. I was about 17 years old and riding this big hill. Then all of a sudden the wheels locked. My body was horizontal to the ground and I just went straight down and hit the back of my head. Think I knocked out for a few minutes and when I woke my friend was looking me in the face. That was just one of the many times I almost died.

But, anyway, what were we talking about? :smiley:

Glad you made it.

We all do work through stuff step by step I guess, when entering unknown territory, (it all depends on how unknown it is, personally I find it best sometimes, to make call after call work, and then also add the steps necessary as I go along when I learn the technology, (most people do that I believe), because merely reading up on a subject, if you haven’t got anything that resembles it quite a bit internalized up front, then the sound practice is to both try to make it work, and read up at the same time. :slight_smile:

But later on, we structure the stuff, so that the code becomes more maintainble, and graspable, the nice sideffect, is that it becomes easy to intergrate with the rest of our goal. On superb technique, is to see to that everything is encapsulated, and that the different objects, just shares data through methods. (It is not set in stone.)

Restructuring code becomes easier with practice. And then you in the end end up with programs that are both easy to read, and to maintain/develop further.

I am not overbearing in anyway here, I am just telling you that you don’t deviate that much, and that lots of code are originated the same way, it is what happens afterwards that is the big difference. And sometimes, it don’t happen too, (the restructirng), especially in AppleScript because it is really slow, and if you are to iterate over a lot of items, then a procedural (step-by-step) approach is better.

It is very late here. Tomorrow. :slight_smile:

Hi McUser,

That’s because I’m anal retentive. :smiley: Any change and I gotta go all over again step by step. It’s a pain, but sometimes you find changes and you have to rewrite some things all over again to get the max. Almost from scratch sometimes. But, as I said everybody has their own way of doing things.

When I’m done I can sleep. Darn, haven’t slept for 2 days. The mind just keeps going.

Edited: i.e. haven’t slept good for 2 days.

Edited: btw: it’s like deja vu all over again!

Have a nice day,
kel

Hi McUsr,

I’m so confused! :smiley: Didn’t follow my own advice to start from scratch and go step by step. Instead, I tried to just modify the template and that what is causing the confusion. Need to start all over again. Although, I do have the algorithm in my mind.

Later,
kel

This is a good starting template for me! :slight_smile:

script AppDelegate
	property parent : class "NSObject"
	
	-- IBOutlets
	property theWindow : missing value
	
	on applicationWillFinishLaunching_(aNotification)
		-- Insert code here to initialize your application before any files are opened 
	end applicationWillFinishLaunching_
    
    -- reopen handler
    on applicationShouldHandleReopen_hasVisibleWindows_(theApplication, aFlag)
        my applicationDidFinishLaunching_(missing value)
        return NO
    end applicationShouldHandleReopen_hasVisibleWindows_
    
    -- main
    on applicationDidFinishLaunching_(aNotification)
        -- set the delegate to script
        current application's NSUserNotificationCenter's defaultUserNotificationCenter's setDelegate_(me)
        return
    end applciationDidFinishLaunching_
	
    -- quit app if user presses close button
    on applicationShouldTerminateAfterLastWindowClosed_()
        return true
    end applicationShouldTerminateAfterLastWindowClosed_
    
	on applicationShouldTerminate_(sender)
		-- Insert code here to do any housekeeping before your application quits 
		return current application's NSTerminateNow
	end applicationShouldTerminate_
	
end script

Bare bones with the main subroutines. What I think was mixing me up man was the variables and all the extraneous stuff. Not sure.

Later,
kel

Ok, first step finished. Created the first notification:

script AppDelegate
	property parent : class "NSObject"
	
	-- IBOutlets
	property theWindow : missing value
    
    -- first step
    -- create the first notification
    property notifTitle: "Notification4"
    property notifSubtitle: "Click here to select split/pause."
    property notifInfoText: "Current mode: Split"
    property notifActionButtonTitle: "Start"
    property notifOtherButtonTitle: "Cancel"
    property notifSound: "Boing"
    -- now every text field of the notification is filled
    -- next is boolean variables, but check out how it looks in a notification
    -- send the notification after finished launching
    
	on applicationWillFinishLaunching_(aNotification)
		-- Insert code here to initialize your application before any files are opened 
	end applicationWillFinishLaunching_
    
    -- reopen handler
    on applicationShouldHandleReopen_hasVisibleWindows_(theApplication, aFlag)
        my applicationDidFinishLaunching_(missing value)
        return NO
    end applicationShouldHandleReopen_hasVisibleWindows_
    
    -- main
    on applicationDidFinishLaunching_(aNotification)
        -- set the delegate to script
        current application's NSUserNotificationCenter's defaultUserNotificationCenter's setDelegate_(me)
        my sendNotification_(notifTitle,notifSubtitle,notifInfoText,notifActionButtonTitle,notifOtherButtonTitle,notifSound)
        return
    end applciationDidFinishLaunching_

    -- method for sending a notification
    on sendNotification_(aTitle, aSubtitle, aInfoText, aActionButtonTitle, aOtherButtonTitle, aSound)
        -- make the notification
        set myNotification to current application's NSUserNotification's alloc()'s init()
        set myNotification's title to aTitle
        set myNotification's subtitle to aSubtitle
        set myNotification's informativeText to aInfoText
        set myNotification's actionButtonTitle to aActionButtonTitle
        set myNotification's otherButtonTitle to aOtherButtonTitle
        set myNotification's soundName to aSound
        -- schedule the notification
        current application's NSUserNotificationCenter's defaultUserNotificationCenter's scheduleNotification_(myNotification)
        return
    end sendNotification_
	
    -- quit app if user presses close button
    on applicationShouldTerminateAfterLastWindowClosed_()
        return true
    end applicationShouldTerminateAfterLastWindowClosed_
    
	on applicationShouldTerminate_(sender)
		-- Insert code here to do any housekeeping before your application quits 
		return current application's NSTerminateNow
	end applicationShouldTerminate_
	
end script

We can crunch it up later. Don’t think just do. :smiley:

Edited: remember that you need to set the Notifications preferences in System Preferences to Alerts instead of Banners so that you can see the buttons.

Later,
kel

Hi McUsr,

Added the baroness Delegate instance methods:
[applescriptscript AppDelegate
property parent : class “NSObject”

-- IBOutlets
property theWindow : missing value

-- first step
-- create the first notification
property notifTitle: "Notification4"
property notifSubtitle: "Click here to select split/pause."
property notifInfoText: "Current mode: Split"
property notifActionButtonTitle: "Start"
property notifOtherButtonTitle: "Cancel"
property notifSound: "Boing"
-- now every text field of the notification is filled
-- next is boolean variables, but check out how it looks in a notification
-- send the notification after finished launching

-- step 2
-- user selects start button if user wants split and to start timing
-- else user selects content area to toggle split to pause

-- see Delegate instance methods at bottom

on applicationWillFinishLaunching_(aNotification)
	-- Insert code here to initialize your application before any files are opened 
end applicationWillFinishLaunching_

-- reopen handler
on applicationShouldHandleReopen_hasVisibleWindows_(theApplication, aFlag)
    my applicationDidFinishLaunching_(missing value)
    return NO
end applicationShouldHandleReopen_hasVisibleWindows_

-- main
on applicationDidFinishLaunching_(aNotification)
    -- set the delegate to script
    current application's NSUserNotificationCenter's defaultUserNotificationCenter's setDelegate_(me)
    my sendNotification_(notifTitle,notifSubtitle,notifInfoText,notifActionButtonTitle,notifOtherButtonTitle,notifSound)
    return
end applciationDidFinishLaunching_

-- method for sending a notification
on sendNotification_(aTitle, aSubtitle, aInfoText, aActionButtonTitle, aOtherButtonTitle, aSound)
    -- make the notification
    set myNotification to current application's NSUserNotification's alloc()'s init()
    set myNotification's title to aTitle
    set myNotification's subtitle to aSubtitle
    set myNotification's informativeText to aInfoText
    set myNotification's actionButtonTitle to aActionButtonTitle
    set myNotification's otherButtonTitle to aOtherButtonTitle
    set myNotification's soundName to aSound
    -- schedule the notification
    current application's NSUserNotificationCenter's defaultUserNotificationCenter's scheduleNotification_(myNotification)
    return
end sendNotification_

-- DELEGATE INSTANCE METHODS

-- Instruction to force presentation for when application process is frontmost
on userNotificationCenter_shouldPresentNotification_(aCenter, aNotification)
    return yes
end userNotificationCenter_shouldPresentNotification_

-- delivered
on userNotificationCenter_didDeliverNotification_(aCenter, aNotification)
    return
end userNotificationCenter_didDeliverNotification_

-- activated in content field or action button
on userNotificationCenter_didActivateNotification_(aCenter, aNotification)
    return
end userNotificationCenter_didActivateNotification_

-- user subroutines

on contentsClicked_(aNotification)
    return
end contentsClicked_

on actionButtonClicked_(aNotification)
    return
end actionButtonClicked_

-- end user subroutines

-- END DELEGATE INSTANCE METHODS

-- quit app if user presses close button
on applicationShouldTerminateAfterLastWindowClosed_()
    return true
end applicationShouldTerminateAfterLastWindowClosed_

on applicationShouldTerminate_(sender)
	-- Insert code here to do any housekeeping before your application quits 
	return current application's NSTerminateNow
end applicationShouldTerminate_

end script


Forgot to delete some of the old comments, but doing that now. Now we're getting somewhere.

Edited: deleted the one comment I missed to delete.

Edited: now the hard part comes with the logic! :D

Edited: hoping that it's easier now that it's more organized.

Edited: man, maybe I should do it in pencil and paper (the flowchart).

Later,
kel

First pulled the user subroutines out of the delegate methods because it didn’t belong there:

script AppDelegate
	property parent : class "NSObject"
	
	-- IBOutlets
	property theWindow : missing value
    
    -- first step
    -- create the first notification
    property notifTitle: "Notification4"
    property notifSubtitle: "Click here to select split/pause."
    property notifInfoText: "Current mode: Split"
    property notifActionButtonTitle: "Start"
    property notifOtherButtonTitle: "Cancel"
    property notifSound: "Boing"
    -- now every text field of the notification is filled
    -- next is boolean variables, but check out how it looks in a notification
    -- send the notification after finished launching
    
    -- step 2
    -- user selects start button if user wants split and to start timing
    -- else user selects content area to toggle split to pause

    -- see Delegate instance methods at bottom
    
	on applicationWillFinishLaunching_(aNotification)
		-- Insert code here to initialize your application before any files are opened 
	end applicationWillFinishLaunching_
    
    -- reopen handler
    on applicationShouldHandleReopen_hasVisibleWindows_(theApplication, aFlag)
        my applicationDidFinishLaunching_(missing value)
        return NO
    end applicationShouldHandleReopen_hasVisibleWindows_
    
    -- main
    on applicationDidFinishLaunching_(aNotification)
        -- set the delegate to script
        current application's NSUserNotificationCenter's defaultUserNotificationCenter's setDelegate_(me)
        my sendNotification_(notifTitle,notifSubtitle,notifInfoText,notifActionButtonTitle,notifOtherButtonTitle,notifSound)
        return
    end applciationDidFinishLaunching_

    -- method for sending a notification
    on sendNotification_(aTitle, aSubtitle, aInfoText, aActionButtonTitle, aOtherButtonTitle, aSound)
        -- make the notification
        set myNotification to current application's NSUserNotification's alloc()'s init()
        set myNotification's title to aTitle
        set myNotification's subtitle to aSubtitle
        set myNotification's informativeText to aInfoText
        set myNotification's actionButtonTitle to aActionButtonTitle
        set myNotification's otherButtonTitle to aOtherButtonTitle
        set myNotification's soundName to aSound
        -- schedule the notification
        current application's NSUserNotificationCenter's defaultUserNotificationCenter's scheduleNotification_(myNotification)
        return
    end sendNotification_
    
    -- DELEGATE INSTANCE METHODS
    
    -- Instruction to force presentation for when application process is frontmost
    on userNotificationCenter_shouldPresentNotification_(aCenter, aNotification)
        return yes
    end userNotificationCenter_shouldPresentNotification_
    
    -- delivered
    on userNotificationCenter_didDeliverNotification_(aCenter, aNotification)
        return
    end userNotificationCenter_didDeliverNotification_
    
    -- activated in content field or action button
    on userNotificationCenter_didActivateNotification_(aCenter, aNotification)
        return
    end userNotificationCenter_didActivateNotification_
    
    -- END DELEGATE INSTANCE METHODS
    
    -- user subroutines
    
    on contentsClicked_(aNotification)
        return
    end contentsClicked_
    
    on actionButtonClicked_(aNotification)
        return
    end actionButtonClicked_
    
    -- end user subroutines
	
    -- quit app if user presses close button
    on applicationShouldTerminateAfterLastWindowClosed_()
        return true
    end applicationShouldTerminateAfterLastWindowClosed_
    
	on applicationShouldTerminate_(sender)
		-- Insert code here to do any housekeeping before your application quits 
		return current application's NSTerminateNow
	end applicationShouldTerminate_
	
end script

Hello kel.

That looks very comprehensive to me. :slight_smile:

Have a nice evening.

Hi McUsr,

Yes I had to start al over again. What I’ve found is that in anything that is complicated in logic I need to start from a base way of thinking. Simple stuff. Otherwise, I get into trouble mixing complicated with complicated and end up lost in space.

Btw, I do n’t know if I’ve mentioned this, but i did see the last lunar eclipse and it was beautiful red. There was just a glimpse of it when the clouds separated. I was thinking about this because just saw the documentary on tv.

Step by step inch by inch, getting there.

Have a good one,
kel

Hi McUsr,

Think it’s working now, but still need more testing and cleaning up. Also, need to reinitialize for a second run. Not sure where the best place is to do that. Still thinking about somethings.

script AppDelegate
	property parent : class "NSObject"
	
	-- IBOutlets
	property theWindow : missing value
    
    -- first step
    -- create the first notification
    property notifTitle: "Notification4"
    property notifSubtitle: "Click here to select split/pause."
    property notifInfoText: "Current mode: "
    property notifActionButtonTitle: "Start"
    property notifOtherButtonTitle: "Cancel"
    property notifSound: "Single Click"
    property splitPauseMode: "Split"
    property startDate: missing value
    property totalTime: 0
    property splitTimes: {}
    -- now every text field of the notification is filled
    -- next is boolean variables, but check out how it looks in a notification
    -- send the notification after finished launching
    
    -- step 2
    -- user selects start button if user wants split and to start timing
    -- else user selects content area to toggle split to pause

    -- see Delegate instance methods at bottom
    
	on applicationWillFinishLaunching_(aNotification)
		-- Insert code here to initialize your application before any files are opened 
	end applicationWillFinishLaunching_
    
    -- reopen handler
    on applicationShouldHandleReopen_hasVisibleWindows_(theApplication, aFlag)
        -- initialize properties
        -- not sure if I should do it here or after user presses stop button
        my applicationDidFinishLaunching_(missing value)
        return NO
    end applicationShouldHandleReopen_hasVisibleWindows_
    
    -- main
    on applicationDidFinishLaunching_(aNotification)
        -- set the delegate to script
        current application's NSUserNotificationCenter's defaultUserNotificationCenter's setDelegate_(me)
        set notifInfoTextMode to notifInfoText & splitPauseMode
        my sendNotification_(notifTitle,notifSubtitle,notifInfoTextMode,notifActionButtonTitle,notifOtherButtonTitle,notifSound)
        return
    end applciationDidFinishLaunching_

    -- method for sending a notification
    on sendNotification_(aTitle, aSubtitle, aInfoText, aActionButtonTitle, aOtherButtonTitle, aSound)
        -- make the notification
        set myNotification to current application's NSUserNotification's alloc()'s init()
        set myNotification's title to aTitle
        set myNotification's subtitle to aSubtitle
        set myNotification's informativeText to aInfoText
        set myNotification's actionButtonTitle to aActionButtonTitle
        set myNotification's otherButtonTitle to aOtherButtonTitle
        set myNotification's soundName to aSound
        -- schedule the notification
        current application's NSUserNotificationCenter's defaultUserNotificationCenter's scheduleNotification_(myNotification)
        return
    end sendNotification_
    
    -- DELEGATE INSTANCE METHODS
    
    -- Instruction to force presentation for when application process is frontmost
    on userNotificationCenter_shouldPresentNotification_(aCenter, aNotification)
        return yes
    end userNotificationCenter_shouldPresentNotification_
    
    -- delivered
    on userNotificationCenter_didDeliverNotification_(aCenter, aNotification)
        return
    end userNotificationCenter_didDeliverNotification_
    
    -- activated in content field or action button
    on userNotificationCenter_didActivateNotification_(aCenter, aNotification)
        set userActivationType to (aNotification's activationType) as integer
        if userActivationType is 1 then
            say "Contents clicked"
            my contentsClicked_(aNotification)
        else  if userActivationType is 2 then
            say "action button clicked"
            my actionButtonClicked_(aNotification)
        else
            say "none"
        end if
        return
    end userNotificationCenter_didActivateNotification_
    
    -- END DELEGATE INSTANCE METHODS
    
    -- user subroutines
    
    on contentsClicked_(aNotification)
        if notifActionButtonTitle is "Start" then
            -- toggle split/pause
            if splitPauseMode is "Split" then
                set splitPauseMode to "Pause"
            else
                set splitPauseMode to "Split"
            end if
            -- remove notification
            current application's NSUserNotificationCenter's defaultUserNotificationCenter's removeDeliveredNotification_(aNotification)
            -- send new notification
            my applicationDidFinishLaunching_(missing value)
        else if notifActionButtonTitle is "Stop"
            -- log split time
            set curDate to (current date)
            set timeDiff to curDate - startDate
            set end of splitTimes to totalTime + timeDiff
            log splitTimes
            -- set subtitle
            set notifSubtitle to "Split time: " & (totalTime + timeDiff) & " secs"
            -- split or pause
            if splitPauseMode is "Pause" then
                -- add current time to total time
                set totalTime to totalTime + timeDiff
                -- set action button title to restart
                set notifActionButtonTitle to "Restart"
            end if
            -- remove notification
            current application's NSUserNotificationCenter's defaultUserNotificationCenter's removeDeliveredNotification_(aNotification)
            -- send new notification
            my applicationDidFinishLaunching_(missing value)
        else -- action button title is "Restart"
           -- do nothing
        end if
        beep 1
        return
    end contentsClicked_
    
    on actionButtonClicked_(aNotification)
        if notifActionButtonTitle is "Start" then -- start timer
            -- change action button title
            set notifActionButtonTitle to "Stop"
            -- start or restart timer
            set startDate to (current date)
            -- change subtitle
            set notifSubtitle to "Click here to " & splitPauseMode
            -- send new notification
            my applicationDidFinishLaunching_(missing value)
        else if notifActionButtonTitle is "Stop" then
            -- calculate total time and inform user
            set curDate to (current date)
            set timeDiff to curDate - startDate
            set totalTime to totalTime + timeDiff
            log splitTimes
            log totalTime
            -- reset initial properties for new notification here
            
        else -- action button title is "Restart"
            -- reset start date
            set startDate to (current date)
            -- change action button to "Stop"
            set notifActionButtonTitle to "Stop"
            -- change subtitle
            set notifSubtitle to "Click here to " & splitPauseMode
            -- send new notification
            my applicationDidFinishLaunching_(missing value)
        end if
        beep 2
        return
    end actionButtonClicked_
    
    -- end user subroutines
	
    -- quit app if user presses close button
    on applicationShouldTerminateAfterLastWindowClosed_()
        return true
    end applicationShouldTerminateAfterLastWindowClosed_
    
	on applicationShouldTerminate_(sender)
		-- Insert code here to do any housekeeping before your application quits 
		return current application's NSTerminateNow
	end applicationShouldTerminate_
	
end script

Almost there. :slight_smile:

Edited: using the quote here to see if formatting can be saved:

Have a good day,
kel

Hello kel.

It starts to get meat on it. :slight_smile: I think it is only two placess you can reinitialize, and that is on the stop button when you have finally stopped, or on the start button, when you restart it. Now you can see the benefits, of using the model-view controller pattern, as it allows you to have most of the controller logic in one place, and not spread around the buttons, though, you mat easily get away with just programming the buttons. :slight_smile: Here is an interesting paper, that gives you some perspective on the model view controller pattern: The DCI Architecture: A New Vision of Object-Oriented Programming, should you wish to read it.

Have a nice day. :slight_smile:

Hi McUsr,

I’ve read that before from your link although I can’t remember what it was about. I’ll look through it again. What I remember was that it says something like do your calculations somewhere or something. :stuck_out_tongue: I was thinking about breaking the subroutines down, but was trying to save time. Not sure if it would matter that much.

The program seems to ok speed wise although still need to optimize it. Just wanted to get the logic down first like an algorithm. Just got back and was planning to try and see what I could do to speed it up among the other things. Probably using the text in the logic is slowing it down. Ultimately, what I wanted to do was convert to AppleScript instead of the Xcode app for learning purposes mainly.

Edited: also, taking out the 'say’s would eliminate the 1 or 2 second delays. But that’s just for the testing.

Thanks and have a nice day,
kel

Hi McUsr,

Deep down in my soul I’m thinking that the initial variables should reinitialize by themselves and thinking how to do that. Need to think about that later. Maybe there is some subroutine that sets the properties back to their initial values. I have been searching the docs and couldn’t find it.

Anyway, it seems that it is working on the first run so that’s good.

Getting there,
kel

Hello kel.

Sorry for the belated answer, I have gotten fibre with complications, and now they have removed the complications, so, now I am online again!

I really mean that you should make a script object, or an object, with handlers to start and stop the watch, and then just call those methods from your notfications, since that will provide for cleaner code: When you read the “clock object”, you have all the functionality in front of you, at once, so it is easily to grasp what it does, and what not. And then when you read your notification code, then you can really focus on the notifications, that was what I meant above I guess.

I am glad it progresses for you. :slight_smile:

Edit

You don’t need to implement the stopwatch with a separate object for the clock, but it is a great boon, to actually, just view the functionality of it as a whole during design, and test it, either, by rubberducking, or implementing it as an object, to see that those parts works as a whole.

Hi Mcusr,

I did use separate handlers to start and stop the watch in the beginning. The problem was that in the user interface the Start text for the button didn’t look right if it was coming from a pause. What I had to do was switch Start to Restart. The hard part was thinking about the changing the text in the dialog and buttons. The limited amount of buttons made it like playing a game where you have to move three pieces. You can move one to two and two to three, but you can’t move three to one all at the same time. :slight_smile: You can move only one at a time. Don’t know if you know If I’m explaining my thoughts right, but anyway still looking into it. It was fun! :smiley:

Edited: looking again at the script, I would have the hardest time trying to simplify it! :lol: Yes, it is very complicated on second thought. Who would have thought that such a simple thing would be so complicated. If someone can do it, I would love to see it. Always open to examples.

Have a good day,
kel

Hi McUsr,

The reopen handler is there mainly because I was thinking what if the user clicks the icon in the dock. Still thinking about a solution for this. The user might have accidentally clicked it also. That’s why that handler is still there. Just in case you were wondering why. That’s one of the things I was thinking about.

Edited: also, the reopen handler might be used to start a new timer. It could be a new instance. Still thinking about that for memories sake.

Have a good day,
kel

Just thought of this. If the user creates new instances of the timer, then we need to keep track of how many and limit it. It’s turning into a monster! :smiley: Only in my mind. :slight_smile: