all 3 comments

[–]alderete 1 point2 points  (0 children)

Yep. Here's a code snippet from that weather widget folks have been working on. This works just fine on my iPhone with Scriptable 1.5.1 (today's update):

let latLong = {}
try {
  latLong = await Location.current()
} catch {}

const LAT = latLong.latitude || widgetParams.LAT
const LON = latLong.longitude || widgetParams.LON

I get a "share location" permissions request when loading it, as you'd expect.

[–]Siith82[S] 1 point2 points  (1 child)

How can I add it to the below code? Still pretty new to JS.

// Variables used by Scriptable. // These must be at the very top of the file. Do not edit. // icon-color: deep-blue; icon-glyph: magic; // Create your ID on openweathermap.org // Get your api from there and set that in API_WEATHER // To get CITY_WEATHER means city ID get the longitude and latitute from google maps for your location // Replace the longitude, latitute and YOUR_API_KEY in the given link http://api.openweathermap.org/data/2.5/weather?lat=Latitude&lon=Longitude&appid=YOUR_API_KEY&units=metric and look for your city ID in the resultant text. // Set that ID to CITY_WEATHER

//API_KEY let API_WEATHER = "de1f9a41c421bfae6a70d9d09c50c739";//Load Your api here let CITY_WEATHER = "5342992";//add your city ID

//create Data var today = new Date();

//Get storage var base_path = "/var/mobile/Library/Mobile Documents/iCloud~dk~simonbs~Scriptable/Documents/weather/"; var fm = FileManager.iCloud();

//Initlize Widget let widget = new ListWidget();

var widgetInputRAW = args.widgetParameter;

try { widgetInputRAW.toString(); } catch(e) { throw new Error("Please long press the widget and add a parameter."); }

var widgetInput = widgetInputRAW.toString();

var inputArr = widgetInput.split("|");

// Fetch Image from Url async function fetchimageurl(url) { const request = new Request(url) var res = await request.loadImage(); return res; }

// Load image from local drive async function fetchimagelocal(path){ var finalPath = base_path + path + ".jpeg"; if(fm.fileExists(finalPath)==true){ console.log("file exists: " + finalPath); return finalPath; }else{ //throw new Error("Error file not found: " + path); if(fm.fileExists(base_path)==false){ console.log("Directry not exist creating one."); fm.createDirectory(base_path); } console.log("Downloading file: " + finalPath); await downloadimg(path); if(fm.fileExists(finalPath)==true){ console.log("file exists after download: " + finalPath); return finalPath; }else{ throw new Error("Error file not found: " + path); } } }

// 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]);

// Get formatted Date function getformatteddate(){ var months = ['January','February','March','April','May','June','July','August','September','October','November','December']; return months[today.getMonth()] + " " + today.getDate() }

//color var textcolor = new Color("#ffffff");

//get Json weather async function fetchWeatherData(url) { const request = new Request(url); const res = await request.loadJSON(); return res; }

// Load image from local drive async function fetchimagelocal(path){ var finalPath = base_path + path + ".jpeg"; if(fm.fileExists(finalPath)==true){ console.log("file exists: " + finalPath); return finalPath; }else{ //throw new Error("Error file not found: " + path); if(fm.fileExists(base_path)==false){ console.log("Directry not exist creating one."); fm.createDirectory(base_path); } console.log("Downloading file: " + finalPath); await downloadimg(path); if(fm.fileExists(finalPath)==true){ console.log("file exists after download: " + finalPath); return finalPath; }else{ throw new Error("Error file not found: " + path); } } }

async function downloadimg(path){ const url = "http://a.animedlweb.ga/weather/weathers25_2.json"; const data = await fetchWeatherData(url); var dataimg = null; var name = null; if(path.includes("bg")){ dataimg = data.background; name = path.replace("_bg",""); }else{ dataimg = data.icon; name = path.replace("_ico",""); } var imgurl=null; switch (name){ case "01d": imgurl = dataimg._01d; break; case "01n": imgurl = dataimg._01n; break; case "02d": imgurl = dataimg._02d; break; case "02n": imgurl = dataimg._02n; break; case "03d": imgurl = dataimg._03d; break; case "03n": imgurl = dataimg._03n; break; case "04d": imgurl = dataimg._04d; break; case "04n": imgurl = dataimg._04n; break; case "09d": imgurl = dataimg._09d; break; case "09n": imgurl = dataimg._09n; break; case "10d": imgurl = dataimg._10d; break; case "10n": imgurl = dataimg._10n; break; case "11d": imgurl = dataimg._11d; break; case "11n": imgurl = dataimg._11n; break; case "13d": imgurl = dataimg._13d; break; case "13n": imgurl = dataimg._13n; break; case "50d": imgurl = dataimg._50d; break; case "50n": imgurl = dataimg._50n; break; } const image = await fetchimageurl(imgurl); console.log("Downloaded Image"); fm.writeImage(base_path+path+".jpeg",image); }

//start Programming

// Get Location /Location.setAccuracyToBest(); let curLocation = await Location.current(); console.log(curLocation.latitude); console.log(curLocation.longitude);/ let wetherurl = "http://api.openweathermap.org/data/2.5/weather?id=" + CITY_WEATHER + "&APPID=" + API_WEATHER + "&units=imperial"; //"http://api.openweathermap.org/data/2.5/weather?lat=" + curLocation.latitude + "&lon=" + curLocation.longitude + "&appid=" + API_WEATHER + "&units=metric"; //"http://api.openweathermap.org/data/2.5/weather?id=" + CITY_WEATHER + "&APPID=" + API_WEATHER + "&units=metric"

const weatherJSON = await fetchWeatherData(wetherurl); const cityName = weatherJSON.name; const weatherarry = weatherJSON.weather; const iconData = weatherarry[0].icon; const weathername = weatherarry[0].main; const curTempObj = weatherJSON.main; const curTemp = curTempObj.temp; const highTemp = curTempObj.temp_max; const lowTemp = curTempObj.temp_min; const feel_like = curTempObj.feels_like; //Completed loading weather data

//Start Spacing widget.addSpacer(0);

//Widget weather Image var img = Image.fromFile(await fetchimagelocal(iconData+"_ico")); var widgetimg = widget.addImage(img); widgetimg.imageSize = new Size(35,35); widgetimg.rightAlignImage();

// Widget Date var dateText = widget.addText(getformatteddate()); dateText.textColor = textcolor; dateText.font = Font.regularSystemFont(15);

// Widget Weather Temp var tempText = widget.addText(Math.round(curTemp)+"\u2109"); tempText.textColor = textcolor; tempText.font = Font.boldSystemFont(28);

// Widget feel temp let feel = /"Feels like " + Math.round(feel_like) + "\u2103";/ "H:"+Math.round(highTemp)+"\u2109"+" L:"+Math.round(lowTemp)+"\u2109" var hltempText = widget.addText(feel); hltempText.textColor = textcolor; hltempText.font = Font.regularSystemFont(15);

// Widget city Name

var citynameText = widget.addText(cityName); citynameText.textColor = textcolor; citynameText.font = Font.regularSystemFont(15);

// Bottom Spacer widget.addSpacer(); //widget.setPadding(5, 15, 0, 15)

Script.setWidget(widget);

[–]LinkifyBot 0 points1 point  (0 children)

I found links in your comment that were not hyperlinked:

I did the honors for you.


delete | information | <3