Skip to content

Instantly share code, notes, and snippets.

@abhibeckert
Last active August 29, 2015 14:08
Show Gist options
  • Save abhibeckert/711645229cba2bd9224f to your computer and use it in GitHub Desktop.
Save abhibeckert/711645229cba2bd9224f to your computer and use it in GitHub Desktop.
open/create file in swift
#!/usr/bin/swift
import Cocoa
// remove $0 from args
var args = Process.arguments
args.removeAtIndex(0)
// process input files
for arg in args {
// does the URL exist? ask to create it
let url: NSURL! = NSURL(fileURLWithPath:arg)
if !url.checkResourceIsReachableAndReturnError(nil) {
print("File doesn't exist: \(arg)\n Create it now? [y/N] ")
if getchar() != 121 { // y
exit(0)
}
"".writeToURL(url, atomically:true, encoding:NSUTF8StringEncoding, error: nil)
println("Created \(url.path!)")
}
// open in Dux
NSWorkspace.sharedWorkspace().openFile(url.path!, withApplication:"/Applications/Dux.app")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment