Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Leibinger015/37f02464e3efc3deccab1c14642dbb65 to your computer and use it in GitHub Desktop.
Save Leibinger015/37f02464e3efc3deccab1c14642dbb65 to your computer and use it in GitHub Desktop.
iPhone widget "APPwish": Keep an eye on price alerts for premium apps!
// This code is a script for the iPhone app Scriptable.
// An widget named “APPwish” for a paid Premium WishApp. This way you can keep an eye on the price for a discount promotion.
//
// Script-Code: ©️anb030.de
// Script-Date: 28.10.2024
// Script-Last-Edit: 10.11.2024
// Version: 1.3b - Minor code fixes, fullscreen AppIcon cover with txt layer, country code for the right Apple AppStore with only 2 letters, edit the parameter in the widget for the AppID and design fine-tuning.
//
//
// Standard AppStore App-ID: Example app without parameters -> HomeDash (Please enter your desired AppID in the Wigdet parameter!)
const defaultAppId = "1003033186";
// Define your country code for the right AppStore: "de" = Germany, "it" = Italy, "us" = USA ...
const countryCode = "de";
//
//
// Start of the script "wishAPPprice" for the iPhone homescreen widget
//
//
// Überprüfen, ob eine `appId` als Parameter übergeben wurde
const appId = (args.widgetParameter && args.widgetParameter.trim().length > 0) ? args.widgetParameter.trim() : defaultAppId;
// Funktion, um die API-URL mit einer App-ID und Länderkennung zu erstellen
function getAppStoreUrl(appId, countryCode) {
return `https://itunes.apple.com/lookup?id=${appId}&country=${countryCode}`;
}
// App Store URL für die direkte Weiterleitung beim Antippen des Widgets
const appStoreOpenUrl = `https://apps.apple.com/${countryCode}/app/id${appId}`;
const appStoreUrl = getAppStoreUrl(appId, countryCode);
async function fetchAppData() {
const req = new Request(appStoreUrl);
const res = await req.loadJSON();
if (res.results.length === 0) {
throw new Error("↻ App not found!");
}
return res.results[0];
}
// Funktion zur Kürzung des App-Namens auf maximal 10 Zeichen
function shortenText(text, maxLength) {
if (text.length > maxLength) {
return text.substring(0, maxLength) + "…";
}
return text;
}
async function createWidget() {
const widget = new ListWidget();
// Setze die URL, die beim Antippen des Widgets geöffnet wird
widget.url = appStoreOpenUrl;
try {
const appData = await fetchAppData();
let appName = appData.trackName;
const appPrice = appData.formattedPrice;
const appIconUrl = appData.artworkUrl512;
// Lade das App-Icon
const appIcon = await loadImage(appIconUrl);
// Setze das App-Icon als vollflächiges Hintergrundbild des Widgets
widget.backgroundImage = appIcon;
// Farben und Stil für die Textstreifen
const overlayColor = new Color("#000000", 0.5); // Schwarzer, halbtransparenter Hintergrund
const textColor = Color.white(); // Weißer Text
const cornerRadius = 10; // Abrundung der Ecken für die Streifen
// Kürze den App-Namen auf maximal 12 Zeichen
appName = shortenText(appName, 12);
// Füge den App-Namen hinzu, oben mittig zentriert
const nameStack = widget.addStack();
nameStack.layoutHorizontally();
nameStack.backgroundColor = overlayColor;
nameStack.cornerRadius = cornerRadius;
nameStack.size = new Size(200, 25);
nameStack.centerAlignContent();
const nameText = nameStack.addText(appName);
nameText.font = new Font("Arial-BoldMT", 16);
nameText.textColor = textColor;
// Abstand in der Mitte
widget.addSpacer();
// Füge den App-Preis und das Länderkürzel ganz unten hinzu, linksbündig im selben Streifen
const priceStack = widget.addStack();
priceStack.layoutHorizontally();
priceStack.backgroundColor = overlayColor;
priceStack.cornerRadius = cornerRadius;
priceStack.size = new Size(150, 25);
// App-Preis
const priceText = priceStack.addText(appPrice);
priceText.font = new Font("Arial-BoldMT", 18);
priceText.textColor = textColor;
priceText.leftAlignText(); // Text linksbündig ausrichten
// Abstand zwischen App-Preis und Länderkürzel
priceStack.addSpacer(5);
// Länderkürzel
const countryLabel = priceStack.addText(`(${countryCode.toUpperCase()})`);
countryLabel.font = new Font("Helvetica", 8);
countryLabel.textColor = textColor;
countryLabel.leftAlignText(); // Text linksbündig ausrichten
} catch (error) {
console.error(error);
// Fehlerhandling, wenn App-Daten nicht geladen werden können
const symbol = SFSymbol.named("exclamationmark.triangle");
const symbolImage = widget.addImage(symbol.image);
symbolImage.tintColor = Color.white();
symbolImage.imageSize = new Size(40, 40);
symbolImage.centerAlignImage();
const errorText = widget.addText("App-Data loading problem!");
errorText.font = new Font("Helvetica", 16);
errorText.textColor = Color.white();
errorText.centerAlignText();
}
return widget;
}
async function loadImage(url) {
const req = new Request(url);
const img = await req.loadImage();
return img;
}
if (config.runsInWidget) {
const widget = await createWidget();
Script.setWidget(widget);
} else {
const widget = await createWidget();
widget.presentSmall();
}
Script.complete();
@Leibinger015
Copy link
Author

This code is a script for the iPhone app Scriptable. A widget called "APPwish" for a paid premium wish app. This way you can keep an eye on the price for a discount promotion.

IMG_8903
IMG_8913

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