Scriptableで英文ノートをiPhoneのガジェットに表示する
https://gyazo.com/0ee81fcce89d913932c7de6de3be0ff1
code:sample.js
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: deep-green; icon-glyph: sticky-note;
/* #################################
simple sticky note widget
-config:
---background color
------green, red, yellow, blue, #hex ---text size
------small, medium, large
-sparate config and note by: "."
-text note:
---if you want to add more than one poin
---use ", " to sparate each poin
usage: (config).(note)
e.g : medium green. note1, note2, ...
or just note: note1, note2, ...
made by @morinagapltynm
################################# */
const iCloud = FileManager.iCloud();
// **TODO** Add the Vault folder to the File Bookmark in Scriptable settings.
const vaultPath = iCloud.bookmarkedPath('history.json');
if (!iCloud.isFileDownloaded(vaultPath)) {
console.log('Downloading daily note...');
await iCloud.downloadFileFromiCloud(vaultPath)
console.log('Daily note has been downloaded.');
}
await iCloud.downloadFileFromiCloud(vaultPath)
const temptest = iCloud.readString(vaultPath);
const parsedData = JSON.parse(temptest);
let count = 0
//ここでjsonを絞り込むこともできる
const filteredData = parsedData.filter(item =>{
if ("address" in item){
return item
}
}
})
const randNum = Math.floor(Math.random() * Object.keys(filteredData).length)
let placeholder = "small yellow. " + firstLine
let input = (args.widgetParameter == null) ? placeholder : args.widgetParameter
let define = input.split(/\.\s(.*)|\.(.*)/)
let noteColor = Color.black()
// fontSize
if (conf.match(/small/i)){
fontSize = 16
}else if (conf.match(/medium/i)){
fontSize = 32
}else if (conf.match(/large/i)){
fontSize = 45
}else{
//default
fontSize = 16
}
// bgColor default is random
color = {
"green": "C5EA9C",
"yellow": "FFFFA1",
"blue": "B1DAE4",
"red": "FFC2E4",
"white": "F6F6F7",
"black": "191919"
}
if (conf.match(/green/i)){
bgColor = new Color(color.green)
}else if (conf.match(/yellow/i)){
bgColor = new Color(color.yellow)
}else if (conf.match(/blue/i)){
bgColor = new Color(color.blue)
}else if (conf.match(/red/i)){
bgColor = new Color(color.red)
}else if (conf.match(/#/)){
//custom color
let prefix = conf.indexOf("#")
let ctmClr = conf.slice(prefix + 1, prefix + 7)
bgColor = new Color(ctmClr)
}else if (conf.match(/acent/i)){
//iOS acent(not working yet due scriptable bug) vote this at automators.fm or dm simon :))
if(Device.isUsingDarkAppearance()){
bgColor = new Color(color.black)
noteColor = new Color(color.white)
}else{
bgColor = new Color(color.white)
noteColor = new Color(color.black)
}
}else{
//random as default
let hex = Object.values(color)
}
// note
if (define.length == 1){
note = define0.replace(/, |,/g, "\n• ") }else{
const notebody = define.slice(1, -1)
notebodyText = notebody.join("")
note = define1.replace(/, |,/g, "\n• ") }
// draw widget
let w = new ListWidget()
//header settings
const header = w.addStack();
header.url = URLScheme.forRunningScript();
header.addText(metaLine).font = Font.regularSystemFont(fontSize);
header.addSpacer();
let reloadIcon = header.addImage(SFSymbol.named("arrow.2.squarepath").image);
reloadIcon.imageSize = new Size(20, 20);
reloadIcon.tintColor = Color.blue();
let text = (note.match("\n") === null) ? w.addText(notebodyText) : w.addText(notebodyText)
text.font = Font.mediumMonospacedSystemFont(fontSize)
text.textColor = noteColor
w.backgroundColor = bgColor
// run widget
if(!config.runsInWidget){
w.presentMedium()
}
Script.setWidget(w)
Script.complete()