使用Scriptable制作IOS小组件之选择相册图片展示

时间:2024-04-16 16:37:51

1.首先添加快捷指令,目的是保存图片至文件中

  下载地址:https://www.icloud.com/shortcuts/b82164d0ef224944ae8a74fe501f5f03

  safari打开链接添加快捷指令。快捷指令需要打开“允许不受信任的快捷指令”

2.运行快捷指令

  选择一张或多张图片,选择存储位置iCloud文件夹中的scriptable。

3.打开Scriptable软件,点击添加,长按粘贴

let cover = await getData()
let widget = createWidget(cover)

if (!config.runsInWidget) {
    await widget.presentMedium()
}

Script.setWidget(widget)
Script.complete()

async function getImage(url) {
    let r = new Request(url)
    return await r.loadImage()
}

async function getData() {
    let fm = FileManager.iCloud();
    let imageList = [\'IMG_0016.jpeg\']
    let count = imageList.length
    let m = count
    let n = 1
    let index = Math.ceil(Math.random() * (n - m) + m) - 1
    let path = fm.documentsDirectory() + "/" + imageList[index]
    // Image.fromFile(path) can also be used
    return fm.readImage(path)
}

function createWidget(cover) {
 
    let w = new ListWidget()
    
    w.backgroundImage = cover
    let gradient = new LinearGradient()
    gradient.locations = [0, 1]
    gradient.colors = [
        new Color(\'#00000000\'),
        new Color(\'#000000CC\')
    ]
    w.backgroundGradient = gradient
    
    return w
}

4.添加小组件,选择刚才的脚本

5.或者直接粘贴代码至软件中。然后从文件scriptable中选择图片名称,将let imageList = [\'IMG_0016.jpeg\']  修改成自己的图片名称。如let imageList = [\'自己图片的名称\',\'自己图片的名称\']