Skip to content

Instantly share code, notes, and snippets.

@Rup1
Created October 11, 2018 15:45
Show Gist options
  • Save Rup1/ac128beb2243740fcfda5cdb9c2c91bd to your computer and use it in GitHub Desktop.
Save Rup1/ac128beb2243740fcfda5cdb9c2c91bd to your computer and use it in GitHub Desktop.
Preliminary script. File-watching specific folder using Chokidar, and automating Apple Mail using JXA.
var osa = require('osa2');
var fs = require('fs');
var chokidar = require('chokidar');
var watcher = chokidar.watch('/Users/rup/Desktop/trolls', {
ignored: /(^|[\/\\])\../,
ignoreInitial: true,
persistent: true,
});
watcher.on('add', function(path){
renameFile(path);
});
function renameFile(f){
var myString = "_JON."
var theName = f.replace('.', myString);
var textFile = "/Users/rup/Desktop/trolls/testtextfile.txt";
var textFileContent = fs.readFileSync(textFile, {encoding:"UTF-8"});
if(f.includes(myString)) {
console.log("name " +f+ " won't be changed!");
fs.appendFile(textFile, "Testtttttttt");
fs.appendFileSync(textFile, "\nTesting_November222");
}else{
console.log("Name changed from " +f+ " to: " + theName);
mail();
fs.renameSync(f, theName);
fs.appendFileSync(textFile, "\nchanged file ");
console.log(textFileContent);
console.log(fs.readdirSync("/Users/rup/Desktop/trolls/"));
}
}
function mail(){
console.log("starting mail");
return osa(function(){
var mail = Application("mail");
var systemEvents = Application("System Events");
var m = systemEvents.applicationProcesses.byName("Mail");
var emailList = m.windows[0].splitterGroups[0].splitterGroups[0].scrollAreas[0]
var app = {
scrollArea: m.windows[0].splitterGroups[0].splitterGroups[0].scrollAreas[0],
scrollDownButton: emailList.scrollBars[0].buttons[0],
scrollUpButton : emailList.scrollBars[0].buttons[1],
scrollDownEmail: m.windows[0].splitterGroups[0].splitterGroups[0].scrollAreas[1].scrollBars[0].buttons[0]
}
mail.activate();
delay(2);
scrollDown(14);
function scrollDown(x){
for(i=0;i<x;i++){
console.log("scrolling");
app.scrollDownEmail.click();
}
}
})();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment