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

Random Scriptable API Widget Script

Uploaded by

Hua Yang
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)
8 views2 pages

Random Scriptable API Widget Script

Uploaded by

Hua Yang
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

// 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

Differentiating between running as a widget and in the app allows the script to tailor its behavior and resource usage. As a widget, it displays on the Home Screen and requires quick execution with minimal interactivity. In the app, it can offer a preview and more interactive elements, such as links and additional styling. This distinction optimizes performance and user interaction based on the context .

In the script, the `LinearGradient` object is used to set a background gradient for the widget. It is configured with a `locations` array specifying the start and end points of the gradient, and `colors` array defining the color transition from a dark grey (#141414) to a deep blue (#13233F).

The script retrieves the Scriptable API documentation through `loadDocs()`, which returns a JSON object of APIs. It then extracts API names into an array and selects one randomly using `Math.round(Math.random() * apiNames.length)`. The chosen API is returned with its name, documentation, and URL, subsequently used to generate the widget content with text elements displaying the API name and description .

The script defines and uses the asynchronous functions `loadDocs` and `loadAppIcon` to load external resources. `loadDocs` fetches and loads JSON data from the Scriptable documentation URL, and `loadAppIcon` retrieves the app icon image from a specified URL using HTTP requests .

To present API information, the script utilizes a `createWidget` function where a `ListWidget` object is configured. It sets a background gradient, includes an app icon and title using stacks, and displays API details with text elements showing the API name and description. Optional footer elements are added outside Siri execution for documentation links, enhancing detail accessibility and visual design .

The script maintains consistent styling through the use of `Color` and `Font` objects for various text elements, setting properties like `textColor`, `font`, and `textOpacity`. Gradients are managed by a `LinearGradient` object for the background. These ensure uniform visual themes across the widget components .

The presence or absence of interactive elements, such as the footer with links, governs UI interactivity. The script checks `config.runsWithSiri`; if true, it assumes a static UI and does not add interactive components like the footer link. This condition reflects the limitations of non-interactive presentations in Siri .

When the script does not run with Siri, it adds interactive elements such as a footer with a link to the API documentation. This includes a 'Read more' text linked to the API URL, a linked symbol for additional information, and an image linking to the Scriptable documentation. These enhance user interactivity by providing direct access to resources .

The script checks the condition `if (config.runsInWidget)` to decide. If `runsInWidget` is true, the script sets the widget to be shown on the Home Screen using `Script.setWidget(widget)`. Otherwise, it previews the widget within the app using `widget.presentMedium()` .

`Script.complete()` signals that the script's execution is finished, particularly optimizing performance when the script is executed from Shortcuts or Siri. This method can speed up execution by immediately freeing up system resources upon completion rather than waiting for the runtime environment to automatically conclude the script execution .

You might also like