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.
You have multiple options to install Swipt:
In your Podfile, add:
pod 'Swipt'
In your Cartfile, add:
github "mayankk2308/swipt"
Import Swipt module:
import Swipt
Initialize management object as follows:
let swiptManager = SwiptManager()
To execute scripts, use one of the execute
methods:
swiptManager.execute(...)
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
There are many ways to execute scripts in Swipt for as much flexibility as possible.
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.
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.
This project is available under the MIT license. Please see the license file for more information.