0

I have my Mac OS application, that does some job with image you choose in UI application. I want to find the best solution, how use the same sources, but run my app from CLI like

open MyApp.app image=$IMAGE_URL

And do all the job in background, without invoking UI component. If use "open" command, how access my arguments from sources and how avoid UI invoking? If it is not correct solution, please, provide any others.

Orest Savchak
  • 4,529
  • 1
  • 18
  • 27
  • Use `open -a MyApp.app --args image="$IMAGE_URL"` to pass the arguments to MyApp.app. Type `man open` in Terminal for details. – m7thon Sep 01 '15 at 16:55
  • And how access this arguments in the application? – Orest Savchak Sep 01 '15 at 17:13
  • That depends on the programming language. In C style languages, the `main( int argc, char *argv[] )` function gets called and the command line arguments are passed in `argv`. EDIT: Just say the swift tag. See [here](http://stackoverflow.com/questions/24029633/how-do-you-access-command-line-arguments-in-swift). – m7thon Sep 01 '15 at 17:21

1 Answers1

1

You can use Swift for a CLI application. Put this at the top of your Swift script:

#!/usr/bin/env xcrun swift

And useProcess.arguments to get what has been passed to the app:

#!/usr/bin/env xcrun swift

for argument in Process.arguments {
      println(argument)
} 

Make the file executable in Terminal:

chmod +x testCLI.swift

Call it with args:

./testCLI.swift arg1 arg2

Prints:

./testCLI.swift
arg1
arg2

The first element of the Process.arguments array is always the script path, args start at index 1.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
  • I'll test later, but I think it is what I need. – Orest Savchak Sep 01 '15 at 13:21
  • Okay, actually, it is almost that. But I can not find out how to use my another classes in this Swift file. – Orest Savchak Sep 01 '15 at 14:16
  • Yeah I should have added this in my answer, forgot to: my example is for a *script app*. Not a "full" app with multiple files and/or frameworks. I don't have references at hand but I think you can not *easily* build a pure Swift CLI app with multiple files as in an Xcode project (but I could be wrong of course). There's [libraries](https://github.com/jatoben/CommandLine) to help you build a more complex CLI app, though. – Eric Aya Sep 01 '15 at 14:27
  • Actually, I need only one file, that does my work. I use it in my application. I think, I can create module from this file, and then I can use it as import in both script and application. – Orest Savchak Sep 01 '15 at 14:29
  • Then don't hesitate to write your own answer to this question when you've found how to do this, it could be helpful for others (and for me). – Eric Aya Sep 01 '15 at 14:32