19

I want to develop an app to promote the experience when using terminal on Mac. And I want to get the current working directory (cwd) from terminal. How can I implement it?

I notice that the answer in Get terminal output after a command swift is really good but it seems that it still cannot resolve my problem perfectly. I learned Process().currentDirectoryPath from Apple's document https://developer.apple.com/reference/foundation/process Does it do the trick that I need? Can I use it like the following?

let path = Process().currentDirectoryPath

I am very new to swift and Xcode, please help me! Thanks!


Update: Thank you guys! It seems that both

let path = fileManager.default.currentDirectoryPath

and the Process() one temporarily work for me. Are there any differences between these two?

Seaky Lone
  • 992
  • 1
  • 10
  • 29
  • What exactly are you trying to achieve with this directory? – Alexander May 26 '17 at 03:30
  • @Alexander I intend to get the path and sketch all the elements in the directory out, like making a GUI. I am a student and just want to have a small project to practice. – Seaky Lone May 26 '17 at 03:36
  • 1
    How about `FileManager.default.currentDirectoryPath`? – Pang May 26 '17 at 03:38
  • @Pang Which one should I choose? The fileManager one or the process one? I just got my Macbook and I am still exploring how to use xcode. – Seaky Lone May 26 '17 at 03:45
  • Did you try the code you posted? What did you get? – Pang May 26 '17 at 03:48
  • @Pang I am so sorry I didn't. As I said, I am still exploring its usage... Besides, I am trying to figure out anthoer little bug... – Seaky Lone May 26 '17 at 03:53

1 Answers1

33

I had this same question and went with the latter option:

let path = FileManager.default.currentDirectoryPath

To answer your updated question, the difference between the Process() and the FileManager approaches is that the Process().currentDirectoryPath approach prepares to create a new subprocess and then tells you the working directory that subprocess will use. The subprocess would have the same working directory as the parent process, of course, but it's wasteful to create a new Process instance just to find the working directory.

Definitely use the FileManager approach, as that returns the working directory that is used by the current process without creating any subprocesses or any other overhead.

Sources:
https://developer.apple.com/documentation/foundation/filemanager/1409234-default
https://developer.apple.com/documentation/foundation/filemanager/1410766-currentdirectorypath

Eric Ferreira
  • 1,811
  • 18
  • 20
  • Still seems to work completely as it should, yet it seems the answer got some downvote? Why the downvote? – Eric Ferreira Jul 10 '18 at 14:07
  • 1
    For the sake of the argument, AFAIK simply declaring a Process var won't actually fork a new subprocess, that won't happen until you start() it. Nonetheless, you're right, the FileManager approach is the proper way to do it, even if Process() just wastes a temporary variable. – Guillaume Laurent Apr 30 '19 at 11:02
  • Good observation, Guillaume Laurent – Eric Ferreira Apr 30 '19 at 15:32
  • 1
    `FileManager.default.currentDirectoryPath` does not work with sandboxed apps: https://stackoverflow.com/questions/31476313/getting-current-working-directory-from-a-sandboxed-command-line-app/31488956#comment114709909_31488956 – Daniel Nov 17 '20 at 22:11