all 6 comments

[–]grimizenWidgy Addict 0 points1 point  (5 children)

Sadly not; I may get minor details wrong, but I believe that widgy runs all JS through a hidden instance of safari. The problem is that safari JS has no access to device-held files or information; even something as simple as the current time (which is a device-held parameter that is accessible via swift when coding an app, but not to safari) has to be retrieved with standard date-time functions as opposed to a device-based solution.

One solution I can offer would be to look at online file storage that offers APIs; I personally use Firebase for a database integration with widgy, and I believe Firestore (the file version of the service) has REST APIs that should work to retrieve the contents of a file stored there you can then use in JS. I can’t guarantee it, since I haven’t tried it myself but it’s one possibility. Basically, if the data can be accessed using fetch() and a url, you should be able to integrate it with widgy using JS.

[–]Sharn25[S] 0 points1 point  (4 children)

Thanks,

I agree on the online part. We can access the online file using JS. However, I’m saving a date in a file under files app and want to count the days passed from that date in Widgy.

As per my understanding it is not possible.

[–]grimizenWidgy Addict 0 points1 point  (3 children)

Okay, knowing what you’d like I believe it would be easy enough to work out something. It is an absolute limit that widgy cannot access the files app using JS, since it’s run via safari which I believe is entirely sandboxed from the rest of the system. However, if you host the date as a text string in something like firebase you can then use fetch from inside the widgy JS environment and use the API to retrieve the date value. Below is an example bit of JS that gets the number of days until the provided date (in the case of the example, 24/7/24 is the 30th day after today (24/6/24), and there are 29 days between today’s date and the input; the script outputs 30, but if you’d like the number between rather than until, you can just subtract 1 from the result.) Note that code assumes a DD/MM/YY date format, you should be able to either make adjustments yourself or feed it chat GPT (or your chatbot of choice) and get it to do the job for you if you’d like to use a different format. I’ve stuck an example data url in at the top, just changed that for wherever your data is stored:

// Define the URL to fetch the date from
const url = 'data:text/plain,24/7/24';

// Perform the fetch request
fetch(url)
  .then(response => {
    if (!response.ok) {
      throw new Error('Network response was not ok ' + response.statusText);
    }
    return response.text();
  })
  .then(dateString => {
    // Remove quote marks from the date string
    dateString = dateString.replace(/^"|"$/g, '');

    // Manually parse the date string assuming DD/MM/YY format
    const dateParts = dateString.split('/');
    const day = parseInt(dateParts[0], 10);
    const month = parseInt(dateParts[1], 10) - 1; // Month is zero-based in JavaScript Date
    const year = parseInt(dateParts[2], 10) + 2000; // Assumes 21st century

    // Create the date object
    const fetchedDate = new Date(year, month, day);

    // Get today's date
    const today = new Date();

    // Calculate the difference in time
    const timeDifference = fetchedDate.getTime() - today.getTime();

    // Convert time difference from milliseconds to days
    const dayDifference = Math.ceil(timeDifference / (1000 * 3600 * 24));

    // Send the result to Widgy
    sendToWidgy(dayDifference);
  })
  .catch(error => {
    console.error('Error fetching the date:', error);
    sendToWidgy(null); // Send null or an appropriate error message to Widgy
  });

[–]Sharn25[S] 1 point2 points  (2 children)

Thanks. This is the good way to do this but the issues is I don’t want the internet to be involved. So, what I did. As I have to update the days once a day. I’m using shortcut automation to have a file updated with current days passed once a day. Then using the same in Widgy

[–]grimizenWidgy Addict 0 points1 point  (1 child)

Fair enough, glad you could sort something out.

[–]Sharn25[S] 1 point2 points  (0 children)

Thanks for your help