Using IFTTT with BTT! (My example works out remaining Todoist tasks and returns them)

todoist
ifttt
dropbox
touch-bar

#1

So after a long day of trial, error, and crying to StackOverflow (Thanks CJK). I was able to put together something that although might need some more design work, meets all the functionality I've been looking for.

This is for Todoist, but you could literally use it with any IFTTT service, to display the text on your TouchBar.

So two applets to set up in IFTTT to start:

  1. When new Todoist task is created > Append to text file in Dropbox
  2. When Todoist task is completed > Append to text file in Dropbox

The ingredients you choose is completely up to you, just make sure the text is the same for both new and completed.

Here's mine:


Repeat for completed tasks - named Completed-Tasks

Now the Apple Script:

tell application "System Events"
	set the_folder to the folder "~/Dropbox/Todoist/Tasks"
	set the_file to the file "Tasks.txt" in the_folder
	
	set to_do to do shell script "cat " & ¬
		quoted form of (POSIX path of the_file as text)
	
	set the_folder2 to the folder "~/Dropbox/Todoist/Tasks"
	set the_file2 to the file "Completed-Tasks.txt" in the_folder2
	
	set do_complete to do shell script "cat " & ¬
		quoted form of (POSIX path of the_file2 as text)
end tell

set toDo to paragraphs of to_do
set toDoCompleted to paragraphs of do_complete


set Uniquelist to difference(toDo, toDoCompleted)

-- Return a list of the items in setA which aren't in setB.
on difference(setA, setB)
	set astid to text item delimiters
	set text item delimiters to return & return
	set setA to return & setA & return
	set text item delimiters to return & linefeed & return
	set setB to return & setB & return
	set text item delimiters to linefeed
	set text item delimiters to setB's text items
	set setA to setA's text items
	set text item delimiters to ""
	set setA to setA as text
	if ((count setA) > 0) then
		set text item delimiters to return & return
		set setA to text items of text 2 thru -2 of setA
	else
		set setA to {}
	end if
	set text item delimiters to astid
	repeat with i from 1 to ((count setA) - 1)
		set item i of setA to item i of setA & " || "
	end repeat
	return "{\"text\":\"" & (setA as text) & "\", 
\"icon_path\":\"" & (POSIX path of ((((path to application support folder from user domain) as text) & "BetterTouchTool:" as text) & "Todoist.icns") as text) & "\", 
\"background_color\": \"0,0,0,255\"}"
end difference

What's happening?
The AppleScript is converting the two Dropbox files to a list and then comparing both lists (as there's no way to clear the file out when a task is completed). From the comparison, it spits out the non-matched items (still to complete) and returns them to the TouchBar.

How might this help you?
Well... If you have Dropbox. You may litterally use any service with parts of this script for endless possibilities. Please fill me in with either ideas (I'll try and help you build upon the script), or your final results!


GoldenChaos-BTT: A complete Touch Bar UI replacement preset
Has anyone had any luck combining IFTTT with BTT? (I'm talking read/GET not write/POST)
#2

Hi @liamb,

Just joined this forum and came across your post, which I recall you seeking help for on Stack Overflow.

For a novice scripter, I think your resulting script is impressive, particularly where you manage to obtain the difference between two sets. However, there is still a lot of superfluous code in there, and I thought I'd try and help by tidying it up a bit.

Here's how I might have coded the full script:

set home to POSIX path of (path to home folder)
set to_do to read POSIX file (home & "Dropbox/Todoist/Tasks/Tasks.txt")
set do_Complete to read POSIX file (home & "Dropbox/Todoist/Tasks/Completed-Tasks.txt")

set toDo to paragraphs of to_do
set toDoCompleted to paragraphs of do_Complete
copy toDo to toDoOutstanding

minusSet of toDoCompleted from toDoOutstanding
createTBtext(text of toDoOutstanding)


-- Return a list of the items in sA which aren't in B.
to minusSet of B from A
	local A, B
	
	script
		property L1 : B
		property L2 : A
	end script
	
	tell result to repeat with i from 1 to count its L2
		if item i of its L2 is in its L1 then ¬
			set item i of its L2 to null
	end repeat
end minusSet


on createTBtext(L as list)
	set text item delimiters to " || "
	
    return the contents of ["{\"text\":\"", L, "\", \n\"icon_path\":\"", ¬
		POSIX path of (path to application support folder from user domain), ¬
		"BetterTouchTool:", "Todoist.icns", ¬
		"\", \n\"background_color\": \"0,0,0,255\"}"] as text
end createTBtext

Use what you like, discard what you don't. Any questions, let me know.


#3

Will have a play with this when I'm not swamped with Uni assignments. Thanks @CJK!