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

Actions for the "Tweet Current Song" shortcut #20

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Foundation

public func getCurrentSong() -> ActionWithOutput {
return Action(identifier: "is.workflow.actions.getcurrentsong", parameters: [:])
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Foundation

public func getText(_ text: String) -> ActionWithOutput {
return Action(
identifier: "is.workflow.actions.gettext",
parameters: ["WFTextActionText": withInterpolatedText(text)]
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Foundation

public func tweet(showComposeSheet: Bool = true) -> Action {
return Action(
identifier: "is.workflow.actions.tweet",
parameters: [
"WFSocialActionShowComposeSheet": showComposeSheet,
]
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,14 @@ public enum Aggrandizement {
case fileSize
case fileExtension
case number(Number)
case string(String)

var propertyListValue: Any {
switch self {
case .fileSize: return "WFFileSizeProperty"
case .fileExtension: return "WFFileExtensionProperty"
case .number(let number): return number
case .string(let string): return string
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,16 @@ public func exportShortcut(_ actionContainer: ActionContainer) -> Data {
return try! PropertyListSerialization.data(fromPropertyList: buildShortcut(actionContainer), format: .binary, options: 0)
}

public func exportShortcut(_ shortcut: PropertyList) -> Data {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes it easier to save the shortcut on the mac and send it to the phone for testing.

return try! PropertyListSerialization.data(fromPropertyList: shortcut, 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)
let data = exportShortcut(shortcut)
if let remoteView = PlaygroundPage.current.liveView as? PlaygroundRemoteLiveViewProxy {
remoteView.send(.dictionary(["name": .string(name), "data": .data(data)]))
}
Expand Down
14 changes: 14 additions & 0 deletions ShortcutsSwift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
98D33C2B21D3E34100824660 /* Repeat.swift in Sources */ = {isa = PBXBuildFile; fileRef = 98D33C2A21D3E34100824660 /* Repeat.swift */; };
98D33C3021D3EFF100824660 /* Calculate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 98D33C2F21D3EFF100824660 /* Calculate.swift */; };
98D33C3421D4067C00824660 /* List.swift in Sources */ = {isa = PBXBuildFile; fileRef = 98D33C3321D4067C00824660 /* List.swift */; };
FB651C2721DE29D20029DAE6 /* Twitter.swift in Sources */ = {isa = PBXBuildFile; fileRef = FB651C2621DE29D20029DAE6 /* Twitter.swift */; };
FB651C2921DE2A810029DAE6 /* GetText.swift in Sources */ = {isa = PBXBuildFile; fileRef = FB651C2821DE2A810029DAE6 /* GetText.swift */; };
FB651C2B21DE2AAF0029DAE6 /* GetCurrentSong.swift in Sources */ = {isa = PBXBuildFile; fileRef = FB651C2A21DE2AAF0029DAE6 /* GetCurrentSong.swift */; };
FBDA8D4321DCCA9E00303374 /* AggrandizementTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = FBDA8D4221DCCA9E00303374 /* AggrandizementTests.swift */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -85,7 +88,11 @@
98D33C2A21D3E34100824660 /* Repeat.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Repeat.swift; sourceTree = "<group>"; };
98D33C2F21D3EFF100824660 /* Calculate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Calculate.swift; sourceTree = "<group>"; };
98D33C3321D4067C00824660 /* List.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = List.swift; sourceTree = "<group>"; };
FB651C2621DE29D20029DAE6 /* Twitter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Twitter.swift; sourceTree = "<group>"; };
FB651C2821DE2A810029DAE6 /* GetText.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GetText.swift; sourceTree = "<group>"; };
FB651C2A21DE2AAF0029DAE6 /* GetCurrentSong.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GetCurrentSong.swift; sourceTree = "<group>"; };
FBDA8D4221DCCA9E00303374 /* AggrandizementTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AggrandizementTests.swift; sourceTree = "<group>"; };
FB7EE87321DC326B00C079C7 /* ShortcutsSwiftXcode.playground */ = {isa = PBXFileReference; lastKnownFileType = file.playground; path = ShortcutsSwiftXcode.playground; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -120,6 +127,7 @@
5502FF7C21D0F06500CE7544 = {
isa = PBXGroup;
children = (
FB7EE87321DC326B00C079C7 /* ShortcutsSwiftXcode.playground */,
5502FFF221D0F58400CE7544 /* ShortcutsSwift.playgroundbook */,
5502FFED21D0F2AB00CE7544 /* Helpers */,
5502FFC021D0F1A300CE7544 /* Sources */,
Expand Down Expand Up @@ -184,6 +192,9 @@
5502FFD021D0F1A400CE7544 /* Share.swift */,
5502FFD121D0F1A400CE7544 /* ShowResult.swift */,
98D33C2821D3DA0A00824660 /* SplitText.swift */,
FB651C2821DE2A810029DAE6 /* GetText.swift */,
FB651C2A21DE2AAF0029DAE6 /* GetCurrentSong.swift */,
FB651C2621DE29D20029DAE6 /* Twitter.swift */,
);
path = Actions;
sourceTree = "<group>";
Expand Down Expand Up @@ -332,11 +343,14 @@
5502FFEC21D0F29700CE7544 /* PlaygroundSupport.swift in Sources */,
5502FFEA21D0F1A400CE7544 /* Action.swift in Sources */,
83F8639821D15F7E00925583 /* Dictionary.swift in Sources */,
FB651C2921DE2A810029DAE6 /* GetText.swift in Sources */,
98D33C3421D4067C00824660 /* List.swift in Sources */,
98D33C2B21D3E34100824660 /* Repeat.swift in Sources */,
FB651C2721DE29D20029DAE6 /* Twitter.swift in Sources */,
5502FFE421D0F1A400CE7544 /* Share.swift in Sources */,
98D33C2921D3DA0A00824660 /* SplitText.swift in Sources */,
5502FFDD21D0F1A400CE7544 /* CopyToClipboard.swift in Sources */,
FB651C2B21DE2AAF0029DAE6 /* GetCurrentSong.swift in Sources */,
5502FFDF21D0F1A400CE7544 /* Comment.swift in Sources */,
5502FFE321D0F1A400CE7544 /* ChooseFromMenu.swift in Sources */,
5502FFD721D0F1A400CE7544 /* ShortcutShareViewController.swift in Sources */,
Expand Down
18 changes: 18 additions & 0 deletions ShortcutsSwiftXcode.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

import ShortcutsSwift

let song = actionOutput()
let title = song.with(propertyName: .custom("Title"), userInfo: .string("title"))
let artist = song.with(propertyName: .custom("Artist"), userInfo: .string("artist"))
let albumArtwork = song.with(propertyName: .custom("Album Artwork"), userInfo: .string("artwork"))
let tweetText = actionOutput(name: "Tweet")

let shortcut = buildShortcut(comment("This Shortcut was generated in Swift.")
+ getCurrentSong().savingOutput(to: song)
+ getText("Listening to \(title) by \(artist) using Swift!").savingOutput(to: tweetText)
+ list(["\(tweetText)", "\(albumArtwork)"])
+ tweet()
)

//print(shortcut)
saveShortcut(shortcut, name: "tweet song swift")
17 changes: 17 additions & 0 deletions ShortcutsSwiftXcode.playground/Sources/Support.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import ShortcutsSwift
import Foundation
import PlaygroundSupport

public func saveShortcut(_ shortcut: PropertyList, name: String) {
let data = exportShortcut(shortcut)
func save(ext: String) {
let fileURL = URL(fileURLWithPath: "")
.appendingPathComponent(name)
.appendingPathExtension(ext)
try! data.write(to: fileURL)
print("open \(fileURL.deletingLastPathComponent().absoluteString.dropFirst(7))")
}
save(ext: "shortcut")
// TESTING
save(ext: "plist")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like to save a plist version of the shortcut to be able to open it and compare it with the original.

}
4 changes: 4 additions & 0 deletions ShortcutsSwiftXcode.playground/contents.xcplayground
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='ios' executeOnSourceChanges='false'>
<timeline fileName='timeline.xctimeline'/>
</playground>
9 changes: 9 additions & 0 deletions Tests/AggrandizementTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ class AggrandizementTests: XCTestCase {
)
}

func testCustomStringProperty() {
let result = actionOutput()
let title = result.with(propertyName: .custom("Title"), userInfo: .string("title"))
_ = buildShortcut(
getBatteryLevel().savingOutput(to: result)
+ showResult("Test Aggrandizements \(title)")
)
}

func testCoercion() {
let result = actionOutput()
let coercion = result.with(type: .article)
Expand Down