描述
可通过设置桌面的“Scriptable”小组件的parameter参数选择固定在某条“启发”。
- 运行组件后会看到“启发”列表,最左侧的数字是它的下标
- 在parameter参数设置中填写这个下标即可。
脚本
const fixedIndex =Number(args.widgetParameter) || -1; // 固定在某条启发,填写序号。(-1为随机)
// 启发语录列表
const textList = [
'选我所爱,爱我所选。',
'\n让改变适合你现在的生活和需要,而不是让你的生活和需要围绕着改变去进行。\n',
'此生理想、近期规划、今日功课。',
'流水不争先,争的是滔滔不绝。',
'既往不恋,当下不杂,未来不迎。',
];
/*-------上面是配置区域-------*/
// 创建小组件
const widget = new ListWidget();
if(config.runsInWidget){
const index=fixedIndex>=0?fixedIndex:Math.floor((Math.random()*textList.length));
let textItem=textList[index];
createWidget(textItem);
return;
}
// FIXME: 非常魔幻,明明在小组件环境竟然还能执行下来。
const table = new UITable();
textList.forEach((textItem,index)=>{
const row = new UITableRow();
const textCell = row.addButton(index+". "+textItem.replace(/\n/ig,''));
row.height = 30;
textCell.titleFont = Font.boldSystemFont(16);
textCell.onTap=function(){
console.log(`该启发的行号是:${index}`);
createWidget(textItem); // tips: 运行在app时无法去创建修改桌面的小组件
// sendNotification('该启发的行号是:', String(index));
// widget.presentMedium() // 预览小组件
}
table.addRow(row);
})
table.present();
// 创建桌面小组件
function createWidget(textItem){
let text;
// 设置语录样式
text = widget.addText(textItem);
text.textColor = new Color("#ffffff");
text.font= new Font('Georgia-BoldItalic',26)
text.minimumScaleFactor=0.5;
textItem.length>10?text.leftAlignText():text.centerAlignText();
// 添加渐变色背景
const gradient = new LinearGradient();
gradient.locations = [0, 1];
gradient.colors = [new Color("#333333"), new Color("#111111")];
widget.backgroundGradient = gradient;
Script.setWidget(widget)
}
// 发送通知
function sendNotification(title,body) {
const notification = new Notification();
notification.title = title;
notification.body = body;
notification.sound = "accept";
notification.schedule();
}