Skip to content

Instantly share code, notes, and snippets.

@keverw
Last active October 17, 2017 21:10
Show Gist options
  • Save keverw/dd82b8d6ae9311176a88955a02798eaa to your computer and use it in GitHub Desktop.
Save keverw/dd82b8d6ae9311176a88955a02798eaa to your computer and use it in GitHub Desktop.
Cleanup Apple Notes leftover in Gmail (I moved my notes to iCloud but noticed it kept a bunch of leftovers of every single revision every in gmail, so wrote a small script to remove them to a label to review/delete anything no longer needed)
var Imap = require('imap'),
inspect = require('util').inspect;
var imap = new Imap({
user: 'yourname@gmail.com',
password: 'yourpassword',
host: 'imap.gmail.com',
port: 993,
tls: true
});
function openInbox(cb) {
imap.openBox('[Gmail]/All Mail', false, cb);
}
imap.once('ready', function () {
openInbox(function (err, box) {
if (err) throw err;
var f = imap.seq.fetch('1:' + box.messages.total, {
bodies: 'HEADER.FIELDS (FROM TO SUBJECT DATE X-Uniform-Type-Identifier)',
struct: true
});
f.on('message', function (msg, seqno) {
var isNote = false;
console.log('Message #%d', seqno);
var prefix = '(#' + seqno + ') ';
msg.on('body', function (stream, info) {
var buffer = '';
stream.on('data', function (chunk) {
buffer += chunk.toString('utf8');
});
stream.once('end', function () {
var headers = Imap.parseHeader(buffer);
if (headers.hasOwnProperty('x-uniform-type-identifier') && headers['x-uniform-type-identifier'].length > 0 && headers['x-uniform-type-identifier'][0] == 'com.apple.mail-note') {
console.log(Imap.parseHeader(buffer));
isNote = true;
}
});
});
msg.once('attributes', function (attrs) {
if (isNote) {
imap.addLabels(attrs.uid, 'Notes', function (err) {
if (err) console.log('setLabels error: ', err);
});
console.log(prefix + 'Attributes: %s', inspect(attrs, false, 8));
}
});
msg.once('end', function () {
//console.log(prefix + 'Finished');
});
});
f.once('error', function (err) {
console.log('Fetch error: ' + err);
});
f.once('end', function () {
console.log('Done fetching all messages!');
imap.closeBox(function (err) {
if (err) console.log(err);
imap.end();
});
});
});
});
imap.once('error', function (err) {
console.log(err);
});
imap.once('end', function () {
console.log('Connection ended');
});
imap.connect();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment