Skip to content

Instantly share code, notes, and snippets.

@marco79cgn
Last active September 30, 2020 04:47
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marco79cgn/e05ca19ea2d15194bc7991f7efab8083 to your computer and use it in GitHub Desktop.
Save marco79cgn/e05ca19ea2d15194bc7991f7efab8083 to your computer and use it in GitHub Desktop.
An iOS Scriptable widget that shuffles through your latest Pocket bookmarks
// insert your Pocket consumer key and access token
let consumerKey = "xxx"
let accessToken = "xxx"
// the pocket base url
let pocketUrl = "https://getpocket.com/v3/get?"
let openWith = args.widgetParameter
let widget = new ListWidget()
widget.setPadding(0, 5, 3, 5)
widget.backgroundColor = new Color("#000000")
await getPocketItems()
await loadImage("https://www.pngkit.com/png/full/230-2308488_pocket-png.png")
Script.setWidget(widget)
Script.complete()
// random number, min and max included
function getRandomNumberNumber(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min)
}
// helper function to download an image
async function loadImage(imageUrl) {
let req = new Request(imageUrl)
let image = await req.loadImage()
let widgetImage = widget.addImage(image)
widgetImage.imageSize = new Size(60,20)
widgetImage.rightAlignImage()
}
// query the getPocket API
async function getPocketItems() {
let urlParams = "consumer_key=" + consumerKey + "&access_token=" + accessToken + "&count=10"
let req = new Request(pocketUrl + urlParams)
req.method = "GET"
req.headers = {"Content-Type":"application/json"}
let result = await req.loadJSON()
var pocketItems = [];
for(var i in result.list)
pocketItems.push(result.list[i]);
let title = pocketItems[9].resolved_title
let titleText = widget.addText(title)
titleText.applySubheadlineTextStyling()
titleText.textColor = Color.white()
titleText.textSize = 13
titleText.lineLimit = 2
titleText.leftAlignText()
widget.addSpacer(3)
let excerpt = pocketItems[9].excerpt
let excerptText = widget.addText(excerpt)
excerptText.applySubheadlineTextStyling()
excerptText.textColor = Color.gray()
excerptText.textSize = 13
excerptText.lineLimit = 4
excerptText.leftAlignText()
widget.addSpacer(3)
let source = pocketItems[9].domain_metadata.name
let dateAdded = new Date(pocketItems[9].time_added * 1000)
let df = new DateFormatter()
df.useMediumDateStyle()
df.useShortTimeStyle()
let strDate = df.string(dateAdded) + " Uhr"
console.log("timeAdded: " + strDate)
let dateAddedText = widget.addText("🔗 " + source + " • 📅 " + strDate)
dateAddedText.textColor = Color.white()
dateAddedText.textSize = 11
dateAddedText.centerAlignText()
widget.url = pocketItems[9].resolved_url
}
@marco79cgn
Copy link
Author

marco79cgn commented Aug 26, 2020

Intro

iOS 14 Custom Widget made with the help of the Scriptable app.
It shuffles one of your latest Pocket bookmarks and opens the article in Safari once you tap it.

Requirements

  • iOS 14 Developer Beta 5 (or above)
  • Scriptable TestFlight version 1.5 (156 or later)
    Join the beta here
  • Pocket Developer API
    Please create an app in the Pocket dashboard in order to get your consumer_key and access_token. They are needed to obtain your bookmarks. Insert them at the top of the script. Here's a step by step guide on how to get them (→ the tutorial uses python but feel free to just use cURL or Postman.)
    Alternatively I built a Shortcut which automates the process of obtaining your Pocket access token and generates this whole script automatically. Just insert your Pocket consumer key upon import. After granting access & seeing the DuckDuckGo page, close the window on the upper left ("Done") to proceed. At the end of the shortcut, select your Scriptable folder to save the file. It will then appear automatically in Scriptable named "pocketWidget".

Thanks

A big Thank you to @simonbs for making great apps like Scriptable, DataJar or Jayson.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment