Latest 1.1.0
Homepage https://github.com/mayankk2308/swipt
License MIT
Platforms osx 10.10
Authors

Header
Swipt is an easy-to-use API for macOS Yosemite or later, and enables developers to execute shell scripts directly in Swift. This allows for some interesting special use cases (especially for non-sandboxed applications), and may be used as a suitable alternative to more advanced techniques for simple tasks.

Installation

You have multiple options to install Swipt:

Cocoapods

In your Podfile, add:

pod 'Swipt'

Carthage

In your Cartfile, add:

github "mayankk2308/swipt"

Basics

Import Swipt module:

import Swipt

Initialize management object as follows:

let swiptManager = SwiptManager()

To execute scripts, use one of the execute methods:

swiptManager.execute(...)

Features and Options

You can specify the privilege level you need using Privileges:

  • user – standard user-level privileges (default)
  • admin – superuser privileges (macOS UI will prompt for password automatically)

You can specify the shell type you need using ShellType:

  • sh – Shell (default)
  • bash – Bourne Again Shell
  • ksh – Korn Shell
  • csh – C Shell
  • zsh – Z Shell
  • tcsh – T Shell

Usage

There are many ways to execute scripts in Swipt for as much flexibility as possible.

Single-Threaded Low-Perf Workloads

You can execute unix commands by simply passing in a textual representation using:

let sampleUnixCommand = "echo hello"
swiptManager.execute(unixScriptText: sampleUnixCommand)

You can specify privileges easily:

swiptManager.execute(unixScriptText: sampleUnixCommand, withPrivilegeLevel: .admin)

You can easily handle script outputs and errors:

swiptManager.execute(unixScriptText: sampleUnixCommand) { error, output in
    // handle errors & output
}

You can provide script files, which is more robust and portable:

let scriptFile = "/path/to/script.sh"
swiptManager.execute(unixScriptFile: scriptFile)

You can specify privileges in a similar manner shown earlier, but for files, you may additionally specify the shell type:

swiptManager.execute(unixScriptFile: scriptFile, withShellType: .bash)

Completion handlers work in a similar manner as shown earlier. You can also directly execute AppleScript as text.

Multi-Threaded High-Perf Workloads

In most cases, it is optimal to execute scripts on a separate thread. Swipt allows you to do this with no extra work:

let sampleUnixCommand = "echo hello"
swiptManager.asyncExecute(unixScriptText: sampleUnixCommand)

Just use the async variants of the single-threaded functions. Completion handlers work in exactly the same way as they do for single-threaded calls, as long as the parent process (typically an Application) is alive.

Additionally, you may execute batches of scripts, each with their own set of privileges ([Privileges]), shell types ([ShellType]), and arguments ([[String]]), all optional:

let scriptBatch = [..String (file paths)..]()
swiptManager.executeSerialBatch(unixScriptFiles: scriptBatch)
// or provide arguments, shell types, and Privileges
let privilegeLevels = [...Privileges...]
swiptManager.executeSerialBatch(unixScriptFiles: scriptBatch, withPrivilegeLevels: privilegeLevels, ...)

This function does not support a completion handler at the moment. A more comprehensive threading system will be implemented at a later date. As of now, a single off-main serial queue is supported for scripts.

License

This project is available under the MIT license. Please see the license file for more information.

Latest podspec

{
    "name": "Swipt",
    "version": "1.1.0",
    "platforms": {
        "osx": "10.10"
    },
    "license": {
        "type": "MIT",
        "file": "LICENSE.md"
    },
    "source": {
        "git": "https://github.com/mayankk2308/swipt.git",
        "tag": "1.1.0"
    },
    "authors": {
        "Mayank Kumar": "[email protected]"
    },
    "homepage": "https://github.com/mayankk2308/swipt",
    "swift_version": "4.2",
    "summary": "Swipt is an easy-to-use API that enables developers to execute shell scripts directly in Swift.",
    "source_files": "Swipt/*.swift"
}

Pin It on Pinterest

Share This