L06 - 課程練習(小算盤)

以下程式碼參考

let buttons = {
  num0 : 'crop-0d98b729fc88bb831d6013fa837ce40a151592cfb71ef6f6097f45197d972e66.png',
  num1 : 'crop-0bd1f041f9ece56741765a9fb6db77f2e742c1c9463f395be63e84206d3d9a15.png',
  num2 : 'crop-48d1ba469edbc4587bf844bb43f78add3eebecf66b69ec9a79532caa9014b7b3.png',
  num3 : 'crop-1af177b3fc3ab18f9005e69d69c07603919082ecf318c61b8329223e846dcd4a.png',
  num4 : 'crop-01f36bc2ca7952b4a721dfb71d01c5212e90da561a03a59e4491c6a22df8d3d8.png',
  num5 : 'crop-6e6bc4f0c678c8f6005a98f6b6230c525831fa5993d04e0be6ca734fadfa4e21.png',
  num6 : 'crop-b6a1b5a97209b127f9580efb38e5922951c625661f9f4b34cd4715d05b8002a4.png',
  num7 : 'crop-44fbc23bf827862d6a6b6f489409ec4aa8281023ef6b0f5e648adb2894d8901c.png',
  num8 : 'crop-29db9da11fcb0ddae0650e189c1e0e2fbeeb88c16163e75cce9cba715252f38a.png',
  num9 : 'crop-387aebee2afdc2a06e22ac1e186fd299f6fcbaf88b210e0e18eef562f7b08621.png',
  plus : 'crop-b5a03ebc7dec40468e0171e3de2b59b0ac9263bb1bc30c323415320cf9cfc239.png',
  multiple : 'crop-cc3087b4da2efc805f7ee15e2b640a07cdd06947575055bffab5301d44b36cf3.png',
  minus : 'crop-d7afd97d5b2f2d4d977c2955b5ea5d0214c5fc40f8f4bed2b4139a8b800cc31f.png',
  divide : 'crop-296239effe05b3198f284c1bb0055ae23c69f703a20f208b910177116cd4d368.png',
  equal : 'crop-d03490ebd0df0037e2168c12e04ee3fdf42bb8063b2a2c4704f822ee4a0b6d9e.png',
  ac : 'crop-d194a7b2479f6e8e40a8eacc08cd9e30c1b4a6f8ae53cb40ec6966669152d412.png',
  point : 'crop-731e0de172b3b8244578a5b12ef9c8910f78e30f293bcf9b7440e5a13259d6f0.png'
}


async function pressButton(btn){
  switch(btn){
    case '0' :
      await api.clickCrop(buttons['num0'], 20, 20);
      break;
    case '1' :
      await api.clickCrop(buttons['num1'], 20, 20);
      break;
    case '2' :
      await api.clickCrop(buttons['num2'], 20, 20);
      break;
    case '3' :
      await api.clickCrop(buttons['num3'], 20, 20);
      break;
    case '4' :
      await api.clickCrop(buttons['num4'], 20, 20);
      break;
    case '5' :
      await api.clickCrop(buttons['num5'], 20, 20);
      break;
    case '6' :
      await api.clickCrop(buttons['num6'], 20, 20);
      break;
    case '7' :
      await api.clickCrop(buttons['num7'], 20, 20);
      break;
    case '8' :
      await api.clickCrop(buttons['num8'], 20, 20);
      break;
    case '9' :
      await api.clickCrop(buttons['num9'], 20, 20);
      break;
    case '+' :
      await api.clickCrop(buttons['plus'], 20, 20);
      break;
    case '-' :
      await api.clickCrop(buttons['minus'], 20, 20);
      break;
    case '*' :
      await api.clickCrop(buttons['multiple'], 20, 20);
      break;
    case '/' :
      await api.clickCrop(buttons['divide'], 20, 20);
      break;
    case '.' :
      await api.clickCrop(buttons['point'], 20, 20);
      break;
  }
}

// open calculator
await api.shell.openPath('/System/Applications/Calculator.app')

// wait for ac button
api.found = await api.screen.waitFor(buttons['ac'], 10000, {confidence: 0.99})
if (!api.found) throw 'waitFor not found'

// read csv table
let data = await api.readCSV('table.csv', ',', 0)

// loop calculation
for(let i = 0; i<data.length; i++){
  let number1 = data[i]['number1']
  let operator = data[i]['operator']
  let number2 = data[i]['number2']

  // click number1
  for(let b1 = 0; b1 < number1.length; b1++){
    await pressButton(`${number1[b1]}`);
    await api.sleep(1000);
  }
  
  // click operator
  await pressButton(`${operator}`)
  await api.sleep(1000);

  // click number2
  for(let b2 = 0; b2 < number2.length; b2++){
    await pressButton(`${number2[b2]}`);
    await api.sleep(1000);
  }

  // click =
  await api.clickCrop(buttons['equal'], 20, 20)
  await api.sleep(1000);
  
  // copy result(Mac 為 Super + C/ Windows 為 Ctrl + C)
  await api.keyboard.type(api.key.LeftSuper, api.key.C);
  await api.sleep(1000);

  // read clipboard
  let result = api.clipboard.readText()

  // output to data
  data[i]['result'] = result
  await api.writeCSV('result.csv', data, ['number1', 'operator', 'number2', 'result'])

  // click C
  await api.clickCrop('crop-fce708319a21a174e04ff50985667ebf6bdbe80861897bde2d4cb0304d24419f.png', null, null, {confidence: 0.99})
}