Apple script to add tagged items to todoist

Hi,

i have an apple script, which I used to add E-Mails to todoist.
I created an “message-id” link to the original message and the subject as displayed text e.g. “subject”.
I triggered this manually. Now I adapted the script, to create a link to the DEVONthink item. creating the link works and I verified it using a display dialoge action, copied the “todoist://addtask?..” link to safari and the task was added like i expected.
The problem I have is, that I can’t automate this within the script. In the Mail script, I simply called an “open location” step with the link as parameter, but when I execute this script in devonthink, I just hear this warning sound from macOS and nothing happens. When I comment out the open location step, and insert the display dialoge step, it works…

Second part of the question: I’d like to automate this based on a tag. Every Item, which receives the tag “#todo” should be added to todoist. I’m not sure, which trigger to use - so when to execute this script, because I dont want to add the same item five times :smiley: I just want to add it if the tag was assigned newly to this item.

I could create a shortcut on iOS to do this, too but i think thats more complicated than on the mac…
Thank you!

Could you please post the current script?

A revised version of the script could be used by smart rules, e.g. triggered on tagging.

hi, here is the script:

on performSmartRule(theRecords)
	tell application id "DNtp"
		repeat with theRecord in theRecords
			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) & ")"
			
			
			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
			t
			open location "todoist://addtask?content=" & (encoded_task)
			
			tell application "System Events"
				delay 0.3
				keystroke return
				delay 0.3
			end tell
		end repeat
	end tell
end performSmartRule

I did not find a setting like “on tagging”.?

I did not find a setting like “on tagging”.?

That will be available in the next maintenance release. Thanks for your patience and understanding.

1 Like

Could you do something similar with OmniFocus?

Bill

According to this page, it should be possible…

https://inside.omnifocus.com/url-schemes

you simply need to change the url like @BLUEFROG mentioned :slight_smile:

Any Ideas why the script is not working?

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?