Last active
November 29, 2021 22:09
-
-
Save sharn25/1dcd7689ac3f838cd35143937da36530 to your computer and use it in GitHub Desktop.
Home Widget with random images from internet based upon bjayers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Variables used by Scriptable. | |
// These must be at the very top of the file. Do not edit. | |
// icon-color: teal; icon-glyph: magic; | |
// To use, add a parameter to the widget with a format of: image.png|padding-top|text-color|ture for online server image or false to load local image|Image filers eg: nature,blur | |
// The image should be placed in the iCloud Scriptable folder (case-sensitive). | |
// The padding-top spacing parameter moves the text down by a set amount. | |
// The text color parameter should be a hex value. | |
// For example, to use the image bkg_fall.PNG with a padding of 40 and a text color of red, | |
// the parameter should be typed as: bkg_fall.png|40|#ff0000|true|nature | |
// All parameters are required and separated with "|" | |
// Parameters allow different settings for multiple widget instances. | |
let widgetHello = new ListWidget(); | |
var today = new Date(); | |
let IsEventEnable = true; | |
let numberofevets = 2; | |
var widgetInputRAW = args.widgetParameter; | |
try { | |
widgetInputRAW.toString(); | |
} catch(e) { | |
throw new Error("Please long press the widget and add a parameter. Eg: Imagename.jpg|55|#f3f3f3|true|nature"); | |
} | |
var widgetInput = widgetInputRAW.toString(); | |
var inputArr = widgetInput.split("|"); | |
//Local or online | |
try { | |
var IsOnline = inputArr[3]; | |
} catch(e) { | |
throw new Error("Please long press the widget and add a parameter for selecting image from local or server."); | |
} | |
//Online search filters | |
var imgFilters = ""; | |
try{ | |
imgFilters = inputArr[4]; | |
}catch(e){ | |
console.log(e); | |
} | |
// iCloud file path | |
var scriptableFilePath = "/var/mobile/Library/Mobile Documents/iCloud~dk~simonbs~Scriptable/Documents/"; | |
var removeSpaces1 = inputArr[0].split(" "); // Remove spaces from file name | |
var removeSpaces2 = removeSpaces1.join(''); | |
var tempPath = removeSpaces2.split("."); | |
var backgroundImageURLRAW = scriptableFilePath + tempPath[0]; | |
var fm = FileManager.iCloud(); | |
var backgroundImageURL = scriptableFilePath + tempPath[0] + "."; | |
var backgroundImageURLInput = scriptableFilePath + removeSpaces2; | |
// For users having trouble with extensions | |
// Uses user-input file path is the file is found | |
// Checks for common file format extensions if the file is not found | |
if (fm.fileExists(backgroundImageURLInput) == false) { | |
var fileTypes = ['png', 'jpg', 'jpeg', 'tiff', 'webp', 'gif']; | |
fileTypes.forEach(function(item) { | |
if (fm.fileExists((backgroundImageURL + item.toLowerCase())) == true) { | |
backgroundImageURL = backgroundImageURLRAW + "." + item.toLowerCase(); | |
} else if (fm.fileExists((backgroundImageURL + item.toUpperCase())) == true) { | |
backgroundImageURL = backgroundImageURLRAW + "." + item.toUpperCase(); | |
} | |
}); | |
} else { | |
backgroundImageURL = scriptableFilePath + removeSpaces2; | |
} | |
var spacing = parseInt(inputArr[1]); | |
// Long-form days and months | |
var days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']; | |
var months = ['January','February','March','April','May','June','July','August','September','October','November','December']; | |
// Greetings arrays per time period. | |
var greetingsMorning = [ | |
'Good morning' | |
]; | |
var greetingsAfternoon = [ | |
'Good afternoon' | |
]; | |
var greetingsEvening = [ | |
'Good evening' | |
]; | |
var greetingsNight = [ | |
'Bedtime' | |
]; | |
var greetingsLateNight = [ | |
'Go to sleep!' | |
]; | |
// Holiday customization | |
var holidaysByKey = { | |
// month,week,day: datetext | |
"11,4,4": "Happy Thanksgiving!" | |
} | |
var holidaysByDate = { | |
// month,date: greeting | |
"1,1": "Happy " + (today.getFullYear()).toString() + "!", | |
"10,31": "Happy Halloween!", | |
"12,25": "Merry Christmas!" | |
} | |
var holidayKey = (today.getMonth() + 1).toString() + "," + (Math.ceil(today.getDate() / 7)).toString() + "," + (today.getDay()).toString(); | |
var holidayKeyDate = (today.getMonth() + 1).toString() + "," + (today.getDate()).toString(); | |
// Date Calculations | |
var weekday = days[ today.getDay() ]; | |
var month = months[ today.getMonth() ]; | |
var date = today.getDate(); | |
var hour = today.getHours(); | |
// Append ordinal suffix to date | |
function ordinalSuffix(input) { | |
if (input % 10 == 1 && date != 11) { | |
return input.toString() + "st"; | |
} else if (input % 10 == 2 && date != 12) { | |
return input.toString() + "nd"; | |
} else if (input % 10 == 3 && date != 13) { | |
return input.toString() + "rd"; | |
} else { | |
return input.toString() + "th"; | |
} | |
} | |
// Generate date string | |
var datefull = weekday + ", " + month + " " + ordinalSuffix(date); | |
// Support for multiple greetings per time period | |
function randomGreeting(greetingArray) { | |
return Math.floor(Math.random() * greetingArray.length); | |
} | |
var greeting = new String("Howdy.") | |
if (hour < 5 && hour >= 1) { // 1am - 5am | |
greeting = greetingsLateNight[randomGreeting(greetingsLateNight)]; | |
} else if (hour >= 23 || hour < 1) { // 11pm - 1am | |
greeting = greetingsNight[randomGreeting(greetingsNight)]; | |
} else if (hour < 12) { // Before noon (5am - 12pm) | |
greeting = greetingsMorning[randomGreeting(greetingsMorning)]; | |
} else if (hour >= 12 && hour <= 17) { // 12pm - 5pm | |
greeting = greetingsAfternoon[randomGreeting(greetingsAfternoon)]; | |
} else if (hour > 17 && hour < 23) { // 5pm - 11pm | |
greeting = greetingsEvening[randomGreeting(greetingsEvening)]; | |
} | |
// Overwrite greeting if calculated holiday | |
if (holidaysByKey[holidayKey]) { | |
greeting = holidaysByKey[holidayKey]; | |
} | |
// Overwrite all greetings if specific holiday | |
if (holidaysByDate[holidayKeyDate]) { | |
greeting = holidaysByDate[holidayKeyDate]; | |
} | |
// Try/catch for color input parameter | |
try { | |
inputArr[2].toString(); | |
} catch(e) { | |
throw new Error("Please long press the widget and add a parameter."); | |
} | |
//LoadImage | |
var servers = ["https://source.unsplash.com/random/640x480/?","https://picsum.photos/640/480?"]; | |
async function fetchDataImage() { | |
var number = randomGreeting(servers); | |
const url = servers[number]+imgFilters; | |
const request = new Request(url) | |
var res = await request.loadImage(); | |
return res; | |
} | |
let themeColor = new Color(inputArr[2].toString()); | |
//Load Events | |
widgetHello.addSpacer(5); | |
if(IsEventEnable){ | |
const cal = await CalendarEvent.today([]); | |
console.log(cal.length); | |
var count = 0; | |
for(const i of cal){ | |
if(count==numberofevets){break;} | |
widgetHello.addSpacer(5) | |
let title = widgetHello.addText(i.title); | |
title.font = Font.regularSystemFont(15); | |
title.textColor = themeColor | |
if(i.isAllDay){ | |
let date = widgetHello.addText("All Day"); | |
date.font = Font.regularSystemFont(10); | |
date.textColor = themeColor; | |
console.log(); | |
}else{ | |
let date = widgetHello.addText(formattime(i.startDate)); | |
date.font = Font.regularSystemFont(10); | |
date.textColor = themeColor; | |
} | |
count++; | |
spacing = spacing - 30; | |
} | |
if(cal.length>numberofevets){ | |
let ecount = cal.length-numberofevets; | |
widgetHello.addSpacer(5) | |
let more = widgetHello.addText(ecount + " more events"); | |
more.font = Font.regularSystemFont(12); | |
more.textColor = themeColor; | |
spacing = spacing - 17; | |
} | |
} | |
//time formater | |
function formattime(timems){ | |
let time = new DateFormatter(); | |
time.useNoDateStyle(); | |
time.useShortTimeStyle(); | |
let timestring = time.string(timems); | |
return timestring; | |
} | |
//Battery Render | |
function getBatteryLevel() { | |
const batteryLevel = Device.batteryLevel() | |
const batteryAscii = Math.round(batteryLevel * 100); | |
return batteryAscii + "%"; | |
} | |
/* --------------- */ | |
/* Assemble Widget */ | |
/* --------------- */ | |
//Wedget update time | |
/* | |
var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds(); | |
let update = widgetHello.addText("Last Updated " + time); | |
update.font = Font.regularSystemFont(10); | |
update.textColor = themeColor; | |
update.rightAlignText(); | |
*/ | |
//Top spacing | |
widgetHello.addSpacer(parseInt(spacing)); | |
// Battery Status | |
var battery = "" + getBatteryLevel(); | |
if(Device.isCharging() && !Device.isFullyCharged()){ | |
battery = battery + ", Charging..."; | |
} | |
if(Device.isFullyCharged()){ | |
battery = battery +", Fully Charged. Please unplug."; | |
} | |
let batterytext = widgetHello.addText(battery); | |
batterytext.font = Font.regularSystemFont(15); | |
batterytext.textColor = themeColor; | |
// Device Name label | |
let deviceName = widgetHello.addText(Device.name()); | |
deviceName.font = Font.regularSystemFont(15); | |
deviceName.textColor = themeColor; | |
// Greeting label | |
let hello = widgetHello.addText(greeting); | |
hello.font = Font.boldSystemFont(30); | |
hello.textColor = themeColor; | |
// Date label | |
let datetext = widgetHello.addText(datefull); | |
datetext.font = Font.regularSystemFont(15); | |
datetext.textColor = themeColor; | |
// Bottom Spacer | |
widgetHello.addSpacer(); | |
widgetHello.setPadding(15, 7, 10, 7) | |
// Background image | |
if(IsOnline=="true"){ | |
widgetHello.backgroundImage = await fetchDataImage(); | |
}else{ | |
widgetHello.backgroundImage = Image.fromFile(backgroundImageURL); | |
} | |
// Set widget | |
Script.setWidget(widgetHello); |
Yes, its possible. Code need to be change
Alright, thankyou
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yes, its possible. Code need to be change