ForumsTips & TricksCreate task from starred message in Gmail


Create task from starred message in Gmail
Author Message
Widaker

Posted: Feb 08, 2015
Score: 1 Reference
I thought I'd share this Google Apps script I use to easily add tasks from messages in Gmail by simply starring them. The script checks your Gmail messages regularly and if a message is starred, it sends it to your Toodledo email address to add the task (using the quick add syntax). The message is then unstarred. Implementing it to work for you is more straightforward than you probably think.

The script is a Google Apps script (https://developers.google.com/apps-script/). You can easily create one in your Google Drive with the following steps:
- In Google Drive, click 'Create' and select 'Script' and then 'Blank Project' (you might have to enable Google Apps Scripts first).
- Once the new script has opened, copy the code below in it, but make sure to replace '[email protected]' by your own Toodledo email address.
- Tell Google how often to 'trigger' the script (i.e. how often to check for new starred messages): Click the button that looks like a stopwatch and add a time-driven trigger. I have it triggered every hour, but you can choose an interval that suits you best.
- Save your script and try it out.


<code>
function createTaskFromStarred() {
var threads = GmailApp.search('is:starred');
for (var h = 0; h < threads.length; h++) {
var messages = threads[h].getMessages();
for (var i = 0; i < messages.length; i++) {
if (messages[i].isStarred())
{
Logger.log(messages[i].getSubject());
GmailApp.sendEmail('[email protected]',
'/AR ' + messages[i].getSubject() + ' %Gmail',
'https://mail.google.com/mail/u/0/?shva=1#inbox/' + messages[i].getId() + '\n\n' + messages[i].getPlainBody());
messages[i].unstar();
}
}
}
}
</code>


Some remarks:
- The Gmail message is sent to your Toodledo task list using the message subject as task name, preceded by '/AR' ('Action Required'). The tag 'Gmail' is also added in Toodledo. The message body in plain text is included as task note, preceded by the message URL which will lead you back to the message in Gmail. You can of course change these things as you like (I guess the syntax to do so is pretty straightforward).
- I wrote this script to fit my workflow, but you can of course alter it as per your needs. I for instance don't use the star in Gmail for anything except for adding tasks to Toodledo. So I just get all the starred messages, send them to Toodledo and then unstar them (otherwise the message will be added to Toodledo over and over again). But if you already use the star in Gmail for something else, you might want to change the search for starred messages by a search for messages with a given label. Just make sure to have some action in place at the end of your script that prevents the message from being sent to Toodledo over and over again each time the script is triggered.
- You can modify this script to also check for other conditions to make it more sophisticated. E.g. if you regularly get bills via Gmail and you have a special folder/context/tag for this, you could have the script check for a star AND a label 'Bills' and add the corresponding folder/context/tag info in the quick add syntax in the script. Now, every time you star a message and add the label 'Bills', it will be sent to Toodledo, including the desired folder/context/tag.
- I've had some problems in the past with the script failing and throwing an error when adding messages with certain types of attachments. It's therefore good practice to put the unstarring of the message at the end of the script, after the sending to Toodledo. That way, when the script fails to send to Toodledo, the task will still be starred, indicating it still needs to be added.

I use this functionality very regularly, so hopefully it can also be useful for some of you. I'm not a programmer so the scripting might not be optimal or even proper, but it does the job for me. Any suggestions/remarks are of course welcome.
mirbogat

Posted: Feb 18, 2015
Score: 0 Reference
This seems pretty awesome and is something I have been searching for. For some reason I haven't been able to use IFTTT to do the same, even though it "should" work.

I'm getting a syntax error on row 3:

var threads = GmailApp.search('is:starred');

Any idea why?

Thanks!
Widaker

Posted: Feb 18, 2015
Score: 0 Reference
Posted by mirbogat:
This seems pretty awesome and is something I have been searching for. For some reason I haven't been able to use IFTTT to do the same, even though it "should" work.

I'm getting a syntax error on row 3:

var threads = GmailApp.search('is:starred');

Any idea why?

Thanks!


Do you have any more info about which specific syntax error? Also, that line is line 2 in the code, perhaps you're looking at the wrong one - or your first line is a blank one.

You could try replacing ' by ", although that shouldn't make a difference - both are used interchangeably in the API reference.
Wayne T.

Posted: Feb 18, 2015
Score: 0 Reference
Part of the problem might be if you copied and pasted it into google scripts editor. Don't include the html tags <code> or </code>.

The script should start with: function createTaskFromStarred() {
Pascal Zweipfenning

Posted: Feb 18, 2015
Score: 0 Reference
Using a IFTTT recipe works fine for me. I'm using a specific 'Toodledo' label. The thing is that you need to keep the mail in the nbox until the recipe has been triggered.
Widaker

Posted: Feb 18, 2015
Score: 0 Reference
Posted by Wayne T.:
Part of the problem might be if you copied and pasted it into google scripts editor. Don't include the html tags <code> or </code>.

The script should start with: function createTaskFromStarred() {

Good remark! That's most likely what the problem is for mirbogat. Unfortunately I cannot edit the first post anymore to make it less confusing - when originally posting I assumed all html tags would be interpreted in the formatting.


Posted by Pascal Zweipfenning:
Using a IFTTT recipe works fine for me. I'm using a specific 'Toodledo' label. The thing is that you need to keep the mail in the nbox until the recipe has been triggered.

I created the script after unsuccessfully having tried an IFTTT recipe to do the same. In the meantime the integration between Toodledo and IFTTT might have improved, but back then the functionality was still rather limited. In any case the Google Apps script is more flexible. You can use quick add syntax, and you could implement if-then-else checks combined with e.g. Gmail labels to even better fit your needs.

And more importantly, I've noticed that you cannot rely on IFTTT recipes to work well every time. And I don't want any task to slip through the cracks. If the Google Apps script fails, you will at least get a failure notification, and the email will remain starred.


This message was edited Feb 18, 2015.
Wayne T.

Posted: Mar 05, 2015
Score: 0 Reference
I've tried modifying the script to look for a label rather than the star, but haven't been able to figure it out.
Since labels are added to the entire thread rather than a single message, is it even possible?
Widaker

Posted: Mar 05, 2015
Score: 0 Reference
Posted by Wayne T.:
I've tried modifying the script to look for a label rather than the star, but haven't been able to figure it out.
Since labels are added to the entire thread rather than a single message, is it even possible?


The fact that labels are a property of the entire thread and not of an individual message indeed complicates things a bit. But you could use the code below. It looks for threads with the label 'Toodledo' and adds the last message of each of those threads. This is assuming that usually you will want the most recent message to be added and not the first one for instance.

Is this what you had in mind? Perhaps it is also useful setting your script to be triggered more regularly to prevent new messages having come in before the intended message was added.


function createTaskFromLabel() {
var threads = GmailApp.search('label:Toodledo');
var myLabel = GmailApp.getUserLabelByName('Toodledo');
for (var h = 0; h < threads.length; h++) {
var messages = threads[h].getMessages();
var iMess = messages.length-1;
Logger.log(messages[iMess].getSubject());
GmailApp.sendEmail('[email protected]',
'/AR ' + messages[iMess].getSubject() + ' %Gmail',
'https://mail.google.com/mail/u/0/?shva=1#inbox/' + messages[iMess].getId() + '\n\n' + messages[iMess].getPlainBody());
threads[h].removeLabel(myLabel);
}
}
Roman Jilge

Posted: Mar 25, 2015
Score: 0 Reference
Dear Widaker

Your script is awesome! Thank you.
Because I am not a developer and don't understand Google Script, do you have an idea, how to implement the exact function but instead of 'marked' email I would like to use a new added Google Calendar Event posted to Toodledo. I have tried IFTTT but doesn't work for me.

What I am looking for:
I would like to add an event in Google Cal.
Scripts checks Google Cal periodically...
If new event was found, the scipt post the Event as a task in Toodledo with following fields

# Name of the Task == Name of event in Google Cal
# Due Date and Time == Date and Start Time of the event in Google Cal
# Lenght == End of the event in Google Cal

Thank you for your help and best regards for sharing your great script.

Roman


This message was edited Mar 25, 2015.
Widaker

Posted: Mar 29, 2015
Score: 0 Reference
Posted by Roman Jilge:
Dear Widaker

Your script is awesome! Thank you.
Because I am not a developer and don't understand Google Script, do you have an idea, how to implement the exact function but instead of 'marked' email I would like to use a new added Google Calendar Event posted to Toodledo. I have tried IFTTT but doesn't work for me.

What I am looking for:
I would like to add an event in Google Cal.
Scripts checks Google Cal periodically...
If new event was found, the scipt post the Event as a task in Toodledo with following fields

# Name of the Task == Name of event in Google Cal
# Due Date and Time == Date and Start Time of the event in Google Cal
# Lenght == End of the event in Google Cal

Thank you for your help and best regards for sharing your great script.

Roman


Hi Roman,

Thanks for the feedback, glad to hear the script is of use to you as well.

Regarding your Google Cal script: Unfortunately, there is still no way to trigger a script when a new calendar event is added. But luckily there is a workaround. The script below first gets all the events from a given calendar within one year from now. It then checks for each of those events if it's been added to Toodledo already by checking its 'toodledo' tag value . If not, it adds it to Toodledo and tags it as added to Toodledo (to prevent it from being added to Toodledo over and over again).

Probably not the prettiest implementation, but I ran some quick tests with it and it seems to do what you had in mind. Some remarks:
- The script assumes that all the events you want to add are part of a single calendar (you can look up the calendar ID of your desired one the in Google calendar settings). But you can duplicate and modify the script to check for several calendars. The API reference (https://developers.google.com/apps-script/reference/calendar/) can also be helpful for modifying the script to suit your needs more.
- The 'toodledo' tag the script checks is not visible in the calendar interface as far as I can tell.
- Don't forget to again add a time-based trigger to regularly run the script.

Good luck!
Widaker


Script:

function createTaskFromNewEvent() {
var now = new Date();
var oneYearFromNow = new Date(now.getTime() + (365 * 24 * 60 * 60 * 1000));
var events = CalendarApp.getOwnedCalendarById('[email protected]').getEvents(now, oneYearFromNow);
for (var h = 0; h < events.length; h++) {
if (events[h].getTag('toodledo') != 'added')
{
var eventLength = (events[h].getEndTime() - events[h].getStartTime())/60000;
GmailApp.sendEmail('[email protected]',
events[h].getTitle() + ' #' + events[h].getStartTime() + ' ~' + eventLength + ' %Gcal',
'');
events[h].setTag('toodledo','added');
}
}
}


This message was edited Mar 29, 2015.
Ummagumma

Posted: Jun 16, 2015
Score: 0 Reference
This only works for a Google Apps account, not a regular GMail account, correct ?
Widaker

Posted: Jun 23, 2015
Score: 0 Reference
Posted by e20.09g:
This only works for a Google Apps account, not a regular GMail account, correct ?


Yes, you need the Google Apps functionalities. Here's how to (de)activate them: https://support.google.com/a/answer/182442?hl=en.


This message was edited Jun 23, 2015.
You cannot reply yet

U Back to topic home

R Post a reply

To participate in these forums, you must be signed in.