Scriptable: Clipboard URL to Markdown Link

With the move from Workflow to Shortcuts, I’ve come a little unstuck with a new issue around sharing them on iOS. Previously, if I wanted to get the name of a workflow and it’s URL in a Markdown link format, I could use a workflow to grab the title of the download page of the URL, which would match to the name of the workflow. After that it was just a case of wrapping the details in some Markdown syntax and we were done. Unfortunately that approach doesn’t work any more, but I have come up with a workaround utilising Scriptable.

What’s the Problem?

Quite simply, the download page is now dynamically built in a manner that means when the page frst loads, the title is not correctly set. Only when additional page elements have been loaded does the entire page update to offer the download, and also title the page accordingly. Whenever I have tried to automate this on iOS, I’ve hit a limitation of the utility app being used grabbing the title of the first build of the page rather than the final build of the page. Nothing I tried got the system to wait appropriately.

What’s the Solution?

The approach I ended up with utilises Scriptable and puts the onus on me to tell it when the page has loaded. Scriptable is a JavaScript-based utility and it has an option to use a web view to let me view the loading of the page, and it loads it just like Safari does, and then allows subsequent automated interaction. These are rather important factors that my previous attenmpts had lacked.

I start by sharing the iCloud link to a shortcut to the device clipboard. After that I trigger my Scriptable script (typically from Launch Center Pro). The Scriptable script receives the URL and passes it to a function called MDLink. The function creates a web view and loads the URL from the clipboard into it. Once the page finished loading to the point where I can see that the title is set, I select the Done option in the top bar of the web view. At that point, the script evaluates some additional JavaScript from the final state of the web view that grabs the title of the page. Finally the function builds the Markdown formatted URL with the title as the link text and the URL as the link URL. This is then put onto the clipboard ready to be pasted.

The Scriptable script looks like this.

Pasteboard.copyString((await MDLink(Pasteboard.pasteString())))

async function MDLink(p_strURL)
{
	let wv = new WebView()
	wv.loadURL(p_strURL)
	await wv.present()
	let strTitle = await wv.evaluateJavaScript("document.title", false)
	return "[" + strTitle + "](" + p_strURL + ")"
}

While this isn’t the fully automated solution I really want, it’ll do until Apple opens up something for Shortcuts that let’s me directly access both pieces of information, or a way to automate the wait without a manual intervention web view is available.

Author: Stephen Millard
Tags: | scriptable | javascript | shortcuts |

Buy me a coffeeBuy me a coffee



Related posts that you may also like to read