Skip to content

Instantly share code, notes, and snippets.

@saagarjha
Created May 7, 2018 10:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saagarjha/bfecf23d9b383e753a5fc53bc982ce78 to your computer and use it in GitHub Desktop.
Save saagarjha/bfecf23d9b383e753a5fc53bc982ce78 to your computer and use it in GitHub Desktop.
A small command line calculator for macOS, using Calculate.framework
import Foundation
let handle = dlopen("/System/Library/PrivateFrameworks/Calculate.framework/Calculate", RTLD_LAZY)
let CalculatePerformExpression = unsafeBitCast(dlsym(handle, "CalculatePerformExpression"), to:
(@convention(c) (UnsafePointer<CChar>, Int, Int, UnsafePointer<CChar>) -> Bool).self)
let expression = Array(CommandLine.arguments.dropFirst()).joined(separator: " ")
let answer = Array<CChar>(repeating: 0, count: 100)
if CalculatePerformExpression(expression, 16, 1, answer) {
print(String(cString: answer))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment