Last active
August 29, 2015 14:08
-
-
Save abhibeckert/711645229cba2bd9224f to your computer and use it in GitHub Desktop.
open/create file in swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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