Created
January 23, 2025 07:26
-
-
Save syneart/9de05655ffc9914259c852476a1510ab to your computer and use it in GitHub Desktop.
Scriptable app 腳本, 乾淨的手機發票條碼 iOS Widget, 點擊後可以複製手機條碼
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This Scriptable script is made by SyneArt <[email protected]> | |
// 設定條碼的文字內容 | |
let barcodeText = "/ABC+123"; // 改為你的手機條碼 | |
// 判斷是否是從 Widget 點擊進來 | |
if (args.queryParameters.barcodeText) { | |
// 如果有傳入參數,執行複製功能 | |
let barcodeText = args.queryParameters.barcodeText; | |
// 將文字內容複製到剪貼簿 | |
Pasteboard.copyString(barcodeText); | |
// 顯示已複製的提示 | |
let alert = new Alert(); | |
alert.title = "已複製"; | |
alert.message = `條碼內容「${barcodeText}」已成功複製到剪貼簿!`; | |
alert.addAction("確定"); | |
await alert.present(); | |
return; // 結束腳本 | |
} | |
// 以下為 Widget 的建立邏輯 | |
// 建立 widget | |
let widget = new ListWidget(); | |
widget.backgroundColor = new Color("#ffffff"); // 設定背景為純白色 | |
// 使用 Barcode4J 產生條碼的 URL | |
let barcodeUrl = `https://bwipjs-api.metafloor.com/?bcid=code39&text=${barcodeText}&scale=4&height=18`; | |
// 下載條碼圖片 | |
let img = await downloadImage(barcodeUrl); | |
// 在 widget 中新增條碼圖片 | |
widget.addImage(img); | |
// 增加空白間距,讓條碼與文字之間有適當距離 | |
widget.addSpacer(5); | |
// 在條碼下方新增文字並置中顯示 | |
let text = widget.addText(barcodeText); | |
text.textColor = new Color("#000000"); // 設定文字顏色為黑色 | |
text.font = Font.boldSystemFont(16); // 設定文字字型及大小 | |
text.centerAlignText(); // 文字置中顯示 | |
// 設定 widget 的內邊距 | |
widget.setPadding(10, 20, 10, 10); | |
// 設定點擊 widget 時的動作,將條碼文字複製到剪貼簿 | |
widget.url = `scriptable:///run?scriptName=${encodeURIComponent(Script.name())}&barcodeText=${encodeURIComponent(barcodeText)}`; | |
// 顯示 widget | |
if (config.runsInWidget) { | |
Script.setWidget(widget); // 設定 widget | |
} else { | |
// 在預覽模式下顯示提示訊息 | |
let alert = new Alert(); | |
alert.message = "條碼已產生!"; | |
alert.addAction("確定"); | |
await alert.present(); | |
} | |
// 下載圖片 | |
async function downloadImage(url) { | |
let req = new Request(url); | |
let img = await req.loadImage(); // 從 URL 下載圖片 | |
return img; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment