NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

How do you get current directory for command line tools?

Forums > macOS

For fun, I'm writing a Swift command line tool for macOS. I want to pass in an argument like "foo.txt", which would be a text file in the current directory. I'm unclear on how the app will know what directory the shell is currently in. How do you pass the current directory to the command line apps implicity so the user does not have to specify them?

1      

I think FileManager.currentDirectoryPath is what you are looking for.

2      

Thank you, David. I tested it and you are correct!

I disregarded currentDirectoryPath initially because the documentation says: "The path to the program’s current directory."

If I'd read the full docs, I'd have seen the warning: "This property reports the current working directory for the current process, not just the receiver."

Which is exactly what I wanted, thanks again!

1      

Hacking with Swift is sponsored by RevenueCat

SPONSORED In-app subscriptions are a pain to implement, hard to test, and full of edge cases. RevenueCat makes it straightforward and reliable so you can get back to building your app. Oh, and it's free if your app makes less than $10k/mo.

Learn more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.