Check if a message has a label using Google Script - gmail

At work we have a google script code that is run every 4 hours and check everyone's #folders for unread mail. If unread mail is found it is moved to INBOX and tagged with ##UNREAD.
What i need is a way for the script to check if it already has the ##UNREAD tag and in that case not move to inbox.
This is the code
function process_unread() {
//Define user label
var label = GmailApp.getUserLabelByName("#Josh");
//Define unread label
var unreadlabel = GmailApp.getUserLabelByName("##UNREAD");
if (label) {
var threads = label.getThreads();
for (var i = 0; i < threads.length; i++) {
var thread = threads[i];
if (thread.isUnread()) {
//Remove label
thread.addLabel(unreadlabel);
thread.moveToInbox();
}
}
}
}
How can i only move emails if they do not have the ##UNREAD label?

Here is my attempt:
function process_unread() {
//Define user label
var label = GmailApp.getUserLabelByName("#Josh");
//Define unread label
var unreadlabel = GmailApp.getUserLabelByName("##UNREAD");
if (label) {
var threads = label.getThreads();
for (var i = 0; i < threads.length; i++) {
var thread = threads[i];
var labels = thread.getLabels();
var doesThisThreadHaveTheLabel = false;
for (var i = 0; i < labels.length; i++) {
var thisParticularLabel = labels[i].getName();
Logger.log(labels[i].getName());
if (thisParticularLabel === "##UNREAD") {
var doesThisThreadHaveTheLabel = true;
};
}
if (thread.isUnread() && doesThisThreadHaveTheLabel === false) {
//Remove label
thread.addLabel(unreadlabel);
thread.moveToInbox();
}
}
}
}
Before you move the thread to the inbox, you want to make sure it does NOT have the label. So add another condition to the If check.
if (thread.isUnread() && doesThisThreadHaveTheLabel === false) {
I created a variable: doesThisThreadHaveTheLabel that will either have a true or false value. It's default gets set to false before every for loop.
var doesThisThreadHaveTheLabel = false;
for (var i = 0; i < labels.length; i++) {
You can debug the code to check it:
In the above picture, you see an icon of a bug in the menu. Before you click that, first click the drop down menu just to the right of the bug, and choose the name of the function to run. Also, add a breakpoint to the code. In that picture you'll see a red dot in the line numbers in the code editor. That's where the code will stop.
I added the label #josh to one of the emails in my account, so the variable label has an object in it. But, I don't have any emails with a label ##UNREAD , so you'll notice that in the list of variables, the variable unreadlabel has a value of null.
In that picture, the code is suspended on the line, 269. I can step in to the next line of code by clicking the step in icon. Hover over the icon to get a context help to pop up.
I stepped line by line further, and retrieved the label that was put into the variable "ThisParticularLabel". You can see in the window, that it has a value of #Josh.
I stepped through that code, and it ended after the main for loop had run once. I also ran that code by itself without debugging it, and it ran in:
Execution succeeded [0.246 seconds total runtime]
You need to debug your code, and see what it is doing on every line, and know what every single variable has for a value, and how the conditional statements are working.

Script will not finish with two for loops both using variable i as i will be set to 0 continually by the inner for loop and will not increment. Switch inner loop to j variable and script will finish.

Related

New Filter of Gmail for auto movetrash specific subjects with time condition. Warning: it never matches the incoming email

I want to make filters in Gmail email with certain subjects automatically deleted if more than 1 day from example#gmail.com. but when the filter is entered there is a warning from gmail:
Filter searches containing "label:", "in:", "is:", date range (e.g. "before:" or "after:") or stars criteria (e.g. "has:yellow-star") are not recommended as they will never match incoming emails. Do you still wish to continue to the next step?
How do you do it in the latest version (Gmail)?
I want to delete emails with certain subjects more than 1 day from example#gmail.com, how do that?.
Try using Google Script:
- Open: https://script.google.com
- Click File => New => Script File
- Type this code
// reference: https://medium.com/#fw3d/a-cleaning-robot-for-your-gmail-inbox-f44c01306ea2
// Modify by: Dicky Ibrohim for delete (move to trash) specific subject, sender, and time
function removetotrash() {
var delayDays = 1; // Impact form email more than 1day
var maxDate = new Date();
maxDate.setDate(maxDate.getDate()-delayDays);
// What will be executed, adjust the string here
var searches = [
'"SUBJECT WANT SEARCH TO DELETE" from:example#gmail.com'
];
// Looping for 500 last email
var threads = [];
for (var i = 0; i < searches.length; i++) {
var tmp_threads = GmailApp.search(searches[i], 0, 500); // search until 500 mail
var threads = threads.concat(tmp_threads);
}
// Then remove all to trash
for (var i = 0; i < threads.length; i++) {
if (threads[i].getLastMessageDate()<maxDate)
{
threads[i].moveToTrash();
}
}
}
Click File => Save
Make it work automatic: Click icon "Current project's triggers",
Click "Create new triger"
Setup AND SELECT intermediate time
Save

Magento - How to pass value from template to js file?

I developed an extension where you can define a connection id for newsletter 2 go.
I need to pass this id to the script skin\frontend\base\default\fekete\Newsletter2Go\js\utils.js
Template:
My first approach was to pass it to the script by adding a get parameter to it.
{... Hint: I removed the rest code which is not relevant for this question ... }
(window,document,"script","skin/frontend/base/default/fekete/Newsletter2Go/js/utils.js?id=<?php echo $id ?>","n2g");
I checked if this worked like this:
alert(findGetParameter("id"));
function findGetParameter(parameterName) {
var result = null,
tmp = [];
var items = location.search.substr(1).split("&");
for (var index = 0; index < items.length; index++) {
tmp = items[index].split("=");
if (tmp[0] === parameterName) result = decodeURIComponent(tmp[1]);
}
return result;
}
But it alerts null.
How can I pass the id to the script, without having to move the whole script to the template?
I solved it by creating a new extension. I developed a controller and then just made a XHR Request to my route, which just returns the needed value.

Inventory Assignment Sublist doesn't react to Suitescript

I'm currently having some trouble with Client-Side Scripting of an Inventory Detail Subrecord on an Assembly Build. As you know, Assembly Builds have two Inventory Details. The one in the top right corner works as expected, and I can access every field with Suitescript.
I'm having trouble with the bottom Inventory Detail though. I'm able to access it and use nlapiGetFieldValue() just fine. However, when I access the sublist, I am only able to look up the value of 'id'.
These are the fields that are supposed to exist, along with a less documented one called "receiptinventorynumber".
Here is my code:
//get the line items in the bottom inventory details
var bottom_line_items = [];
for(var line_index = 0; line_index < nlapiGetLineItemCount("component"); line_index++)
{
var bottom_inv_detail = nlapiViewLineItemSubrecord("component", 'componentinventorydetail', line_index+1);
if(bottom_inv_detail != null)
{
var bottom_line_count = bottom_inv_detail.getLineItemCount('inventoryassignment');
for(var index =0; index < bottom_line_count; index++)
{
bottom_inv_detail.selectLineItem('inventoryassignment', index+1);
var sn = bottom_inv_detail.getCurrentLineItemValue('inventoryassignment', 'receiptinventorynumber');
bottom_line_items.push(sn);
}
}
}
console.log(bottom_line_items);
Here is the result of executing it in the browser console:
As you can see, 'id', and 'internalid' work. 'receiptinventorynumber' does not. Neither do any of the other fields.
Because of my use case, I cannot wait for the record to be saved on the server. I have to catch this client side. Any suggestions are appreciated.
It has been a long time since I have worked with Inventory Detail subrecord, but I think there is another field called 'assigninventorynumber'. Have you tried using that?
Just was able to answer my own question, but it did end up involving a server-side search. I'm pretty sure it works as I wanted it to, but I am still involved in testing it. In essence, I grabbed the field 'issueinventorynumber', which had an id. That id was the internalid of a 'Inventory Serial Number', which I was able to perform a search for to get the actual number. Here's the resulting code:
//get the line items in the bottom inventory details
var bottom_line_ids = [];
for(var line_index = 0; line_index < nlapiGetLineItemCount("component"); line_index++)
{
var bottom_inv_detail = nlapiViewLineItemSubrecord("component", 'componentinventorydetail', line_index+1);
if(bottom_inv_detail != null)
{
var bottom_line_count = bottom_inv_detail.getLineItemCount('inventoryassignment');
for(var index =0; index < bottom_line_count; index++)
{
bottom_inv_detail.selectLineItem('inventoryassignment', index+1);
var sn = bottom_inv_detail.getCurrentLineItemValue('inventoryassignment', 'issueinventorynumber');
bottom_line_ids.push(sn);
}
}
}
//do search to identify numbers of bottom serial numbers
var columns = [new nlobjSearchColumn('inventorynumber')];
var filters = []
for(var index = 0; index < bottom_line_ids.length; index++)
{
filters.push(['internalid', 'is', bottom_line_ids[index]]);
filters.push('or');
}
//remove the last 'or'
if(filters.length > 0)
{
filters.pop();
}
var search = nlapiCreateSearch('inventorynumber', filters, columns);
var results = search.runSearch().getResults(0,1000);
bottom_line_items = []
if(results.length != bottom_line_ids.length)
{
//if you get to this point, pop an error as the 'issueinventorynumber' we pulled is associated with multiple serial numbers
//you can see which ones by doing a 'Inventory Serial Number' Saved Search
//this is a serious problem, so we'd have to figure out what to do from there
}
for(var index = 0; index < results.length; index++)
{
bottom_line_items.push(results[index].getValue('inventorynumber'));
}
console.log(bottom_line_items);

(After Effects) Change text content over the time

I would like to put the title + artist name when the song is playing, but I don't know how to achieve this, because with almost 30-35 songs, it would be long and boring to create a specific text layer for each song.
If there is a trick to achieve this quickly.
You can achieve this via scripting.
I have these two scripts that should do what you want. I wrote them some time ago. Maybe you need to do some adjustments.
This one adds several text layers from a csv file.
https://github.com/fabiantheblind/after-effects-script-snippets/blob/master/comp_with_text.jsx
This one should add one text layer with the source text set to the content of a csv.
https://github.com/fabiantheblind/after-effects-script-snippets/blob/master/text_to_comp.jsx
This is a minimal example to add a text layer with sourceText
/**
* main function
*/
var main = function() {
var txt = ['Hello - World', 'dog -cat', 'foo - bah']; // the text to add
app.beginUndoGroup('add source text'); // open a undo group
var curComp = app.project.activeItem; // get the current comp
// check if the curent active item is a comp
if (!curComp || !(curComp instanceof CompItem)) {
alert('noComp');
return;
// end if no comp is active
}
var txtLayer = curComp.layers.addText('titles'); // add a text layer
var counter = 0; // the time to add a keyframe to (in seconds)
// loop the text
for (var i = 0; i < txt.length; i++) {
var curFrame = (counter / curComp.frameRate); // calc time for each frame
$.writeln(curFrame);
// add a keyframe with the text as value every frame
txtLayer.text.sourceText.setValueAtTime(curFrame, txt[i]);
counter++; // increase the time by one
}
app.endUndoGroup();
};
main();

recognize multple lines on info.selectionText from Context Menu

My extension adds a context menu whenever a user selects some text on the page.
Then, using info.selectionText, I use the selected text on a function executed whenever the user selects one of the items from my context menu. (from http://code.google.com/chrome/extensions/contextMenus.html)
So far, all works ok.
Now, I got this cool request from one of the extension users, to execute that same function once per line of the selected text.
A user would select, for example, 3 lines of text, and my function would be called 3 times, once per line, with the corresponding line of text.
I haven't been able to split the info.selectionText so far, in order to recognize each line...
info.selectionText returns a single line of text, and could not find a way to split it.
Anyone knows if there's a way to do so? is there any "hidden" character to use for the split?
Thanks in advance... in case you're interested, here's the link to the extension
https://chrome.google.com/webstore/detail/aagminaekdpcfimcbhknlgjmpnnnmooo
Ok, as OnClickData's selectionText is only ever going to be text you'll never be able to do it using this approach.
What I would do then is inject a content script into each page and use something similar to the below example (as inspired by reading this SO post - get selected text's html in div)
You could still use the context menu OnClickData hook like you do now but when you receive it instead of reading selectionText you use the event notification to then trigger your context script to read the selection using x.Selector.getSelected() instead. That should give you what you want. The text stays selected in your extension after using the context menu so you should have no problem reading the selected text.
if (!window.x) {
x = {};
}
// https://stackoverflow.com/questions/5669448/get-selected-texts-html-in-div
x.Selector = {};
x.Selector.getSelected = function() {
var html = "";
if (typeof window.getSelection != "undefined") {
var sel = window.getSelection();
if (sel.rangeCount) {
var container = document.createElement("div");
for (var i = 0, len = sel.rangeCount; i < len; ++i) {
container.appendChild(sel.getRangeAt(i).cloneContents());
}
html = container.innerHTML;
}
} else if (typeof document.selection != "undefined") {
if (document.selection.type == "Text") {
html = document.selection.createRange().htmlText;
}
}
return html;
}
$(document).ready(function() {
$(document).bind("mouseup", function() {
var mytext = x.Selector.getSelected();
alert(mytext);
console.log(mytext);
});
});​
http://jsfiddle.net/richhollis/vfBGJ/4/
See also: Chrome Extension: how to capture selected text and send to a web service

Resources