Guest User

Untitled

a guest
Nov 11th, 2020
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.71 KB | None | 0 0
  1. {
  2. "always_run_in_app" : false,
  3. "icon" : {
  4. "color" : "yellow",
  5. "glyph" : "magic"
  6. },
  7. "name" : "Calendar",
  8. "script" : "\/\/ Variables used by Scriptable.\n\/\/ These must be at the very top of the file. Do not edit.\n\/\/ icon-color: red; icon-glyph: calendar-alt;\n\n\/\/ This widget was created by Max Zeryck @mzeryck\n\n\/\/ Note: before using this script in a widget, change const TEST_MODE to true and run it in the Scriptable app.\n\/\/ The app will prompt you to give calendar access, and then show a preview of the widget.\n\/\/ Make sure to change it back to const TEST_MODE = false prior to adding it to a widget. Happy coding!\n\nconst IMAGE_SOURCE = \"Unsplash\"\nconst TERMS = \"nature\"\n\nconst FORCE_IMAGE_UPDATE = true\nconst TEST_MODE = false\n\n\/\/ Store current datetime\nconst date = new Date()\n\n\/\/ If we're running the script normally, go to the Calendar.\nif (!config.runsInWidget && !TEST_MODE) {\n const appleDate = new Date('2001\/01\/01')\n const timestamp = (date.getTime() - appleDate.getTime()) \/ 1000\n const callback = new CallbackURL(\"calshow:\"+timestamp)\n callback.open()\n Script.complete()\n \n\/\/ Otherwise, create the widget. \n} else {\n\n let widget = new ListWidget()\n \n \/\/ Format the date info\n let df = new DateFormatter()\n df.dateFormat = \"EEEE\"\n let dayOfWeek = widget.addText(df.string(date).toUpperCase())\n let dateNumber = widget.addText(date.getDate().toString())\n dayOfWeek.font = Font.semiboldSystemFont(13)\n dateNumber.font = Font.lightSystemFont(34)\n \n \/\/ Find future events that aren't all day and aren't canceled\n const events = await CalendarEvent.today([])\n let futureEvents = []\n for (const event of events) {\n if (event.startDate.getTime() > date.getTime() && !event.isAllDay && !event.title.startsWith(\"Canceled:\")) {\n futureEvents.push(event) \n }\n }\n \n \/\/ If there is at least one future event today\n if (futureEvents.length != 0) {\n\n dayOfWeek.textColor = Color.red()\n widget.addSpacer()\n \n let titleOne = widget.addText(futureEvents[0].title) \n titleOne.font = Font.mediumSystemFont(14)\n \n widget.addSpacer(7)\n \n let timeOne = widget.addText(formatTime(futureEvents[0].startDate))\n timeOne.font = Font.regularSystemFont(14)\n timeOne.textColor = Color.gray()\n \n \/\/ If we have multiple future events, show the following one\n if (futureEvents.length > 1) {\n \n \/\/ We only have room for single-line event names\n titleOne.lineLimit = 1\n \n widget.addSpacer(12)\n \n let titleTwo = widget.addText(futureEvents[1].title) \n titleTwo.font = Font.mediumSystemFont(14)\n titleTwo.lineLimit = 1\n \n widget.addSpacer(7)\n \n let timeTwo = widget.addText(formatTime(futureEvents[1].startDate))\n timeTwo.font = Font.regularSystemFont(14)\n timeTwo.textColor = Color.gray()\n }\n \n \/\/ If there are no future events today\n } else {\n dayOfWeek.textColor = Color.white()\n dateNumber.textColor = Color.white()\n \n let files = FileManager.local()\n const path = files.joinPath(files.documentsDirectory(), \"mz_calendar_widget.jpg\")\n const modificationDate = files.modificationDate(path) \n \n \/\/ Download image if it doesn't exist, wasn't created today, or update is forced\n if (!modificationDate || !sameDay(modificationDate,date) || FORCE_IMAGE_UPDATE) {\n try {\n let img = await provideImage(IMAGE_SOURCE,TERMS)\n files.writeImage(path,img)\n widget.backgroundImage = img\n } catch { \n widget.backgroundImage = files.readImage(path)\n }\n } else {\n widget.backgroundImage = files.readImage(path) \n }\n \n \/\/ Add overlay to image\n let gradient = new LinearGradient()\n gradient.colors = [new Color(\"#000000\",0.5), new Color(\"#000000\",0)]\n gradient.locations = [0, 0.5]\n widget.backgroundGradient = gradient\n\n widget.addSpacer()\n }\n \n \/\/ Finalize widget settings\n widget.setPadding(16,16,16,0)\n widget.spacing = -3\n \n Script.setWidget(widget)\n widget.presentSmall()\n Script.complete() \n}\n\n\/\/ Helper function to interpret sources and terms\nasync function provideImage(source,terms) {\n \n if (source == \"Bing\") {\n const url = \"http:\/\/www.bing.com\/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=en-US\"\n const req = new Request(url)\n const json = await req.loadJSON()\n const imgURL = \"http:\/\/bing.com\" + json.images[0].url\n const img = await downloadImage(imgURL)\n const rect = new Rect(-78,0,356,200)\n return cropImage(img, rect)\n \n } else if (source == \"Unsplash\") {\n const img = await downloadImage(\"https:\/\/source.unsplash.com\/featured\/500x500\/?\"+terms)\n return img\n } \n\n}\n\n\/\/ Helper function to download images\nasync function downloadImage(url) {\n const req = new Request(url)\n return await req.loadImage() \n}\n\n\/\/ Crop an image into a rect\nfunction cropImage(img,rect) {\n \n let draw = new DrawContext()\n draw.respectScreenScale = true\n \n draw.drawImageInRect(img,rect) \n return draw.getImage()\n}\n\n\/\/ Formats the times under each event\nfunction formatTime(date) {\n let df = new DateFormatter()\n df.useNoDateStyle()\n df.useShortTimeStyle()\n return df.string(date)\n}\n\n\/\/ Determines if two dates occur on the same day\nfunction sameDay(d1, d2) {\n return d1.getFullYear() === d2.getFullYear() &&\n d1.getMonth() === d2.getMonth() &&\n d1.getDate() === d2.getDate()\n}",
  9. "share_sheet_inputs" : [
  10.  
  11. ]
  12. }
Advertisement
Add Comment
Please, Sign In to add comment