Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read a file in a macOS Command Line Tool project

I've added a few JSON files to my macOS Command Line Tool project, but I can't seem to find them the usual way.

if let path = Bundle.main.path(forResource: "Data", ofType: "json")
{
    if let contents = try? String(contentsOfFile: path)
    {
        print (contents)
    }
    else { print("Could not load contents of file") }
}
else { print("Could not find path") }

This code works absolutely fine when used in a View based application, but always prints "Could not find path" in the command line tool.

Does anyone know what I'm doing wrong?

P.S: Fully aware that I should be using guard statements for this, but the code isn't in a function, just faffing about in main.swift till I figure this out.

like image 238
XmasRights Avatar asked May 07 '17 01:05

XmasRights


People also ask

How do you open a file in Mac command line?

In the Terminal app on your Mac, invoke a command-line editor by typing the name of the editor, followed by a space and then the name of the file you want to open. If you want to create a new file, type the editor name, followed by a space and the pathname of the file.

How do you use the command line on a Mac?

In the Terminal app on your Mac, press the Up Arrow key. The last command you entered appears on the command line. Continue pressing the Up Arrow key until you see the command you want, then press Return.

Where is command line on Mac?

The Mac command line is a program called Terminal. It lives in the /Applications/Utilities/ folder. To find it, go to your Applications folder. Near the bottom, there is a folder called Utilities.

How do I use the command line on a Mac?

You can use the command-line environment interactively by typing a command and waiting for a result, or you can use the shell to compose scripts that run without direct interaction. In the Terminal app on your Mac, enter the complete pathname of the tool’s executable file, followed by any needed arguments, then press Return.

How do I run mycommandlineprog on a Mac?

For example, to run MyCommandLineProg, use the following: To open an app, use the open command: When entering commands, if you get the message command not found, check your spelling. Here’s an example: In the Terminal app on your Mac, click the Terminal window that is running the command you want to terminate.

Can I embed a command-line tool in my Mac App?

When building an app for the Mac, you can embed a command-line tool in the app to act as a helper tool. Instances where this may be helpful include, for example: You want to run some code in a separate process. In many cases, an XPC service is a better choice for this, but sometimes it’s easier to embed a command-line tool.

How do I run a command in a folder in Linux?

To run a command in the current user’s home folder, precede it with the folder specifier. For example, to run MyCommandLineProg, use the following: To open an app, use the open command:


2 Answers

You can create a separate bundle, place the files there, and then load them without having to worry about their individual URLs.

Let's do it:

  1. In Xcode, click on File --> New --> Target...
  2. Click on the macOS tab at the top
  3. Scroll down to the Framework & Library, select Bundle, click Next. Give a name to the new bundle (let's say JSONMocks).
  4. Add files to this bundle. Add the file to Xcode, then make sure that the bundle is ticked in its target membership section:

    target membership for file in bundle

  5. Add the bundle to your target. In the target's Build Phases, open the Copy Files phase and click the + button. Select the bundle and click Add:

    enter image description here

  6. Programmatically access the contents of your bundle thus:

let currentDirectoryURL = URL(fileURLWithPath: FileManager.default.currentDirectoryPath)
let bundleURL = URL(fileURLWithPath: "JSONMocks.bundle", relativeTo: currentDirectoryURL)
let bundle = Bundle(url: bundleURL)
let jsonFileURL = bundle!.url(forResource: jsonFileName, withExtension: "json")!
like image 193
Eric Avatar answered Nov 15 '22 16:11

Eric


There is no app bundle for a command-line tool. Just a binary.

You need to put the JSON file in a known location (current directory?) and construct the path yourself

like image 29
Lou Franco Avatar answered Nov 15 '22 15:11

Lou Franco