0% found this document useful (0 votes)
27 views3 pages

Random Scriptable API Widget Script

Uploaded by

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

Random Scriptable API Widget Script

Uploaded by

mansocrack732
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as 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]
Purple124/v4/21/1e/13/211e13de-2e74-4221-f7db-d6d2c53b4323/
AppIcon-1x_U007emarketing-[Link]/[Link]"
let req = new Request(url)
return [Link]()
}

Common questions

Powered by AI

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 .

You might also like