Skip to content

Instantly share code, notes, and snippets.

@CodeByAidan
Last active April 25, 2023 22:09
Show Gist options
  • Select an option

  • Save CodeByAidan/8afad274789bc43f7f9c1f4aef75448b to your computer and use it in GitHub Desktop.

Select an option

Save CodeByAidan/8afad274789bc43f7f9c1f4aef75448b to your computer and use it in GitHub Desktop.
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: orange; icon-glyph: sun;
// Make a scriptable that will show the current weather and the location of the user
// Get the location of the user
let location = await Location.current();
console.log(location);
// Get the weather of the user
async function getWeather() {
// using https://api.weather.gov/points/{latitude},{longitude}
let url =
"https://api.weather.gov/points/" +
location.latitude +
"," +
location.longitude;
let req = new Request(url);
let json = await req.loadJSON();
let forecast = json.properties.forecast;
let forecastReq = new Request(forecast);
let forecastJson = await forecastReq.loadJSON();
let currentWeather = forecastJson.properties.periods[0];
return currentWeather;
}
let weather = await getWeather();
console.log(weather);
//Configuring --> gradient color profiles
//change colors hex codes if you wish
let darkMode = true; //true to enable dark mode
//for best results, use it with ios dark mode turned on)
let gradient = new LinearGradient();
gradient.locations = [0, 1];
if (darkMode) {
gradient.colors = [new Color("2D2E30"), new Color("2C2C2E")];
} else {
gradient.colors = [new Color("FFFFFF"), new Color("FFFFFF")];
}
//End of configuration
//MAIN
//DO NOT touch anything unless you know what you are doing
let widget = await createWidget();
if (config.runsInWidget) {
Script.setWidget(widget);
Script.complete();
} else {
widget.presentSmall();
}
async function createWidget() {
let widget = new ListWidget();
widget.backgroundGradient = gradient;
widget.setPadding(20, 20, 20, 20);
let weatherLine = widget.addText("Weather: " + weather);
weatherLine.leftAlignText();
weatherLine.Color = Color.white();
weatherLine.font = Font.mediumMonospacedSystemFont(13);
widget.addSpacer(25);
let locationText = widget.addText(JSON.stringify(location));
locationText.leftAlignText();
locationText.font = Font.mediumMonospacedSystemFont(8);
return widget;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment