Apple script to add tagged items to todoist

As smart rule scripts are executed in the background (and sometimes asynchronously), scripting the user interface or using system events in smart rule scripts is discouraged. How does the actual URL of the open location command look like? Maybe it’s an invalid URL.

The url is valid, as I said I debugged this by just adding a dialogue and displayed the url (this worked) and then I copied the url, pasted it into a new safari tab and the add task interface of todoist started.

tell application "System Events"
   delay 0.3
   keystroke return
   delay 0.3
end tell

What is the point of this?

kind of a workaround, the “addtask” url of todoist actually opens a quick entry menu, where the name of the task will be inserted. then normally I’d need to press enter to confirm the task. but as I want to automate this, the task entry window should just appear quickly and then disappear without the need to press any button / key.

open location has to be performed outside the tell application id "DNtp" block:

on performSmartRule(theRecords)
	repeat with theRecord in theRecords
		tell application id "DNtp"
			set this_subject to (name of theRecord) as Unicode text
			set the_reference to (the reference URL of theRecord) as string
			set link_text to "[" & (this_subject) & "](" & (the_reference) & ")"
		end tell
		
		set encoded_task to do shell script "/usr/bin/python -c 'import sys, urllib; print urllib.quote(sys.argv[1])' " & quoted form of link_text
		set theURL to "todoist://addtask?content=" & (encoded_task)
		open location theURL

		tell application "System Events"
			delay 0.3
			keystroke return
			delay 0.3
		end tell
	end repeat
end performSmartRule

NOTE:
The next release won’t require this workaround anymore.

thanks, but it still won’t open the link. I can hear the “bing” from macOS…

maybe I’ll try it with the Todoist CLI: https://dev.to/jiadar/connect-todoist-with-postbox-via-applescript-1kj1

I don’t use Todoist but tried it was other URLs (e.g. to contacts). Do you use my revised script, which version of macOS?

yes I do.

macOS Catalina 10.15.4

I just inserted

open location "https://duckduckgo.com"

to test another link, same behaviour. if I copy both links to the script editor and run them there it works just like intended

And what about this script? Does this work?

on performSmartRule(theRecords)
	open location "https://duckduckgo.com"
end performSmartRule

no (unsurprisingly) not.

I think there must be something wrong with permissions / security settings… can you tell me what has to be enabled?
DEVONthink is already enabled in the Accessibility settings.

You could check System Preferences > Security > Automation > DEVONthink 3.app and whether anything is disabled.

image
seems to be ok…

Does it work after booting the machine in safe mode by pressing the Shift key during startup?

I just get the group selector when running this smart rule.

Running the line in Script Editor opens the URL in my default browser as expected.

on performSmartRule(theRecords)
	tell application "System Events" to open location "https://duckduckgo.com"
end performSmartRule

Opens the URL in the default browser.

Do you use the public release or the latest build?

It was build 11.
Just updated to build 12 and it’s working as expected.

First of all Thank you for the help!
this did the trick:

final script number one:

on performSmartRule(theRecords)
	repeat with theRecord in theRecords
		tell application id "DNtp"
			set this_subject to (name of theRecord) as Unicode text
			set the_reference to (the reference URL of theRecord) as string
			set link_text to "[" & (this_subject) & "](" & (the_reference) & ")"
		end tell
		
		set encoded_task to do shell script "/usr/bin/python -c 'import sys, urllib; print urllib.quote(sys.argv[1])' " & quoted form of link_text
		set theURL to "todoist://addtask?content=" & (encoded_task)
		tell application "System Events" to open location theURL
		
		tell application "System Events"
			delay 0.3
			keystroke return
			delay 0.3
		end tell
	end repeat
end performSmartRule

@BillW this should work with Omnifocus, too. you would just need to replace the todoist link with “omnifocus:///add?name=” - please note: the link might not be formatted correctly in Omnifocus, but as I’m not a user, I can’t test this. maybe you need to add the “note” parameter to the called link and paste “the_reference” there…

as I mentioned here I already installed the CLI tool for todoist.
A few trial and errors and I got it working now and the addtask window won’t appear, it just happens in the background.
my script looks like this:

on performSmartRule(theRecords)
	repeat with theRecord in theRecords
		tell application id "DNtp"
			set this_subject to (name of theRecord) as Unicode text
			set the_reference to (the reference URL of theRecord) as string
		end tell
		
		do shell script "$HOME/path/to/the/script/scripts/addSubjectWithLink.sh " & "\"" & this_subject & "\" \"" & the_reference & "\""
		
	end repeat
end performSmartRule

and the shell script I run:

#!/bin/bash
/usr/local/bin/todoist add "[$1]($2)"

I’d be happy to help if someone else needs this.

You’re welcome.
Note the next release will forward unknown URLs, so the

open location "https://duckduckgo.com"

would work too.

great!

do you have any estimate, when the next release will be published?

I’m also wondering, how the “on tagging” trigger will work. E.g. if I add an item on the iPhone with DTTG and add the #todo tag there, will this also be triggered after sync?

And is it possible to force a smart rule to only once on an item? is this what the cancel step does? I did not find anything in the manual.

We don’t publish release dates but it will be soon.

I’m also wondering, how the “on tagging” trigger will work. E.g. if I add an item on the iPhone with DTTG and add the #todo tag there, will this also be triggered after sync?

A file tagged in DEVONthink To Go will not trigger an On Tagging event. It could be processed after an After Synchronization event.

And is it possible to force a smart rule to only once on an item? is this what the cancel step does? I did not find anything in the manual.

No. Just like folder actions, files can be processed more than once. That’s why we advocate moving the files or making a change so they’re no longer matched by the smart rule.

1 Like