Help converting old iCal script to python

Use this forum to post specific questions you may have when trying to convert an AppleScript to Python. Answers may also include pointers to plugins that would help.
digtrail
Posts: 28
Joined: Thu Jun 02, 2016 8:45 am

Help converting old iCal script to python

Post 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
 
User avatar
jay (support)
Site Admin
Posts: 18411
Joined: Wed Mar 19, 2008 11:52 am
Location: Austin, Texas
Contact:

Re: Help converting old iCal script to python

Post 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')
Jay (Indigo Support)
Twitter | Facebook | LinkedIn
digtrail
Posts: 28
Joined: Thu Jun 02, 2016 8:45 am

Re: Help converting old iCal script to python

Post 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')
howartp
Posts: 4559
Joined: Thu Jan 09, 2014 4:43 pm
Location: West Yorkshire, UK

Re: Help converting old iCal script to python

Post 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
User avatar
jay (support)
Site Admin
Posts: 18411
Joined: Wed Mar 19, 2008 11:52 am
Location: Austin, Texas
Contact:

Re: Help converting old iCal script to python

Post 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).
Jay (Indigo Support)
Twitter | Facebook | LinkedIn
digtrail
Posts: 28
Joined: Thu Jun 02, 2016 8:45 am

Re: Help converting old iCal script to python

Post 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
digtrail
Posts: 28
Joined: Thu Jun 02, 2016 8:45 am

Re: Help converting old iCal script to python

Post 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
User avatar
jay (support)
Site Admin
Posts: 18411
Joined: Wed Mar 19, 2008 11:52 am
Location: Austin, Texas
Contact:

Re: Help converting old iCal script to python

Post 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).
Jay (Indigo Support)
Twitter | Facebook | LinkedIn
digtrail
Posts: 28
Joined: Thu Jun 02, 2016 8:45 am

Re: Help converting old iCal script to python

Post by digtrail »

That worked. I'm back to automating based on my work schedule. Thanks!
digtrail
Posts: 28
Joined: Thu Jun 02, 2016 8:45 am

Re: Help converting old iCal script to python

Post by digtrail »

Hello all,

Having a new issue with this one. Realized certain schedules didn't seem to be working as should so I checked my variables list and some of those weren't updated as should be. Went to run this script and got this error in event log...

Code: Select all

cript Error                    embedded script error:
   Script Error                    'ascii' codec can't decode byte 0xe2 in position 0: ordinal not in range(128)
   Script Error                    Exception Traceback (most recent call shown last):

     embedded script, line 3, at top level
     File '/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/encodings/ascii.py', line 26, in decode
       return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 0: ordinal not in range(128)
Here is the full python script that should run on a schedule...

Code: Select all

import os
the_string = os.popen('/usr/local/bin/icalbuddy eventsToday').read()
if "Day Work" in the_string:
   indigo.variable.updateValue(1422763041, value='true')
else:
   indigo.variable.updateValue(1422763041, value='false')

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

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

if "VAC" in the_string:
   indigo.variable.updateValue(41667262, value='true')
else:
   indigo.variable.updateValue(41667262, value='false')
Any help is appreciated
User avatar
FlyingDiver
Posts: 7323
Joined: Sat Jun 07, 2014 10:36 am
Location: Southwest Florida, USA

Re: Help converting old iCal script to python

Post by FlyingDiver »

You should print out "the_string" to the log after reading it in so you can see what's actually there.

Code: Select all

import os

the_string = os.popen('/usr/local/bin/icalbuddy eventsToday').read()
indigo.server.log(f"the_string = {the_string}")

if "Day Work" in the_string:
   indigo.variable.updateValue(1422763041, value='true')
else:
   indigo.variable.updateValue(1422763041, value='false')

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

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

if "VAC" in the_string:
   indigo.variable.updateValue(41667262, value='true')
else:
   indigo.variable.updateValue(41667262, value='false')
joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177
digtrail
Posts: 28
Joined: Thu Jun 02, 2016 8:45 am

Re: Help converting old iCal script to python

Post by digtrail »

Tried that. It didn't print out. Returned same script error
digtrail
Posts: 28
Joined: Thu Jun 02, 2016 8:45 am

Re: Help converting old iCal script to python

Post by digtrail »

I'm looking at the GitHub for icalbaddy issues https://github.com/ali-rantakari/icalBuddy/issues and it looks like its going to be a python2 to python3 problem
User avatar
FlyingDiver
Posts: 7323
Joined: Sat Jun 07, 2014 10:36 am
Location: Southwest Florida, USA

Re: Help converting old iCal script to python

Post by FlyingDiver »

If you run that command in a Terminal window, what do you get?
joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177
Post Reply

Return to “Help Converting from AppleScript”