Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2020
145
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.49 KB | None | 1 0
  1. // To use, add a parameter to the widget with a format of: image.png|padding-top|text-color
  2. // The image should be placed in the iCloud Scriptable folder (case-sensitive).
  3. // The padding-top spacing parameter moves the text down by a set amount.
  4. // The text color parameter should be a hex value.
  5.  
  6. // For example, to use the image bkg_fall.PNG with a padding of 40 and a text color of red,
  7. // the parameter should be typed as: bkg_fall.png|40|#ff0000
  8.  
  9. // All parameters are required and separated with "|"
  10.  
  11. // Parameters allow different settings for multiple widget instances.
  12.  
  13. let widgetHello = new ListWidget();
  14. var today = new Date();
  15.  
  16. var widgetInputRAW = args.widgetParameter;
  17.  
  18. try {
  19. widgetInputRAW.toString();
  20. } catch(e) {
  21. throw new Error("Please long press the widget and add a parameter.");
  22. }
  23.  
  24. var widgetInput = widgetInputRAW.toString();
  25.  
  26. var inputArr = widgetInput.split("|");
  27.  
  28. // iCloud file path
  29. var scriptableFilePath = "/var/mobile/Library/Mobile Documents/iCloud~dk~simonbs~Scriptable/Documents/";
  30. var removeSpaces1 = inputArr[0].split(" "); // Remove spaces from file name
  31. var removeSpaces2 = removeSpaces1.join('');
  32. var tempPath = removeSpaces2.split(".");
  33. var backgroundImageURLRAW = scriptableFilePath + tempPath[0];
  34.  
  35. var fm = FileManager.iCloud();
  36. var backgroundImageURL = scriptableFilePath + tempPath[0] + ".";
  37. var backgroundImageURLInput = scriptableFilePath + removeSpaces2;
  38.  
  39. // For users having trouble with extensions
  40. // Uses user-input file path is the file is found
  41. // Checks for common file format extensions if the file is not found
  42. if (fm.fileExists(backgroundImageURLInput) == false) {
  43. var fileTypes = ['png', 'jpg', 'jpeg', 'tiff', 'webp', 'gif'];
  44.  
  45. fileTypes.forEach(function(item) {
  46. if (fm.fileExists((backgroundImageURL + item.toLowerCase())) == true) {
  47. backgroundImageURL = backgroundImageURLRAW + "." + item.toLowerCase();
  48. } else if (fm.fileExists((backgroundImageURL + item.toUpperCase())) == true) {
  49. backgroundImageURL = backgroundImageURLRAW + "." + item.toUpperCase();
  50. }
  51. });
  52. } else {
  53. backgroundImageURL = scriptableFilePath + removeSpaces2;
  54. }
  55.  
  56. var spacing = parseInt(inputArr[1]);
  57.  
  58. // Long-form days and months
  59. var days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
  60. var months = ['January','February','March','April','May','June','July','August','September','October','November','December'];
  61.  
  62. // Greetings arrays per time period.
  63. var greetingsMorning = [
  64. 'Good morning.'
  65. ];
  66. var greetingsAfternoon = [
  67. 'Good afternoon.'
  68. ];
  69. var greetingsEvening = [
  70. 'Good evening.'
  71. ];
  72. var greetingsNight = [
  73. 'Bedtime.'
  74. ];
  75. var greetingsLateNight = [
  76. 'Go to sleep!'
  77. ];
  78.  
  79. // Holiday customization
  80. var holidaysByKey = {
  81. // month,week,day: datetext
  82. "11,4,4": "Happy Thanksgiving!"
  83. }
  84.  
  85. var holidaysByDate = {
  86. // month,date: greeting
  87. "1,1": "Happy " + (today.getFullYear()).toString() + "!",
  88. "10,31": "Happy Halloween!",
  89. "12,25": "Merry Christmas!"
  90. }
  91.  
  92. var holidayKey = (today.getMonth() + 1).toString() + "," + (Math.ceil(today.getDate() / 7)).toString() + "," + (today.getDay()).toString();
  93.  
  94. var holidayKeyDate = (today.getMonth() + 1).toString() + "," + (today.getDate()).toString();
  95.  
  96. // Date Calculations
  97. var weekday = days[ today.getDay() ];
  98. var month = months[ today.getMonth() ];
  99. var date = today.getDate();
  100. var hour = today.getHours();
  101.  
  102. // Append ordinal suffix to date
  103. function ordinalSuffix(input) {
  104. if (input % 10 == 1 && date != 11) {
  105. return input.toString() + "st";
  106. } else if (input % 10 == 2 && date != 12) {
  107. return input.toString() + "nd";
  108. } else if (input % 10 == 3 && date != 13) {
  109. return input.toString() + "rd";
  110. } else {
  111. return input.toString() + "th";
  112. }
  113. }
  114.  
  115. // Generate date string
  116. var datefull = weekday + ", " + month + " " + ordinalSuffix(date);
  117.  
  118. // Support for multiple greetings per time period
  119. function randomGreeting(greetingArray) {
  120. return Math.floor(Math.random() * greetingArray.length);
  121. }
  122.  
  123. var greeting = new String("Howdy.")
  124. if (hour < 5 && hour >= 1) { // 1am - 5am
  125. greeting = greetingsLateNight[randomGreeting(greetingsLateNight)];
  126. } else if (hour >= 23 || hour < 1) { // 11pm - 1am
  127. greeting = greetingsNight[randomGreeting(greetingsNight)];
  128. } else if (hour < 12) { // Before noon (5am - 12pm)
  129. greeting = greetingsMorning[randomGreeting(greetingsMorning)];
  130. } else if (hour >= 12 && hour <= 17) { // 12pm - 5pm
  131. greeting = greetingsAfternoon[randomGreeting(greetingsAfternoon)];
  132. } else if (hour > 17 && hour < 23) { // 5pm - 11pm
  133. greeting = greetingsEvening[randomGreeting(greetingsEvening)];
  134. }
  135.  
  136. // Overwrite greeting if calculated holiday
  137. if (holidaysByKey[holidayKey]) {
  138. greeting = holidaysByKey[holidayKey];
  139. }
  140.  
  141. // Overwrite all greetings if specific holiday
  142. if (holidaysByDate[holidayKeyDate]) {
  143. greeting = holidaysByDate[holidayKeyDate];
  144. }
  145.  
  146. // Try/catch for color input parameter
  147. try {
  148. inputArr[2].toString();
  149. } catch(e) {
  150. throw new Error("Please long press the widget and add a parameter.");
  151. }
  152.  
  153. let themeColor = new Color(inputArr[2].toString());
  154.  
  155. /* --------------- */
  156. /* Assemble Widget */
  157. /* --------------- */
  158.  
  159. //Top spacing
  160. widgetHello.addSpacer(parseInt(spacing));
  161.  
  162. // Battery display
  163. // let batteryText = widgetHello.addText("🔋 = " + Math.floor(Device.batteryLevel()*100).toString() + "%");
  164. // batteryText.textColor = new Color("#6ef2ae")
  165. // batteryText.font = new Font("Menlo", 14);
  166. // batteryText.rightAlignText();
  167.  
  168. const batteryLine = widgetHello.addText('[🔋]' + " " + renderBattery());
  169.  
  170. batteryLine.textColor = new Color("#6ef2ae"); batteryLine.font = new Font("Menlo", 14); batteryLine.rightAlignText();
  171.  
  172. function renderBattery() {
  173. const batteryLevel = Device.batteryLevel();
  174. const juice = "#".repeat(Math.floor(batteryLevel * 8));
  175. const used = ".".repeat(8 - juice.length)
  176. const batteryAscii = "[" + juice + used + "] " + Math.round(batteryLevel * 100) + "%";
  177. return batteryAscii;
  178. }
  179.  
  180. // Greeting label
  181. let hello = widgetHello.addText(greeting);
  182. hello.font = Font.boldSystemFont(42);
  183. hello.textColor = themeColor;
  184. hello.centerAlignText();
  185.  
  186. // Date label
  187. let datetext = widgetHello.addText(datefull);
  188. datetext.font = Font.regularSystemFont(16);
  189. datetext.textColor = themeColor;
  190. datetext.centerAlignText();
  191.  
  192.  
  193. // Bottom Spacer
  194. widgetHello.addSpacer();
  195. widgetHello.setPadding(15, 7, 10, 0)
  196.  
  197. // Background image
  198. widgetHello.backgroundImage = Image.fromFile(backgroundImageURL);
  199.  
  200. // Set widget
  201. Script.setWidget(widgetHello);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement