Random Scriptable API Widget Script
Random Scriptable API Widget Script
The script sets specific text properties to ensure visibility: it uses white color, adjusts font sizes appropriately, and sets text opacity. These settings create a contrast against the dark gradient background, ensuring that text elements like titles and descriptions are readable regardless of the color variations .
The 'presentMedium()' method is used to preview the widget's appearance when the script is run inside the app but not from within the widget environment. This allows the developer to simulate and inspect how the widget will look and behave in a medium-sized widget format on the home screen .
The widget displays a footer with documentation links only when it is not run via Siri. This is due to the non-interactive nature of UI in Siri, which makes showing interactive elements like documentation links unnecessary .
The script uses the 'config.runsInWidget' condition to differentiate execution environments. If running in a widget, it uses 'Script.setWidget(widget)' to display the widget; otherwise, it calls 'widget.presentMedium()' for app execution. This determines whether the script's output should be presented directly in the app or configured for a widget on the Home Screen .
To add a documentation link, the script creates a 'footerStack' within which it adds a 'linkStack'. This stack is center-aligned and has a URL pointing to the API documentation. A text element labeled 'Read more' is added, followed by a spacer and an arrow symbol image, both styled and linked to the documentation .
The 'loadJSON()' function is called to retrieve the API documentation data from a specified URL. It returns the data in JSON format, enabling the script to access and utilize information about different Scriptable APIs dynamically for display in the widget .
The script uses the 'randomAPI' function to select an API. It first loads a JSON from 'https://docs.scriptable.app/scriptable.json' which contains documentation data. Then it selects a random API by generating a random number and picking the API corresponding to that index in the list of API names from the JSON data .
The 'LinearGradient' is used to add a background gradient to the widget. This makes the widget visually more appealing by transitioning colors smoothly from one to another. The gradient settings specify two color stops at the beginning and end .
Running the script from Shortcuts or Siri can improve execution speed because the script signals completion with 'Script.complete()'. This is particularly useful for reducing the execution time in these contexts where quick responses are critical .
The 'cornerRadius' is applied to the 'appIconElement' to make the icon appear smoother with rounded corners, which can be visually more attractive and consistent with modern UI design standards where sharp angles are often avoided .