{"id":13688462,"url":"https://github.com/a2/swift-shortcuts","last_synced_at":"2025-04-06T11:08:33.398Z","repository":{"id":42443336,"uuid":"275711160","full_name":"a2/swift-shortcuts","owner":"a2","description":"An iOS 14 Shortcuts creator written in Swift, inspired by SwiftUI.","archived":false,"fork":false,"pushed_at":"2023-04-24T03:44:34.000Z","size":532,"stargazers_count":396,"open_issues_count":6,"forks_count":15,"subscribers_count":16,"default_branch":"main","last_synced_at":"2025-03-30T09:09:13.494Z","etag":null,"topics":["apple","automation","ios","ios14","shortcuts","swift"],"latest_commit_sha":null,"homepage":"https://a2.github.io/swift-shortcuts","language":"Swift","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/a2.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2020-06-29T02:26:58.000Z","updated_at":"2025-03-09T06:42:30.000Z","dependencies_parsed_at":"2023-02-11T18:30:53.678Z","dependency_job_id":"e3ef6491-93c9-4c12-ab23-a6a0caef7e73","html_url":"https://github.com/a2/swift-shortcuts","commit_stats":{"total_commits":81,"total_committers":2,"mean_commits":40.5,"dds":"0.012345679012345734","last_synced_commit":"e1a2f41bf55d3b32fab9a82bb48f967c3683ebec"},"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/a2%2Fswift-shortcuts","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/a2%2Fswift-shortcuts/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/a2%2Fswift-shortcuts/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/a2%2Fswift-shortcuts/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/a2","download_url":"https://codeload.github.com/a2/swift-shortcuts/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247471519,"owners_count":20944158,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["apple","automation","ios","ios14","shortcuts","swift"],"created_at":"2024-08-02T15:01:14.256Z","updated_at":"2025-04-06T11:08:33.379Z","avatar_url":"https://github.com/a2.png","language":"Swift","funding_links":[],"categories":["Swift"],"sub_categories":[],"readme":"# SwiftShortcuts\n\nAn iOS 14 Shortcuts creator written in Swift, inspired by SwiftUI.\n\n## Installation\n\nSwiftShortcuts is distributed using the [Swift Package Manager](https://swift.org/package-manager/). To install it into a project, add it as a dependency within your `Package.swift` manifest:\n\n```swift\nlet package = Package(\n    ...\n    dependencies: [\n        .package(url: \"https://github.com/a2/swift-shortcuts.git\", from: \"1.0.0\")\n    ],\n    ...\n)\n```\n\nThen import SwiftShortcuts in your project wherever you'd like to use it:\n\n```swift\nimport SwiftShortcuts\n```\n\n## Getting Started\n\nSwiftShorcuts was inspired by SwiftUI and, just as every SwiftUI `View` is made from other `View` types, so too is every `Shortcut` built from other `Shortcut` types. The only requirement of the `Shortcut` protocol is an instance property named `body` whose type is another `Shortcut`:\n\n```swift\n/// A type that represents a user workflow, or a part of one, in the Shortcuts app.\npublic protocol Shortcut {\n    /// The type of shortcut representing the body of this shortcut.\n    ///\n    /// When you create a custom shortcut, Swift infers this type from your\n    /// implementation of the required `body` property.\n    associatedtype Body: Shortcut\n\n    /// The contents of the shortcut.\n    var body: Body { get }\n}\n```\n\nTo start writing your own shortcut, create a type (such as a `struct`) that conforms to the Shortcut protocol. At first, return a `Comment` with some \"Hello, world!\" text.\n\n```swift\n// main.swift\n\nstruct HelloWorldShortcut: Shortcut {\n    var body: some Shortcut {\n        Comment(\"Hello, world!\")\n    }\n}\n```\n\nTo create a file that you can import into the Shortcuts app, call the `build()` function on your shortcut and write the `Data` to a file.\n\n```swift\n// continued from above\n\nlet shortcut = HelloWorldShortcut()\nlet data = try shortcut.build()\ntry data.write(to: URL(fileURLWithPath: \"Hello World.shortcut\"))\n```\n\nNow you can share (for example, via AirDrop) the _Hello World.shortcut_ file to your device and it will open in the Shortcuts app. Unfortunately iOS 13 does not support opening serialized `.shortcut` files.\n\n## Examples\n\n### Warn for Low Battery Level\n\nSaves the output of `BatteryLevel` shortcut to an `OutputVariable` and later references that value in a `ShowResult` shortcut.\n\n```swift\n// Swift 5.2\nimport SwiftShortcuts\n\nstruct BatteryLevelShortcut: Shortcut {\n    @OutputVariable var batteryLevel: Variable\n\n    var body: some Shortcut {\n        ShortcutGroup {\n            Comment(\"This Shortcut was generated in Swift.\")\n            BatteryLevel()\n                .savingOutput(to: $batteryLevel)\n            If(batteryLevel \u003c Number(20), then: {\n                SetLowPowerMode(true)\n                ShowResult(\"Your battery level is \\(batteryLevel)%; you might want to charge soon.\")\n            }, else: {\n                ShowResult(\"Your battery level is \\(batteryLevel)%; you're probably fine for now.\")\n            })\n        }\n    }\n}\n```\n\n### Clap Along\n\nTakes advantage of the `usingResult()` function to chain shortcut outputs to shortcut inputs.\n\n```swift\n// Swift 5.2\nimport SwiftShortcuts\n\nstruct ClapAlongShortcut: Shortcut {\n    var body: some Shortcut {\n        ShortcutGroup {\n            Comment(\"This Shortcut was generated in Swift.\")\n            AskForInput(prompt: \"WHAT 👏 DO 👏 YOU 👏 WANT 👏 TO 👏 SAY\")\n                .usingResult { providedInput in\n                    ChangeCase(variable: providedInput, target: .value(.uppercase))\n                }\n                .usingResult { changedCaseText in\n                    ReplaceText(variable: changedCaseText, target: \"[\\\\s]\", replacement: \" 👏 \", isRegularExpression: true)\n                }\n                .usingResult { updatedText in\n                    ChooseFromMenu(items: [\n                        MenuItem(label: \"Share\") {\n                            Share(input: updatedText)\n                        },\n                        MenuItem(label: \"Copy to Clipboard\") {\n                            CopyToClipboard(content: updatedText)\n                        },\n                    ])\n                }\n        }\n    }\n}\n```\n\n### Shorten with [small.cat](https://small.cat)\n\nA more complicated example that temporarily shortens a URL or some text with the small.cat service.\n\n```swift\n// Swift 5.2\nimport SwiftShortcuts\n\nstruct ShortenWithSmallCatShortcut: Shortcut {\n    @OutputVariable var url: Variable\n    @OutputVariable var expiry: Variable\n\n    var body: some Shortcut {\n        ShortcutGroup {\n            GetType(input: .shortcutInput)\n                .usingResult { type in\n                    If(type == \"URL\", then: {\n                        Text(\"\\(.shortcutInput)\")\n                    }, else: {\n                        GetClipboard()\n                    })\n                }\n                .usingResult { ifResult in\n                    URLEncode(input: \"\\(ifResult)\")\n                }\n                .savingOutput(to: $url)\n\n            ChooseFromMenu(prompt: \"Expires in:\", items: [\n                    MenuItem(label: \"10 minutes\") {\n                        Text(\"10\")\n                    },\n                    MenuItem(label: \"1 hour\") {\n                        Text(\"60\")\n                    },\n                    MenuItem(label: \"1 day\") {\n                        Text(\"1440\")\n                    },\n                    MenuItem(label: \"1 week\") {\n                        Text(\"10080\")\n                    },\n                ])\n                .savingOutput(to: $expiry)\n\n            GetContentsOfURL(method: .POST, url: \"https://small.cat/entries\", body: .form([\n                \"entry[duration]\": \"\\(expiry)\",\n                \"entry[value]\": \"\\(url)\",\n                \"utf8\": \"✓\",\n            ])).usingResult { contents in\n                GetURLsFromInput(input: \"\\(contents)\")\n            }.usingResult { urls in\n                FilterFiles(input: urls, filters: .all([\n                    NameFilter(beginsWith: \"http://small.cat/\"),\n                    NameFilter(isNot: \"http://small.cat/\"),\n                ]), limit: 1)\n            }.usingResult { url in\n                ChooseFromMenu(items: [\n                    MenuItem(label: \"Copy\") {\n                        CopyToClipboard(content: url)\n                    },\n                    MenuItem(label: \"Share\") {\n                        Share(input: url)\n                    },\n                    MenuItem(label: \"Show\") {\n                        ShowAlert(title: \"Small.cat\", message: \"\\(url)\", showsCancelButton: false)\n                    },\n                ])\n            }\n        }\n    }\n}\n```\n\n## Design and Goals\n\nSwiftShortcuts began as the similarly named [ShortcutsSwift](https://github.com/a2/shortcuts-swift) and was originally inspired by [Shortcuts JS](https://github.com/joshfarrant/shortcuts-js). Both SwiftShortcuts, and ShortcutsSwift before it, aimed to be in Swift what Shortcuts JS is for JavaScript.\n\nThe goal of this iteration of SwiftShortcuts is to make writing Shortcuts app workflows in Swift as easy as composing `View`s in SwiftUI. As you can see [above](#getting-started), even the base `Shortcut` protocol is heavily inspired by SwiftUI's `View` protocol.\n\nThis repository does not contain every possible Shortcuts-supported action or every possible permutation of parametres for those shortcuts. Please feel free to [contribute](#contributions-and-support) missing shortcut types and even test cases.\n\n## License\n\nSwiftShortcuts is available under the MIT license. See the [LICENSE](LICENSE) file for more info.\n\n## Contributions and Support\n\n*Inspired by the support model behind [Publish](https://github.com/JohnSundell/Publish).*\n\nSwiftShortcuts is developed completely in the open, and your contributions are more than welcome.\n\nThis project does not come with GitHub Issues-based support, and users are instead encouraged to become active participants in its continued development — by fixing any bugs that they encounter, or by improving the documentation wherever it's found to be lacking.\n\nIf you wish to make a change, [open a Pull Request](https://github.com/a2/swift-shortcuts/pull/new) — even if it just contains a draft of the changes you're planning, or a test that reproduces an issue — and we can discuss it further from there.\n\nHope you'll enjoy using SwiftShortcuts!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fa2%2Fswift-shortcuts","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fa2%2Fswift-shortcuts","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fa2%2Fswift-shortcuts/lists"}