描述

可通过设置桌面的“Scriptable”小组件的parameter参数选择固定在某条“启发”。

  • 运行组件后会看到“启发”列表,最左侧的数字是它的下标
  • 在parameter参数设置中填写这个下标即可。

脚本

  1. const fixedIndex =Number(args.widgetParameter) || -1; // 固定在某条启发,填写序号。(-1为随机)
  2. // 启发语录列表
  3. const textList = [
  4. '选我所爱,爱我所选。',
  5. '\n让改变适合你现在的生活和需要,而不是让你的生活和需要围绕着改变去进行。\n',
  6. '此生理想、近期规划、今日功课。',
  7. '流水不争先,争的是滔滔不绝。',
  8. '既往不恋,当下不杂,未来不迎。',
  9. ];
  10. /*-------上面是配置区域-------*/
  11. // 创建小组件
  12. const widget = new ListWidget();
  13. if(config.runsInWidget){
  14. const index=fixedIndex>=0?fixedIndex:Math.floor((Math.random()*textList.length));
  15. let textItem=textList[index];
  16. createWidget(textItem);
  17. return;
  18. }
  19. // FIXME: 非常魔幻,明明在小组件环境竟然还能执行下来。
  20. const table = new UITable();
  21. textList.forEach((textItem,index)=>{
  22. const row = new UITableRow();
  23. const textCell = row.addButton(index+". "+textItem.replace(/\n/ig,''));
  24. row.height = 30;
  25. textCell.titleFont = Font.boldSystemFont(16);
  26. textCell.onTap=function(){
  27. console.log(`该启发的行号是:${index}`);
  28. createWidget(textItem); // tips: 运行在app时无法去创建修改桌面的小组件
  29. // sendNotification('该启发的行号是:', String(index));
  30. // widget.presentMedium() // 预览小组件
  31. }
  32. table.addRow(row);
  33. })
  34. table.present();
  35. // 创建桌面小组件
  36. function createWidget(textItem){
  37. let text;
  38. // 设置语录样式
  39. text = widget.addText(textItem);
  40. text.textColor = new Color("#ffffff");
  41. text.font= new Font('Georgia-BoldItalic',26)
  42. text.minimumScaleFactor=0.5;
  43. textItem.length>10?text.leftAlignText():text.centerAlignText();
  44. // 添加渐变色背景
  45. const gradient = new LinearGradient();
  46. gradient.locations = [0, 1];
  47. gradient.colors = [new Color("#333333"), new Color("#111111")];
  48. widget.backgroundGradient = gradient;
  49. Script.setWidget(widget)
  50. }
  51. // 发送通知
  52. function sendNotification(title,body) {
  53. const notification = new Notification();
  54. notification.title = title;
  55. notification.body = body;
  56. notification.sound = "accept";
  57. notification.schedule();
  58. }