Skip to content
This repository has been archived by the owner on Jul 4, 2020. It is now read-only.

Commit

Permalink
Add shareShortcut function
Browse files Browse the repository at this point in the history
  • Loading branch information
a2 committed Dec 23, 2018
1 parent 3f19ff1 commit 7a664a4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
var batteryLevel = Variable.actionOutput()
let shortcutName = "Warn for Low Battery Level"
let shortcutData = try exportShortcut(
let shortcut = buildShortcut(
comment("This Shortcut was generated in Swift.") +
getBatteryLevel(&batteryLevel) +
ifLessThan(20, ifTrue: (
Expand All @@ -10,12 +9,5 @@ let shortcutData = try exportShortcut(
showResult("Your battery is at \(batteryLevel)%, you're probably fine for now.")
))
)
//#-hidden-code
import PlaygroundSupport
if let remoteView = PlaygroundPage.current.liveView as? PlaygroundRemoteLiveViewProxy {
remoteView.send(.dictionary([
"name": .string(shortcutName),
"data": .data(shortcutData),
]))
}
//#-end-hidden-code

shareShortcut(shortcut, named: "Warn for Low Battery Level")
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
let shortcutName = "Clap Along"
let shortcutData = try exportShortcut(
let shortcut = buildShortcut(
comment("This Shortcut was generated in Swift.") +
ask(question: "WHAT πŸ‘ DO πŸ‘ YOU πŸ‘ WANT πŸ‘ TO πŸ‘ SAY") +
changeCase(to: .uppercase) +
Expand All @@ -9,12 +8,5 @@ let shortcutData = try exportShortcut(
("Copy to Clipboard", copyToClipboard()),
])
)
//#-hidden-code
import PlaygroundSupport
if let remoteView = PlaygroundPage.current.liveView as? PlaygroundRemoteLiveViewProxy {
remoteView.send(.dictionary([
"name": .string(shortcutName),
"data": .data(shortcutData),
]))
}
//#-end-hidden-code

shareShortcut(shortcut, named: "Clap Along")
17 changes: 15 additions & 2 deletions ShortcutsSwift.playgroundbook/Contents/Sources/BuildShortcut.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@ public func buildShortcut(_ actionContainer: ActionContainer) -> PropertyList {
] as PropertyList
}

public func exportShortcut(_ actionContainer: ActionContainer) throws -> Data {
return try PropertyListSerialization.data(fromPropertyList: buildShortcut(actionContainer), format: .binary, options: 0)
public func exportShortcut(_ actionContainer: ActionContainer) -> Data {
return try! PropertyListSerialization.data(fromPropertyList: buildShortcut(actionContainer), format: .binary, options: 0)
}

#if canImport(PlaygroundSupport)

import PlaygroundSupport

public func shareShortcut(_ shortcut: PropertyList, named name: String) {
let data = try! PropertyListSerialization.data(fromPropertyList: shortcut, format: .binary, options: 0)
if let remoteView = PlaygroundPage.current.liveView as? PlaygroundRemoteLiveViewProxy {
remoteView.send(.dictionary(["name": .string(name), "data": .data(data)]))
}
}

#endif

0 comments on commit 7a664a4

Please sign in to comment.