From f66de0eb493a02d8bbb644ccead6fb0bd1a32567 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Marti=CC=81nez?= Date: Wed, 2 Jan 2019 00:45:28 +0100 Subject: [PATCH 1/5] Added xcode playground --- .../Contents/Sources/BuildShortcut.swift | 6 +++++- ShortcutsSwift.xcodeproj/project.pbxproj | 2 ++ ShortcutsSwiftXcode.playground/Contents.swift | 16 ++++++++++++++++ .../contents.xcplayground | 4 ++++ 4 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 ShortcutsSwiftXcode.playground/Contents.swift create mode 100644 ShortcutsSwiftXcode.playground/contents.xcplayground diff --git a/ShortcutsSwift.playgroundbook/Contents/Sources/BuildShortcut.swift b/ShortcutsSwift.playgroundbook/Contents/Sources/BuildShortcut.swift index c3e28a1..c9927ec 100644 --- a/ShortcutsSwift.playgroundbook/Contents/Sources/BuildShortcut.swift +++ b/ShortcutsSwift.playgroundbook/Contents/Sources/BuildShortcut.swift @@ -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 { + 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)])) } diff --git a/ShortcutsSwift.xcodeproj/project.pbxproj b/ShortcutsSwift.xcodeproj/project.pbxproj index 0039229..b311aa3 100644 --- a/ShortcutsSwift.xcodeproj/project.pbxproj +++ b/ShortcutsSwift.xcodeproj/project.pbxproj @@ -86,6 +86,7 @@ 98D33C2F21D3EFF100824660 /* Calculate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Calculate.swift; sourceTree = ""; }; 98D33C3321D4067C00824660 /* List.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = List.swift; sourceTree = ""; }; FBDA8D4221DCCA9E00303374 /* AggrandizementTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AggrandizementTests.swift; sourceTree = ""; }; + FB7EE87321DC326B00C079C7 /* ShortcutsSwiftXcode.playground */ = {isa = PBXFileReference; lastKnownFileType = file.playground; path = ShortcutsSwiftXcode.playground; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.swift; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -120,6 +121,7 @@ 5502FF7C21D0F06500CE7544 = { isa = PBXGroup; children = ( + FB7EE87321DC326B00C079C7 /* ShortcutsSwiftXcode.playground */, 5502FFF221D0F58400CE7544 /* ShortcutsSwift.playgroundbook */, 5502FFED21D0F2AB00CE7544 /* Helpers */, 5502FFC021D0F1A300CE7544 /* Sources */, diff --git a/ShortcutsSwiftXcode.playground/Contents.swift b/ShortcutsSwiftXcode.playground/Contents.swift new file mode 100644 index 0000000..fcec27b --- /dev/null +++ b/ShortcutsSwiftXcode.playground/Contents.swift @@ -0,0 +1,16 @@ + +import ShortcutsSwift + +let batteryLevel = actionOutput() +let shortcut = buildShortcut( + comment("This Shortcut was generated in Swift.") + + getBatteryLevel().savingOutput(to: batteryLevel) + + ifLessThan(20, ifTrue: ( + setLowPowerMode(true) + + showResult("Your battery is at \(batteryLevel)%, you might want to charge it.") + ), ifFalse: ( + showResult("Your battery is at \(batteryLevel)%, you're probably fine for now.") + )) +) + +print(shortcut) diff --git a/ShortcutsSwiftXcode.playground/contents.xcplayground b/ShortcutsSwiftXcode.playground/contents.xcplayground new file mode 100644 index 0000000..9f5f2f4 --- /dev/null +++ b/ShortcutsSwiftXcode.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file From a4ea1d1a3441065d51cea87aa348875b138e4a9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Marti=CC=81nez?= Date: Wed, 2 Jan 2019 00:56:25 +0100 Subject: [PATCH 2/5] save shortcut to file to send it to iOS --- ShortcutsSwiftXcode.playground/Contents.swift | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/ShortcutsSwiftXcode.playground/Contents.swift b/ShortcutsSwiftXcode.playground/Contents.swift index fcec27b..8254817 100644 --- a/ShortcutsSwiftXcode.playground/Contents.swift +++ b/ShortcutsSwiftXcode.playground/Contents.swift @@ -1,6 +1,22 @@ 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") +} + let batteryLevel = actionOutput() let shortcut = buildShortcut( comment("This Shortcut was generated in Swift.") + @@ -13,4 +29,5 @@ let shortcut = buildShortcut( )) ) -print(shortcut) +//print(shortcut) +saveShortcut(shortcut, name: "batery") From 9e403f17505afe47c012535cbc7c6981841f407b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Marti=CC=81nez?= Date: Wed, 2 Jan 2019 12:11:31 +0100 Subject: [PATCH 3/5] Add string property --- .../Contents/Sources/Aggrandizement.swift | 2 ++ Tests/AggrandizementTests.swift | 9 +++++++++ 2 files changed, 11 insertions(+) diff --git a/ShortcutsSwift.playgroundbook/Contents/Sources/Aggrandizement.swift b/ShortcutsSwift.playgroundbook/Contents/Sources/Aggrandizement.swift index e21fa90..107ee33 100644 --- a/ShortcutsSwift.playgroundbook/Contents/Sources/Aggrandizement.swift +++ b/ShortcutsSwift.playgroundbook/Contents/Sources/Aggrandizement.swift @@ -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 } } } diff --git a/Tests/AggrandizementTests.swift b/Tests/AggrandizementTests.swift index 29d4369..7c7786c 100644 --- a/Tests/AggrandizementTests.swift +++ b/Tests/AggrandizementTests.swift @@ -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) From b9f706c7f02a3bdbe40ec7134d729d02f414bcf9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Marti=CC=81nez?= Date: Thu, 3 Jan 2019 12:29:18 +0100 Subject: [PATCH 4/5] Build tweet current song --- .../Contents/Sources/InterpolatedText.swift | 2 +- ShortcutsSwiftXcode.playground/Contents.swift | 43 ++++++++++++++----- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/ShortcutsSwift.playgroundbook/Contents/Sources/InterpolatedText.swift b/ShortcutsSwift.playgroundbook/Contents/Sources/InterpolatedText.swift index 57d571b..be4ef20 100644 --- a/ShortcutsSwift.playgroundbook/Contents/Sources/InterpolatedText.swift +++ b/ShortcutsSwift.playgroundbook/Contents/Sources/InterpolatedText.swift @@ -1,6 +1,6 @@ import Foundation -func withInterpolatedText(_ string: String) -> Any { +public func withInterpolatedText(_ string: String) -> Any { var result = string var attachments = [String: Any]() diff --git a/ShortcutsSwiftXcode.playground/Contents.swift b/ShortcutsSwiftXcode.playground/Contents.swift index 8254817..bbcd9fc 100644 --- a/ShortcutsSwiftXcode.playground/Contents.swift +++ b/ShortcutsSwiftXcode.playground/Contents.swift @@ -17,17 +17,38 @@ public func saveShortcut(_ shortcut: PropertyList, name: String) { save(ext: "plist") } -let batteryLevel = actionOutput() -let shortcut = buildShortcut( - comment("This Shortcut was generated in Swift.") + - getBatteryLevel().savingOutput(to: batteryLevel) + - ifLessThan(20, ifTrue: ( - setLowPowerMode(true) + - showResult("Your battery is at \(batteryLevel)%, you might want to charge it.") - ), ifFalse: ( - showResult("Your battery is at \(batteryLevel)%, you're probably fine for now.") - )) +public func getCurrentSong() -> ActionWithOutput { + return Action(identifier: "is.workflow.actions.getcurrentsong", parameters: [:]) +} + +public func getText(_ text: String) -> ActionWithOutput { + return Action( + identifier: "is.workflow.actions.gettext", + parameters: ["WFTextActionText": withInterpolatedText(text)] + ) +} + +public func tweet(showComposeSheet: Bool = true) -> Action { + return Action( + identifier: "is.workflow.actions.tweet", + parameters: [ + "WFSocialActionShowComposeSheet": showComposeSheet, + ] + ) +} + +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: "batery") +saveShortcut(shortcut, name: "tweet song swift") From fb7b86639c86d0613f841efde3c30482acd822a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Marti=CC=81nez?= Date: Thu, 3 Jan 2019 12:48:33 +0100 Subject: [PATCH 5/5] Clean up and moved actions to the framework --- .../Sources/Actions/GetCurrentSong.swift | 5 +++ .../Contents/Sources/Actions/GetText.swift | 8 +++++ .../Contents/Sources/Actions/Twitter.swift | 10 ++++++ .../Contents/Sources/InterpolatedText.swift | 2 +- ShortcutsSwift.xcodeproj/project.pbxproj | 12 +++++++ ShortcutsSwiftXcode.playground/Contents.swift | 36 ------------------- .../Sources/Support.swift | 17 +++++++++ 7 files changed, 53 insertions(+), 37 deletions(-) create mode 100644 ShortcutsSwift.playgroundbook/Contents/Sources/Actions/GetCurrentSong.swift create mode 100644 ShortcutsSwift.playgroundbook/Contents/Sources/Actions/GetText.swift create mode 100644 ShortcutsSwift.playgroundbook/Contents/Sources/Actions/Twitter.swift create mode 100644 ShortcutsSwiftXcode.playground/Sources/Support.swift diff --git a/ShortcutsSwift.playgroundbook/Contents/Sources/Actions/GetCurrentSong.swift b/ShortcutsSwift.playgroundbook/Contents/Sources/Actions/GetCurrentSong.swift new file mode 100644 index 0000000..26d765d --- /dev/null +++ b/ShortcutsSwift.playgroundbook/Contents/Sources/Actions/GetCurrentSong.swift @@ -0,0 +1,5 @@ +import Foundation + +public func getCurrentSong() -> ActionWithOutput { + return Action(identifier: "is.workflow.actions.getcurrentsong", parameters: [:]) +} diff --git a/ShortcutsSwift.playgroundbook/Contents/Sources/Actions/GetText.swift b/ShortcutsSwift.playgroundbook/Contents/Sources/Actions/GetText.swift new file mode 100644 index 0000000..326d5e9 --- /dev/null +++ b/ShortcutsSwift.playgroundbook/Contents/Sources/Actions/GetText.swift @@ -0,0 +1,8 @@ +import Foundation + +public func getText(_ text: String) -> ActionWithOutput { + return Action( + identifier: "is.workflow.actions.gettext", + parameters: ["WFTextActionText": withInterpolatedText(text)] + ) +} diff --git a/ShortcutsSwift.playgroundbook/Contents/Sources/Actions/Twitter.swift b/ShortcutsSwift.playgroundbook/Contents/Sources/Actions/Twitter.swift new file mode 100644 index 0000000..4aaa23d --- /dev/null +++ b/ShortcutsSwift.playgroundbook/Contents/Sources/Actions/Twitter.swift @@ -0,0 +1,10 @@ +import Foundation + +public func tweet(showComposeSheet: Bool = true) -> Action { + return Action( + identifier: "is.workflow.actions.tweet", + parameters: [ + "WFSocialActionShowComposeSheet": showComposeSheet, + ] + ) +} diff --git a/ShortcutsSwift.playgroundbook/Contents/Sources/InterpolatedText.swift b/ShortcutsSwift.playgroundbook/Contents/Sources/InterpolatedText.swift index be4ef20..57d571b 100644 --- a/ShortcutsSwift.playgroundbook/Contents/Sources/InterpolatedText.swift +++ b/ShortcutsSwift.playgroundbook/Contents/Sources/InterpolatedText.swift @@ -1,6 +1,6 @@ import Foundation -public func withInterpolatedText(_ string: String) -> Any { +func withInterpolatedText(_ string: String) -> Any { var result = string var attachments = [String: Any]() diff --git a/ShortcutsSwift.xcodeproj/project.pbxproj b/ShortcutsSwift.xcodeproj/project.pbxproj index b311aa3..979156f 100644 --- a/ShortcutsSwift.xcodeproj/project.pbxproj +++ b/ShortcutsSwift.xcodeproj/project.pbxproj @@ -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 */ @@ -85,6 +88,9 @@ 98D33C2A21D3E34100824660 /* Repeat.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Repeat.swift; sourceTree = ""; }; 98D33C2F21D3EFF100824660 /* Calculate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Calculate.swift; sourceTree = ""; }; 98D33C3321D4067C00824660 /* List.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = List.swift; sourceTree = ""; }; + FB651C2621DE29D20029DAE6 /* Twitter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Twitter.swift; sourceTree = ""; }; + FB651C2821DE2A810029DAE6 /* GetText.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GetText.swift; sourceTree = ""; }; + FB651C2A21DE2AAF0029DAE6 /* GetCurrentSong.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GetCurrentSong.swift; sourceTree = ""; }; FBDA8D4221DCCA9E00303374 /* AggrandizementTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AggrandizementTests.swift; sourceTree = ""; }; FB7EE87321DC326B00C079C7 /* ShortcutsSwiftXcode.playground */ = {isa = PBXFileReference; lastKnownFileType = file.playground; path = ShortcutsSwiftXcode.playground; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.swift; }; /* End PBXFileReference section */ @@ -186,6 +192,9 @@ 5502FFD021D0F1A400CE7544 /* Share.swift */, 5502FFD121D0F1A400CE7544 /* ShowResult.swift */, 98D33C2821D3DA0A00824660 /* SplitText.swift */, + FB651C2821DE2A810029DAE6 /* GetText.swift */, + FB651C2A21DE2AAF0029DAE6 /* GetCurrentSong.swift */, + FB651C2621DE29D20029DAE6 /* Twitter.swift */, ); path = Actions; sourceTree = ""; @@ -334,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 */, diff --git a/ShortcutsSwiftXcode.playground/Contents.swift b/ShortcutsSwiftXcode.playground/Contents.swift index bbcd9fc..a577033 100644 --- a/ShortcutsSwiftXcode.playground/Contents.swift +++ b/ShortcutsSwiftXcode.playground/Contents.swift @@ -1,42 +1,6 @@ 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") -} - -public func getCurrentSong() -> ActionWithOutput { - return Action(identifier: "is.workflow.actions.getcurrentsong", parameters: [:]) -} - -public func getText(_ text: String) -> ActionWithOutput { - return Action( - identifier: "is.workflow.actions.gettext", - parameters: ["WFTextActionText": withInterpolatedText(text)] - ) -} - -public func tweet(showComposeSheet: Bool = true) -> Action { - return Action( - identifier: "is.workflow.actions.tweet", - parameters: [ - "WFSocialActionShowComposeSheet": showComposeSheet, - ] - ) -} - let song = actionOutput() let title = song.with(propertyName: .custom("Title"), userInfo: .string("title")) let artist = song.with(propertyName: .custom("Artist"), userInfo: .string("artist")) diff --git a/ShortcutsSwiftXcode.playground/Sources/Support.swift b/ShortcutsSwiftXcode.playground/Sources/Support.swift new file mode 100644 index 0000000..234a14f --- /dev/null +++ b/ShortcutsSwiftXcode.playground/Sources/Support.swift @@ -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") +}