Voice event count - help

Hello,

I am trying to voice the count of events on today’s date but I cannot get it to work at all. Can someone help me figure out what I am doing wrong?

on isVoiceOverRunning()
    set isRunning to false
    tell application "System Events"
        set isRunning to (name of processes) contains "VoiceOver"
    end tell
    return isRunning
end isVoiceOverRunning

on isVoiceOverRUnningWithAppleScript()
    if isVoiceOverRunning() then
        set isRunningWithAppleScript to true
        
        -- is AppleScript enabled on VoiceOver -- 
        tell application "VoiceOver"
            try
                set x to bounds of vo cursor
            on error
                set isRunningWithAppleScript to false
            end try
        end tell
        return isRunningWithAppleScript
    end if
    return false
end isVoiceOverRUnningWithAppleScript

on calendarEvents()
    tell application "Calendar"
        set my_Calendars to every calendar
        set beginningDate to (current date)
        set hours of beginningDate to 8
        set minutes of beginningDate to 0
        set seconds of beginningDate to 0
        set endDate to beginningDate + (8 * hours)
        set eventCount to 0
        repeat with a_calendar in my_Calendars
            tell a_calendar to set my_Events to (every event whose (start date is greater than or equal to beginningDate) and (start date is less than or equal to endDate))
            set eventCount to eventCount + (count of my_Events)
        end repeat
    end tell
end calendarEvents

set eventString to " "
set eventCount to calendarEvents()

if eventCount is less than 10 then
    set eventString to eventCount & " events today" as string
else
    set eventString to eventCount & " events today. Good Luck" as string
end if

if isVoiceOverRUnningWithAppleScript() then
    tell application "VoiceOver"
        output eventString
    end tell
else
    say eventString
    delay 2
end if

([applescript] and [/applescript] posting tags added by NG.)

My understanding is that it’s the code dedicated to VoiceOver which fails.

I don’t use it and the script tells correctly the passed string.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 14 avril 2020 22:57:30

I would not want to delve into redundant code to search for errors. Therefore, I cleaned it and tested the improved version.

For the output command, I provide test text. Especially so that it can be seen that the script is working correctly.

Try the script in 4 states: VoiceOver on/off + AppleScript enabled/disabled. (You do this states using VoiceOver item on the panel Accessibility of System Preferences). After checking the script, replace the test text back with the variable eventString, as in the old script:


set eventString to " "
tell application "Calendar"
	set beginningDate to (current date)
	tell beginningDate to set {its hours, its minutes, its seconds} to {8, 0, 0}
	set endDate to beginningDate + (8 * hours)
	set eventCount to 0
	repeat with a_calendar in (calendars)
		tell a_calendar
			set my_Events to (events whose (start date ≥ beginningDate) and (start date ≤ endDate))
		end tell
		set eventCount to eventCount + (count my_Events)
	end repeat
end tell

if eventCount is < 10 then
	set eventString to eventCount & " events today" as string
else
	set eventString to eventCount & " events today. Good Luck" as string
end if

tell application "System Events" to set isRunning to (name of processes) contains "VoiceOver"
if isRunning then -- VoiceOver is ON
	try
		tell application "VoiceOver" 
			set x to bounds of vo cursor
			output "This is one Test text" -- (VoiceOver is ON) & (AppleScript is ENABLED)
		end tell
	on error
		say eventString  -- (VoiceOver is ON) & (AppleScript is DISABLED)
	end try
else
	say eventString -- (VoiceOver is OFF) & (AppleScript is ENABLED or DISABLED)
end if

This is working correctly. Thank you! One question, it is only reading one of my calendar’s events. How do I get it to count ALL my calendars?

  1. I guess I forgot to put its keyword before the word events.
  2. It is better to put code lines setting the dates before the tell block, to increase the speed of the script and improve the syntax.
  3. Sure you have attached different colours to your different calendars to see which calendar is owner of which event?

set eventString to " "
set beginningDate to (current date)
tell beginningDate to set {its hours, its minutes, its seconds} to {8, 0, 0}
set endDate to beginningDate + (8 * hours)

tell application "Calendar"
	set eventCount to 0
	repeat with a_calendar in (calendars)
		tell a_calendar
			set my_Events to (its events whose (start date ≥ beginningDate) and (start date ≤ endDate))
		end tell
		set eventCount to eventCount + (count my_Events)
	end repeat
end tell

if eventCount < 10 then
	set eventString to eventCount & " events today" as string
else
	set eventString to eventCount & " events today. Good Luck" as string
end if

tell application "System Events" to set isRunning to (name of processes) contains "VoiceOver"
if isRunning then -- VoiceOver is ON
	try
		tell application "VoiceOver"
			set x to bounds of vo cursor
			output eventString -- (VoiceOver is ON) & (AppleScript is ENABLED)
		end tell
	on error
		say eventString -- (VoiceOver is ON) & (AppleScript is DISABLED)
	end try
else
	say eventString -- (VoiceOver is OFF) & (AppleScript is ENABLED or DISABLED)
end if

NOTE: don’t see what returns my_Events variable because it returns the events of only the last loop of repeat loop. That is, the events of last calendar of repeat loop.

I tested, the script works correct. I tested this way:


set eventString to " "
set beginningDate to (current date)
tell beginningDate to set {its hours, its minutes, its seconds} to {8, 0, 0}
set endDate to beginningDate + (8 * hours)

tell application "Calendar"
	set allEvents to {}
	repeat with a_calendar in (calendars)
		tell a_calendar
			set my_Events to (its events whose (start date ≥ beginningDate) and (start date ≤ endDate))
		end tell
		set allEvents to allEvents & my_Events
	end repeat
end tell

set eventCount to count allEvents
if eventCount < 10 then
	set eventString to eventCount & " events today" as string
else
	set eventString to eventCount & " events today. Good Luck" as string
end if

tell application "System Events" to set isRunning to (name of processes) contains "VoiceOver"
if isRunning then -- VoiceOver is ON
	try
		tell application "VoiceOver"
			set x to bounds of vo cursor
			output eventString -- (VoiceOver is ON) & (AppleScript is ENABLED)
		end tell
	on error
		say eventString -- (VoiceOver is ON) & (AppleScript is DISABLED)
	end try
else
	say eventString -- (VoiceOver is OFF) & (AppleScript is ENABLED or DISABLED)
end if

return allEvents