Page 1 of 1

Help converting old iCal script to python

PostPosted: Sun Nov 08, 2020 1:10 pm
by digtrail
I have an old AppleScript that I used to check my iCal for my work schedule using icalbuddy. The script then modifies four different variables based on what is returned. I put this together ages ago from some old examples I had seen here on the forum. I found one or two new python scripts that I might be able to modify but they are much more involved than this old script is. Problem is, I cant figure out how to put this in Python as my coding skills are about 1-2 levels above copy paste at this time of my life. Any help would be greatly appreciated. Thanks.

Code: Select all
 set LaString to (do shell script "/usr/local/bin/icalbuddy -nc -ic Work eventsToday")
 if LaString contains "Day Work" then
    set value of variable "DayWork" to "true"
 else
    set value of variable "DayWork" to "false"
 end if
 
 if LaString contains "Night Work" then
    set value of variable "NightWork" to "true"
 else
    set value of variable "NightWork" to "false"
 end if
 
 if LaString contains "OFF" then
    set value of variable "Off" to "true"
 else
    set value of variable "Off" to "false"
 end if
 
 if LaString contains "VAC" then
    set value of variable "Vac" to "true"
 else
    set value of variable "Vac" to "false"
 end if
 

Re: Help converting old iCal script to python

PostPosted: Mon Nov 09, 2020 10:52 am
by jay (support)
I have no idea what icalbuddy is, but just a straight port of the script would be something like this (untested of course):

Code: Select all
# Substitute anything below that starts with IDOF_ with the actual Indigo ID of the
# specified variable - right click the variable in the Variable List window and select Copy ID
import os
la_string = os.popen('/usr/local/bin/icalbuddy -nc -ic Work eventsToday').read()
if "DayWork" in la_string:
   indigo.variables.updateValue(IDOF_DayWork, value='true')
else:
   indigo.variables.updateValue(IDOF_DayWork, value='false')

if "Night Work" in la_string:
   indigo.variables.updateValue(IDOF_NightWork, value='true')
else:
   indigo.variables.updateValue(IDOF_NightWork, value='false')

if "OFF" in la_string:
   indigo.variables.updateValue(IDOF_Off, value='true')
else:
   indigo.variables.updateValue(IDOF_Off, value='false')

if "VAC" in la_string:
   indigo.variables.updateValue(IDOF_Vac, value='true')
else:
   indigo.variables.updateValue(IDOF_Vac, value='false')

Re: Help converting old iCal script to python

PostPosted: Mon Nov 09, 2020 11:22 am
by digtrail
Thanks for that. icallbuddy is a tiny little app that I found through the forum here. It will look for events on iCal and then can be imported into Indigo. Worked great until I moved to 7.4 awhile back. Didn't need this script at that time any more, but now I do again.

I updated variable id's and tried a run. I got this return:

Script Error embedded script: 'VariableList' object has no attribute 'updateValue'
Script Error Exception Traceback (most recent call shown last):

embedded script, line 8, at top level
AttributeError: 'VariableList' object has no attribute 'updateValue'

Code: Select all
# Substitute anything below that starts with IDOF_ with the actual Indigo ID of the
# specified variable - right click the variable in the Variable List window and select Copy ID
import os
la_string = os.popen('/usr/local/bin/icalbuddy -nc -ic Work eventsToday')
if "DayWork" in la_string:
   indigo.variables.updateValue(1422763041, value='true')
else:
   indigo.variables.updateValue(1422763041, value='false')

if "Night Work" in la_string:
   indigo.variables.updateValue(15646541, value='true')
else:
   indigo.variables.updateValue(15646541, value='false')

if "OFF" in la_string:
   indigo.variables.updateValue(790632292, value='true')
else:
   indigo.variables.updateValue(790632292, value='false')

if "VAC" in la_string:
   indigo.variables.updateValue(41667262, value='true')
else:
   indigo.variables.updateValue(41667262, value='false')

Re: Help converting old iCal script to python

PostPosted: Mon Nov 09, 2020 11:59 am
by howartp
As a guess, but I’m not at my PC:

Change indigo.variables.updateValue() to indigo.variable.updateValue()

ie make variables singular.


Sent from my iPhone using Tapatalk Pro

Re: Help converting old iCal script to python

PostPosted: Mon Nov 09, 2020 12:41 pm
by jay (support)
Yep, I think that's it. Stumbled over my own cleverness (I proposed the variables vs variable naming scheme, which I've come to regret somewhat).

Re: Help converting old iCal script to python

PostPosted: Mon Nov 09, 2020 3:56 pm
by digtrail
It runs now. BUT it doesn't do anything. So I tried to run icalbuddy direct in terminal and get this in return: bad CPU type in executable: /Users/Kevin/Downloads/icalBuddy-v1.8.8/icalBuddy

My guess is it is now longer compatible. It hasn't been updated for a long time.

Any other ideas on how to parse out daily calendar events ahead of time? I tried playing with cynical calendars but can't seem to figure hour to get it to see the event and modify the variable before the even actually happens. My old script ran at 1201a to look ahead in the day so that the variables would be changed and then other schedules ran later in the day at different times based on that. I searched and there are very few calendar threads here on the forum.

Thanks for the help

Re: Help converting old iCal script to python

PostPosted: Tue Nov 17, 2020 4:39 pm
by digtrail
Nearing success with this. I figured that the bad cpu type was bc I haven't used icalbuddy since before updating to os 10.15 and that it was a 32bit compiled executable. That assumption was right and even tho the original developer hasn't updated it in about 4-5yrs I was able to find an update that works all the way thru 10.17 now by following pull requests on git. Updated version of icalbuddy can be found here: https://github.com/DavidKaluta/icalBuddy64/releases

Installed updated version of icalbuddy and it runs successfully in terminal. However it seems that the script is able to "see" the output as it has in the past bc is it changes all of my variables to false when run in Indigo instead of recognizing that for instance today I am OFF. Any ideas on this? I appreciate the help to this point. Below is output in terminal when run successfully there

Code: Select all
Kevin@unifi ~ % icalbuddy -ic Work eventsToday
• Night Work (Work)
    notes: T-2
    ... - 7:00 AM
• Send mask email  (Work)
    6:45 AM - 6:50 AM
• OFF (Work)
    7:00 AM - 11:59 PM

Re: Help converting old iCal script to python

PostPosted: Wed Nov 18, 2020 9:32 am
by jay (support)
Sorry, left off something important - change this line:

Code: Select all
la_string = os.popen('/usr/local/bin/icalbuddy -nc -ic Work eventsToday')


to this:

Code: Select all
la_string = os.popen('/usr/local/bin/icalbuddy -nc -ic Work eventsToday').read()


(I also changed it in my post further up to avoid confusion).

Re: Help converting old iCal script to python

PostPosted: Wed Nov 18, 2020 4:36 pm
by digtrail
That worked. I'm back to automating based on my work schedule. Thanks!