Skip to content

Instantly share code, notes, and snippets.

@Sillium
Last active March 1, 2023 13:41
Show Gist options
  • Save Sillium/313164aec3d835c076ebfcd330f1be14 to your computer and use it in GitHub Desktop.
Save Sillium/313164aec3d835c076ebfcd330f1be14 to your computer and use it in GitHub Desktop.
Telekom Data Usage - iOS 14 Widget for Scriptable app
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: pink; icon-glyph: exchange-alt;
const DEBUG = false
const log = DEBUG ? console.log.bind(console) : function () { };
// configure the library
const libraryInfo = {
name: 'TelekomDataUsageLibrary',
version: '1.0.3',
gitlabProject: 'https://gitlab.com/sillium-scriptable-projects/universal-scriptable-widget-libraries',
forceDownload: DEBUG
}
// download and import library
let library = importModule(await downloadLibrary(libraryInfo))
// create the widget
const params = {
widgetParameter: args.widgetParameter,
debug: DEBUG
}
const widget = await library.createWidget(params)
// preview the widget
if (!config.runsInWidget) {
await widget.presentSmall()
}
Script.setWidget(widget)
Script.complete()
/**
* - creates directory for library if not existing
* - downloads library file if not existing or forced
* - returns relative path to library module
* @param {{name: string, version: string, gitlabProject: string, forceDownload: bool}} library
*/
async function downloadLibrary(library) {
let fm = FileManager.local()
let scriptPath = module.filename
let libraryDir = scriptPath.replace(fm.fileName(scriptPath, true), fm.fileName(scriptPath, false))
if (fm.fileExists(libraryDir) && !fm.isDirectory(libraryDir)) {
fm.remove(libraryDir)
}
if (!fm.fileExists(libraryDir)) {
fm.createDirectory(libraryDir)
}
let libraryFilename = library.name + '_' + library.version + '.js'
let path = fm.joinPath(libraryDir, libraryFilename)
let forceDownload = (library.forceDownload) ? library.forceDownload : false
if (fm.fileExists(path) && !forceDownload) {
log("Not downloading library file")
} else {
let r = Math.random().toString(36).substring(7);
let libraryUrl = library.gitlabProject + '/-/raw/' + library.version + '/' + library.name + '.js?random=' + r
log("Downloading library file '" + libraryUrl + "' to '" + path + "'")
const req = new Request(libraryUrl)
let libraryFile = await req.load()
fm.write(path, libraryFile)
}
return fm.fileName(scriptPath, false) + '/' + libraryFilename
}
@Funterrain
Copy link

Seit ein paar Tagen bekomme ich folgende Meldung:
2022-06-23 09:05:39: Error on line 16:27: No file to import at Telekom/TelekomDataUsageLibrary_1.0.3.

Was kann ich tun?

@hstoerk
Copy link

hstoerk commented Jun 24, 2022

Ich habe nicht wirklich Zeit, mich drum zu kümmern, aber beim Fehler "Error on line 16:27: No file to import at Telekom/TelekomDataUsageLibrary_1.0.3." probiert mal bitte, die Zeile 12 zu ändern von forceDownload: DEBUG nach forceDownload: true.

@Funterrain
Copy link

das klappt leider auch nicht

@Funterrain
Copy link

2022-06-26 16:18:05: telekom.png is stored in iCloud but the file have not been downloaded. Use downloadFileFromiCloud(filePath) on a FileManager to download the file before accessing it.
2022-06-26 16:18:05: Error on line 191:44: Expected value of type Image but got value of type null.

@Sillium
Copy link
Author

Sillium commented Jul 5, 2022

Ich habe nicht wirklich Zeit, aber hier ist eine neue Version, die ein paar Fehler beheben sollte: https://gist.github.com/Sillium/235dc9fd873b25e321b3299da64c9c38

Bitte unbedingt einmal den Folder iCloud Drive --> Scriptable --> löschen, oder wie auch immer ihr das Script bei euch genannt habt.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment