Here is the error I have in Script Editor: Error on line 72: Error: sh: img2pdf: command not found
I'm writing a JavaScript for Automation (JXA) script to automate online math homework collection after asking that question. The script is finally finished.
However one of the programs I need to use through Shell, img2pdf does not really work using doShellScript(). This is unexpected because in osascript
it perfectly works.
//Clean sender
function cleanSender(sender) {
var pos = sender.search("<");
return sender.slice(0, pos - 1);
}
//Num to string, padding included
function numToString(num) {
if(num < 10) {
return '0' + num.toString();
}
else {
return num.toString();
}
}
//date to time stamp
function genTimeStamp(date) {
if (!(date instanceof Date)) {
console.log('Type Error!');
return undefined;
}
var hours = numToString(date.getHours());
var minutes = numToString(date.getMinutes());
var seconds = numToString(date.getSeconds());
var month = numToString(date.getMonth() + 1);
var day = numToString(date.getDate());
var year = numToString(date.getFullYear());
var timeStamp = month+day+year+hours+minutes+seconds;
return timeStamp;
}
function run() {
var mail = Application('Mail');
var finder = Application('Finder');
var app = Application.currentApplication();
var keyword = "Test";
var topFolderPath = "/Users/CatLover/Documents/HWBox";
var topFolder = finder.startupDisk.folders.byName("Users").folders.byName("CatLover").folders.byName("Documents").folders.byName("HWBox");
mail.includeStandardAdditions = true;
finder.includeStandardAdditions = true;
app.includeStandardAdditions = true;
var messages = mail.inbox.messages;
var messagesLength = messages.length;
for(let i = 0; i < messagesLength; i++) {
let message = messages[i];
if (message.subject().includes(keyword) && message.mailAttachments().length != 0) {
var sender = cleanSender(message.sender());//Name only
var timeStamp = genTimeStamp(message.dateSent());//Folder name
var attachments = message.mailAttachments();
var individualPath = topFolderPath + '/' + sender;
if (!finder.exists(Path(individualPath))) {
//No folder!
finder.make({new: "folder", at: Path(topFolderPath), withProperties:{name: sender}});
}
var messagePath = individualPath + '/' + timeStamp;
if (!finder.exists(Path(messagePath))) {
//No folder!
finder.make({new: "folder", at: Path(individualPath), withProperties:{name: timeStamp}});
var command = "img2pdf ";
var attachmentsLength = attachments.length;
for(let j = 0; j < attachmentsLength; j++) {
let attachment = attachments[j];
var filename = attachment.name();
var filePath = messagePath + '/' + filename;
mail.save(attachment, {in: Path(filePath)});
command = command + filePath + ' ';
}
if (attachmentsLength > 1) {
//Pics
var pdfName = sender + timeStamp;
command = command + '-o ' + messagePath + '/' + pdfName;
app.doShellScript(command);
}
}
else {
continue;//Non-spammers aren't going to send two emails at the same time down to the same second. No need to process an already processed email.
}
}
}
}
I expect all emails in my Inbox with subjects containing "Test" and attachments to be processed as follows:
The name of the sender should be extracted and a folder with that name be created in
HWBox
folder if it did not exist.The time stamp of the email in MMDDYYYYHHMMSS form should be extracted and a folder with that name be created in the folder corresponding to the name of the sender. If such a time stamp already existed it should be ignored.
All attachments of an email should be downloaded and put in the folder with its name generated by the time stamp in Step 2.
If there are more than 1 attachments I assume that they are all images. In this case the
img2pdf
program is used through Shell to process such images and convert them to a PDF.
In reality Step 1-3 work. Step 4 on the other hand does not work.