Home > Blockchain >  Applescript that check if an event is today
Applescript that check if an event is today

Time:06-25

Is there a way to code an AppleScript that check iCal ? I want to make an AppleScript that do: if an event is today then do a task.

Or the inverse : if an event is not today then do a task.

Thanks ;-)

CodePudding user response:

To check if a date is within a specific day you have to subtract the time portion from the date (and the current date) which practically sets the time to 0:00:00

This handler takes a Calendar event and returns true if the start date of the event is in today

on isEventInToday(theEvent)
    tell application "Calendar"
        tell (current date) to set startOfToday to it - (its time)
        tell (get start date of theEvent) to set startOfEvent to it - (its time)
        return startOfToday is equal to startOfEvent
    end tell
end isEventInToday

If this handler is called many times create startOfToday outside of the handler and pass it as second parameter.

An alternative is AppleScriptObjC which has access to the Foundation framework

use AppleScript version "2.5"
use framework "Foundation"
use scripting additions

on isEventInToday(theEvent)
    tell application "Calendar" to set startDate to start date of theEvent
    set currentCalendar to current application's NSCalendar's currentCalendar()
    return (currentCalendar's isDateInToday:startDate) as boolean
end isEventInToday

CodePudding user response:

The Calendar.app checks itself when the event should perform attached alerts at the indicated start time of event. You have ability to indicate to Calendar.app to perform attached alert some time before start time of event, as well. One of provided alerts is open file alert, which is what you need in your case.

So,

  1. create new event, right click, then click Get Info
  2. set the start time of event (field "starts:" of info). For example, 10 minutes after the current time.
  3. Click "alert:" field of info
  4. Select in the pop up menu "At time of event" (for example)
  5. Click "Custom..." field of pop up menu
  6. Choose "Open file" instead of other alert type
  7. Select "Other..." instead of "Calendar" --> opens choose file dialog
  8. Select the script (or app) you want to be opened in your file system.
  9. Click "Apply"

Now, on indicated start time of the event, it will automatically open your attached compiled script in default script editor, or will run script saved as application if one was attached. Or, it will open some image in the Preview.app if the image file was attached, an so on. Even if the Calendar.app is not running at all.

On the Youtube exists this video tutorial to help more: https://www.youtube.com/watch?v=2HhFzIcHEGY for Using Custom Calendar Alerts On Your Mac.

  • Related