Sunday, August 18, 2013

Google Apps Script demo – Send SMS on new important mail in GMail

Google Drive allows you to easily create and edit Documents, Presentations, Spreadsheets, Forms and Drawings. But not everyone might be familiar with the Google App Script integration functionalities similar to the Visual Basic scripting integration within Microsoft Office macro’s.

Recently, I found this tutorial to link GMail to Google Calendar written by Romain Vialard.

Based on this tutorial, I made the custom script below in order to get a free SMS’s from Google on every new unread e-mail marked as ‘Important’ within GMail. Setting up this little script will give you a brief indication of the power and possibilities of the Google App Script integration.

  1. In Google Calendar, register your mobile phone within the ‘Settings’ > ‘Mobile Setup’, to enable SMS notifications. Sending SMS’s from within Google is free of charges, but it might be limited to 50 messages per day.
  2. In Gmail, create a new label named 'NbSMS' (Notified by SMS). Once a new mail has been notified by SMS, this label will be assigned to the mail in order to make sure the notification is send out only once and the mail will be ignored by the script once this label is assigned.
  3. In Google Drive, create a new Spreadsheet, give it any name.
  4. Choose the menu ‘Tools’ > ‘Script Editor’.
  5. Choose ‘Create script for: Blank Project’.
  6. Copy and paste the following script:
    function sendText() {
    Logger.log('Start of sendText script');
    var today = new Date();
    var nowHour = today.getHours();
    var startTime = 8;
    var endTime = 24;

    Logger.log('The SMS notification will only run between: ' + startTime + ' and ' + endTime + ', current hour: ' + nowHour);
    if (nowHour <= startTime || nowHour >= endTime) {
    Logger.log('Quite time, no SMS notification will be sent between: ' + startTime + ' and ' + endTime + ', current hour: ' + nowHour);
    } else {
    // var events = cal.getEvents(new Date(startDateAndTime), new Date(endDateAndTime));
    //based on https://developers.google.com/apps-script/articles/gmail_filter_sms
    var notifiedBySmsLabel = 'NbSMS';
    var unreadPriority = GmailApp.getPriorityInboxUnreadCount();
    var unreadsFound = 0;
    Logger.log("Number of unread emails in your Priority Inbox : " +
    GmailApp.getPriorityInboxUnreadCount());
    if (unreadPriority > 0) {
    var threads = GmailApp.getPriorityInboxThreads();
    //threads.refresh();
    var now = new Date().getTime();
    var alreadyNotified = false;
    for(i in threads){
    if (!threads[i].isUnread()) {
    alreadyNotified = true;
    if(unreadsFound >= unreadPriority) {
    break;
    } else {
    continue;
    }
    } else {
    ++unreadsFound;
    }
    var threadLabels = threads[i].getLabels();
    for(y in threadLabels) {
    if(threadLabels[y].getName() == notifiedBySmsLabel) {
    alreadyNotified = true;
    break;
    }
    }
    if (!alreadyNotified) {
    var smsText = 'Mail: '+threads[i].getFirstMessageSubject() + ', from: ' + threads[i].getMessages()[0].getFrom();
    smsText = smsText + ' ' + threads[i].getMessages()[0].getPlainBody();
    Logger.log('Event with SMS will be created with content: ' + smsText);
    var event = CalendarApp.createEvent(smsText,
    new Date(now+60000),
    new Date(now+60000));
    event.setDescription(smsText);
    event.addSmsReminder(0);
    var label = GmailApp.getUserLabelByName(notifiedBySmsLabel);
    label.addToThread(threads[i]);
    }
    }
    //threads.refresh();
    Logger.log('All important messages treated and label ' + notifiedBySmsLabel + ' applied.');
    }
    }
    }



  7. Choose the menu ‘Resources’ > ‘Current project’s triggers…’ and add a new trigger.


  8. Select the function ‘sendText()’ > Events: ‘Time-driven’ > ‘Minutes timer’ > ‘Every 5 minutes’, and save the trigger.


  9. Save the script.


  10. Click the Run ► icon. A pop-up opens asking you for your authorization to access the Gmail and Google Calendar services.


  11. Click the Authorize button.


  12. Click the Run ► icon again.


  13. Choose the menu ‘View’ > ‘Logs’ to see the log output.


  14. Debugging is possible using the bug icon. Set some breakpoints by clicking on the line number next to the script.


  15. After one minute, you should receive a text on your mobile device, containing the subject of the important unread emails within GMail.


  16. To see what other functionalities are available within GMail, Google Calendar en other Google services, see the Google App Script reference guide.


  17. Some other interesting tutorial to work with Google Calendar events is available in this blog post.



Update 09/04/2014: optimized the labeling to only apply the label to messages which were not yet labeled, instead of all messages in priority inbox.



Update 21/10/2015: Google no longer supports to get notified by SMS for a calendar item. So the approach above won’t work any longer (calendar item will be created, but no sms will be received).

Thursday, August 8, 2013

Svn file full history

If you’d need to search in the full history of a file stored in SVN, the script below can make an export of all changes ever applied into a file, starting for the first revision (full file) and adding each time an overview of all changes applied.

If you remember you ever applied some changes in a file, but removed it afterwards, this can help to search in the full history. Only tested on text based files.

For each revision, the revision number, author, timestamp and svn comments will be listed, followed by an overview of the removed lines, added lines, update lines etc. The initial revision will be a full extract of the original file.

Usage: save the SvnFileFullHistory.bat script in an SVN folder next to the file for which you’d need a full history extract. Drop the svn file onto the bat script to start the script and retrieve the full history of the dropped svn file. The full svn file history will be saved in a txt file in the same folder and with the same base name as the file to extract, but adding “-FullSvnHistory.txt” behind the file name. The extract can take a while, depending on the number of revisions and the size of the svn file. But while the extract is ongoing, the “…-FullSvnHistory.txt” can be read already, reloading it to get future updates.

A similar linux shell script has been created as well (not fully tested).

Based on information found on stackoverflow by user ladenedge.

Full script content:

@echo off   
TITLE SVN - Full file history
REM Original source: http://stackoverflow.com/questions/282802/how-can-i-view-all-historical-changes-to-a-file-in-svn and http://stackoverflow.com/questions/5622367/generate-history-of-changes-on-a-file-in-svn/5721533#5721533
echo Copy this bat script next to the checked out svn file on which to get full svn history. Drag and drop the svn file onto the bat script to start fetching the info (or a open command window and provide the name of the svn file as first parameter to the bat script execution)
if "%1%"=="" pause
set file=%1
set report=%file%-FullSvnHistory.txt
if [%file%] == [] (
  echo Usage: "%0 <file>"
  exit /b
)
echo Retrieving svn history of file, please wait...
echo The report will be saved in the file: %report%.
echo To stop the process press Ctrl+c.
rem first revision as full text
for /F "tokens=1 delims=-r " %%R in ('"svn log -q %file%"') do (
  svn log -r %%R %file% > %report%
  svn cat -r %%R %file% >> %report%
  goto :diffs
)
:diffs
rem remaining revisions as differences to previous revision
for /F "skip=2 tokens=1 delims=-r " %%R in ('"svn log -q %file%"') do (
  echo.
  svn log -r %%R %file% >> %report%
  svn diff -c %%R %file% >> %report%
)