0% found this document useful (0 votes)
191 views2 pages

Random Scriptable API Widget

This script shows a random Scriptable API in a widget. It loads documentation data from an online source, selects a random API, and displays its name, description, and a link to documentation. It creates a widget with the API details and loads the app icon. If running in the widget, it sets the widget, otherwise it previews it.

Uploaded by

harry sandhu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
191 views2 pages

Random Scriptable API Widget

This script shows a random Scriptable API in a widget. It loads documentation data from an online source, selects a random API, and displays its name, description, and a link to documentation. It creates a widget with the API details and loads the app icon. If running in the widget, it sets the widget, otherwise it previews it.

Uploaded by

harry sandhu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
  • Script Overview
  • API Functions

// Variables used by Scriptable.

// These must be at the very top of the file. Do not edit.


// icon-color: deep-blue; icon-glyph: book;
// This script shows a random Scriptable API in a widget. The script is meant to be
used with a widget configured on the Home Screen.
// You can run the script in the app to preview the widget or you can go to the
Home Screen, add a new Scriptable widget and configure the widget to run this
script.
// You can also try creating a shortcut that runs this script. Running the shortcut
will show widget.
let api = await randomAPI()
let widget = await createWidget(api)
if ([Link]) {
// The script runs inside a widget, so we pass our instance of ListWidget to be
shown inside the widget on the Home Screen.
[Link](widget)
} else {
// The script runs inside the app, so we preview the widget.
[Link]()
}
// Calling [Link]() signals to Scriptable that the script have finished
running.
// This can speed up the execution, in particular when running the script from
Shortcuts or using Siri.
[Link]()

async function createWidget(api) {


let appIcon = await loadAppIcon()
let title = "Random Scriptable API"
let widget = new ListWidget()
// Add background gradient
let gradient = new LinearGradient()
[Link] = [0, 1]
[Link] = [
new Color("141414"),
new Color("13233F")
]
[Link] = gradient
// Show app icon and title
let titleStack = [Link]()
let appIconElement = [Link](appIcon)
[Link] = new Size(15, 15)
[Link] = 4
[Link](4)
let titleElement = [Link](title)
[Link] = [Link]()
[Link] = 0.7
[Link] = [Link](13)
[Link](12)
// Show API
let nameElement = [Link]([Link])
[Link] = [Link]()
[Link] = [Link](18)
[Link](2)
let descriptionElement = [Link]([Link])
[Link] = 0.5
[Link] = [Link]()
[Link] = [Link](18)
// UI presented in Siri ans Shortcuta is non-interactive, so we only show the
footer when not running the script from Siri.
if (![Link]) {
[Link](8)
// Add button to open documentation
let linkSymbol = [Link]("[Link]")
let footerStack = [Link]()
let linkStack = [Link]()
[Link]()
[Link] = [Link]
let linkElement = [Link]("Read more")
[Link] = [Link](13)
[Link] = [Link]()
[Link](3)
let linkSymbolElement = [Link]([Link])
[Link] = new Size(11, 11)
[Link] = [Link]()
[Link]()
// Add link to documentation
let docsSymbol = [Link]("book")
let docsElement = [Link]([Link])
[Link] = new Size(20, 20)
[Link] = [Link]()
[Link] = 0.5
[Link] = "[Link]
}
return widget
}

async function randomAPI() {


let docs = await loadDocs()
let apiNames = [Link](docs)
let num = [Link]([Link]() * [Link])
let apiName = apiNames[num]
let api = docs[apiName]
return {
name: apiName,
description: api["!doc"],
url: api["!url"]
}
}

async function loadDocs() {


let url = "[Link]
let req = new Request(url)
return await [Link]()
}

async function loadAppIcon() {


let url =
"[Link]
f7db-d6d2c53b4323/AppIcon-1x_U007emarketing-[Link]/[Link]"
let req = new Request(url)
return [Link]()
}

Common questions

Powered by AI

The script ensures the widget displays a gradient background by creating a new "LinearGradient" object and setting its "locations" and "colors" properties. The gradient is applied to the widget's "backgroundGradient" property. Additionally, the script customizes layout elements by manipulating properties of the widget's components such as text color, opacity, and font. For instance, "titleElement.textColor = Color.white()" and "titleElement.font = Font.mediumSystemFont(13)" are assigned within the widget layout to customize the display further .

The interactive elements in the script's widget include clickable text and icon that provide links to external content, specifically to the API documentation. The "linkStack" contains a "Read more" text link paired with an arrow symbol, styled in blue to indicate interactivity. This design choice encourages user engagement by offering an intuitive pathway to additional information. By clicking these elements, users can seamlessly transition from the widget to more detailed content, thereby enhancing the widget's functionality and user interaction .

The script does not explicitly implement error handling mechanisms for runtime errors or exceptional cases during API fetching and widget creation. It uses async functions which generally require error handling strategies, such as try-catch blocks, to manage potential failures. The absence of such mechanisms means that if a promise is rejected, the script might terminate unexpectedly without notifying the user about the nature of the error. This approach is often considered risky as it lacks robustness, exposing the script to failures in network requests or API parsing without recourse to recover gracefully .

The ListWidget is used in the script to efficiently organize and display a collection of UI elements in a cohesive layout. Utilizing ListWidget allows the script to layer different components like images, text, and stacks which can be aligned and spaced in a customizable manner. This flexibility is significant as it facilitates the creation of visually engaging and well-structured widgets. For example, the script adds a title, description, an app icon, and optional footer elements in sequence using the ListWidget's "addStack", "addImage", and "addText" methods, ensuring that each component is neatly arranged .

The script uses conditional logic to manage different execution contexts. When it detects that it is being run as a widget (using "config.runsInWidget"), the script uses "Script.setWidget(widget)" to display the widget on the Home Screen. In contrast, if the script runs in the app context, indicated by the absence of the widget-specific conditions, it uses "widget.presentMedium()" to preview the widget within the application. These checks ensure that the script adapts its function to the environment it is executed within, optimizing for user context .

The script enhances user interaction by including clickable elements that lead to external content, specifically API documentation. It adds a "linkStack" within the "footerStack" to incorporate a URL to the API's documentation page. The text "Read more" and a link symbol are displayed, both styled with a blue color to indicate interactivity. By setting "linkStack.url = api.url", it enables users to tap on the link, redirecting them to the external documentation page. This feature provides a seamless bridge from the widget to detailed API information .

The script dynamically selects a random API by fetching documentation data via "loadDocs()" and choosing an API name from the keys of the fetched object. It uses mathematical randomness with "Math.random()" combined with the length of the API names array to ensure variability. This approach impacts user experience positively by providing unpredictability and new information each time the script runs, keeping the content fresh and engaging. However, it can also result in inconsistent content quality if some API entries are less informative, thus affecting user satisfaction based on random chance .

The "await" keyword in the Scriptable script is used to pause the execution of the function until the promise is fulfilled, meaning that the function waits for the asynchronous operation to complete before continuing execution. This is essential in asynchronous programming because it allows the script to handle tasks such as loading an API or an image without blocking the execution of other operations. For instance, the script uses "await" with the "randomAPI", "createWidget", "loadDocs", and "loadAppIcon" functions, ensuring that each function fully resolves before the execution proceeds to the following instructions .

The script ensures execution efficiency through the use of "Script.complete()". By calling this method at the end of the script, it signals to the Scriptable environment that execution has concluded. This is particularly beneficial when the script is run from Shortcuts or using Siri because it helps speed up the execution by optimizing resource usage and ending processes promptly. Additionally, "Script.complete()" can enhance the performance and responsiveness of scripts that involve asynchronous operations .

The script displays adaptive behavior by checking the "config.runsWithSiri" variable to identify whether it's running with Siri or not. If not running with Siri, the script adds additional interactive UI components like the footer stack with documentation links. This adaptability is important because UI presented in Siri and Shortcuts is non-interactive, thus necessitating a dynamic behavior where interactive elements are only added when they can be utilized. This ensures that the widget's design is optimized for the user's context and mitigates the potential user confusion or error in non-interactive environments .

// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: deep-blue; icon-gl
footer when not running the script from Siri.
  if (!config.runsWithSiri) {
    widget.addSpacer(8)
    // Add button to open

You might also like