I\'m writing a script in Swift, and I want it to modify some files that always exist in the same directory as the script itself. Is there a way to get the path to the script fro
The accepted answer doesn't work in Swift 3. Also, this is a more straightforward approach:
import Foundation
let currentDirectoryURL = URL(fileURLWithPath: FileManager.default.currentDirectoryPath)
let url = URL(fileURLWithPath: CommandLine.arguments[1], relativeTo: currentDirectoryURL)
print("script at: " + url.path)
However, it has the same problem pointed out by @rob-napier, if the script's directory is in your PATH.