如何解决“第72行错误:错误:sh:img2pdf:找不到命令”

时间:2018-12-23 20:39:12

标签: javascript macos jxa

这是我在脚本编辑器中遇到的错误:Error on line 72: Error: sh: img2pdf: command not found

在询问that question之后,我正在编写一个JavaScript for Automation(JXA)脚本来自动执行在线数学作业收集。脚本终于完成了。

但是我需要通过Shell使用的程序之一,img2pdf确实不能使用doShellScript()来工作。这是出乎意料的,因为它在osascript中非常有效。

//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.
      }
    }
  }
}

我希望收件箱中所有包含“测试”和附件的主题的电子邮件都将按以下方式处理:

  1. 应该提取发件人的名称,并在HWBox文件夹中创建一个不存在该名称的文件夹。

  2. 应以MMDDYYYYHHMMSS格式提取电子邮件的时间戳,并在与发件人名称相对应的文件夹中创建具有该名称的文件夹。如果已经存在这样的时间戳,则应将其忽略。

  3. 应下载电子邮件的所有附件,并将其名称放入步骤2中时间戳生成的文件夹中。

  4. 如果附件超过1个,我认为它们都是图像。在这种情况下,img2pdf程序将通过Shell用于处理此类图像并将其转换为PDF。

实际上,第1-3步有效。另一方面,第4步不起作用。

1 个答案:

答案 0 :(得分:0)

好的,我想我现在明白问题所在。基本上doShellScript()实际上并不知道img2pdf的路径。使用完整路径后,该错误消失。