I'm about to start a new Cocoa project from scratch and one of the requirements I have already been given is being able to choose either a Cocoa GUI or a command line mode from the same executable. Scriptability may be a possible addition in the future but that is not a concern right now.
What is the best strategy for going about this in Cocoa, especially if I want to maintain certain Cocoa features like Obj-C garbage collection?
Xcode will generate a main.m for you with a main function that has the command line arguments. You should parse them and if you need to stay in command-line mode, never do the application startup. If you are being a GUI, just start normally.
Related
So I'm basically trying to recreate what this app: Fluid, already does. I want to write a Ruby script, or MacRuby app that accepts a few parameters and can generate a simple native app.
Since their code is not open-sourced, I can't see how they do it. And I don't know how I would begin to accomplish something like this.
Also, I would like this script to be run on Windows (not that the user could install the generated app, but so the app could be distributed to Mac users).
How could I do something like this?
Solution:
Here is a project that does exactly the same thing that I'm trying to do. It takes an app bundle and does some string replacing on some files in the bundle. I'm going to use it as an example to imitate.
https://github.com/maccman/macgap-rb
MacRuby can already create native app bundles on OS X (it's a compiler as well as an interpreter), so in a sense there's no question to be answered here. If you want to write an app in MacRuby or Objective-C for OS X, the experience is essentially the same (though, of course, MacRuby has different command line flags for generating the final result, in this case the -deploy flag to MacRuby vs some linker invocation for ObjC). That said, nothing you write in MacRuby will run natively in Windows. Depending on the complexity of the app you have in mind, you may have to go to some cross-platform solution (like Unity) for that.
Check out Prism. It's not Ruby, but it does exactly what you describe and is open-source. One thing you'll to do is embed a web browser into a window, so look for libraries that do that. I'm assuming you'll use Cocoa for GUI since you're using MacRuby. In the end, the simplest way would be just have a window with web browser in it.
I'm quite new to the Mac but reasonably expirienced with windows.
What I need is a window owned opened and closed by the library (dylib).
This is very easy to do in MS Windows but seems not be hard under Cocoa.
I used the code from apple's 'CocoaInCarbon' example with C++ Wrappers.
But the NSApplicationLoad() followed by [NSBundle LoadNibNamed:#"MyWindow" owner:self] fails. Is this caused by the dylib not being a bundle.
Are there alternative ways to open and control a window within a dylib?
Yes, the problem is that it's not a bundle; your nib can't possibly be inside a bundle that doesn't exist, so how would the code find it to open it?
You need to either use a framework (which is a bundle), and ship the nib in that framework, or you need to create the entire window programmatically in your dylib.
It's not a standard practice to just ship dylib on Mac, when the shared library is not very low level and involves GUI in particular. You package it into a framework, so that it can not only have codes in it, but associated resources (nibs, images, sounds) in one place.
Read this Apple document to understand what's going on and how to prepare it. Or, take a look at /System/Library/Frameworks/ to see how the OS X itself provides libraries.
I'm dying to know how I can make a GUI for ffmpeg and jhead in OSX. I've been looking for a solution for a while and thought you, stackoverflow's users, could help me. Maybe you know some document I haven't come across of or, better, a tutorial to make a GUI.
I love those two tools but I like the simplicity of drag/drop operations.
Note: I don't need a GUI for them, I want to make one.
There is a tutorial for wrapping command-line tools using NSTask, on the Cocoa Dev Central site:
Wrapping UNIX Commands
Wrapping UNIX Commands Part II
It's a few years old now, but should get you started.
If you are asking "How do I create a GUI application in Mac OS X that interfaces to a command line tool" the answer is NSTask. Although, if the command line tool provides a programming API, using that would be preferable to invoking the command line tool itself.
If you are asking "How do I create a GUI application in Mac OS X" the answer is to read a book about it and look at the Apple tutorial docs. Cocoa Programming on Mac OS X by Aaron Hillegass was my starting point.
You can use a scripting language like Tcl, Python or Ruby with a toolkit like Tk which uses native widgets on the mac.
First create a Modal Dialog NIB with the needed GUI.
When called in C , create an NSReleasePool, and then the magic sauce.
[NSApplication sharedApplication]
ProcessSerialNumber psn;
GetCurrentProcess( &psn );
TransformProcessType(&psn,kProcessTransformToForegroundApplication);
SetFrontProcess( &psn );
Later after you load the NIB from the Bundle, issue
[NSApp runModalForWindow:[controller window]];
[[controller window] close];
Without the TransfromProcessType(), the Terminal app will get keystrokes, not the Modal Dialog.
This may not be the best answer but in the book "Xcode unleashed" is a chapter how to embed a Command Line Tool inside a Cocoa Application. Maybe you should have a look. Nice book anyway.
On your Leopard/Snow Leopard disk you can find XCode, but you can also download it from the Apple Developer Community. XCode comes with the Interface Builder, which lets you build GUI's and you can rig to your commands using the Cocoa framework.
You are too late, there is already a GUI frontend for ffmpeg called ffmpegX, but anyway, you would create a model that either wraps or uses the library or executable.... if it uses the executable you can use popen to invoke the executable, write to its STDIN, and read from its STDOUT. The view and controller would be basically the same as you would design it for any other GUI application. Since this is a Cocoa post, you could use Objective-C and Cocoa to the make the GUI, but it really can be implemented in any language.
I developed a command line application, whose binary runs in Linux, Windows and Mac OSX. It reads some text input files, but I realize that some special users can not handle this. I would then like to build some kind of graphical interface, where the user only finds buttons and scroll bars for selecting the input parameters, a big "run" button, and then it reads the output of the program and makes some figures.
I also need that everything gets finally packed in a single file, which uses only static libraries, so the user just needs to copy the file to his/her machine and run it.
I would like to know what is the best open source and multi-platform approach to do this. 10 years ago I played a bit with something similar on DEC machines, so I guess that nowadays the situation has probably improved a bit.
P.S. For designing the graphical interface, I am looking for a graphical approach, where you add buttons, scroll bars with the mouse
P.S. 2: the interface is really simple, just need less than 10 buttons, 5 text fields and 2 scrolla bars
Thanks
For advanced UIs, I would generally recommend writing a different UI for each platform (since each platform has its human inteface guidelines). However, is this going to be a simple UI, then one of the cross-platform UIs.
You also didn't mention what language you want to use.
Lastly your "1 exe file" is a bit of a myth - it applies only to Windows. On MacOSX, we use the magic app folders, so it doesn't matter how many files comprise your app, you still get drag-n-drop installs.
Look into GTK+ which originated on Linux, or wxWidgets.
Tcl/Tk is a perfect choice. No other language provides as good of a deployment solution. You can create a virtual filesystem that has your application along with icons, sound files, etc into a single file for each platform (called a 'starpack'). You can even include binary executables and libraries, though those have to be copied to the actual filesystem at runtime to be used.
You also have the option of a two-file deployment -- a platform-specific runtime called 'tclkit', and a platform-independent application file called a 'starkit'. The one starkit will work on all platforms without recompiling, rebundling, etc. It can even have platform-specific parts built-inside and chosen at runtime.
A professional Tcl/Tk developer could do a front end to a command line program in a day without a graphical GUI design tool, easily. If you're new to tcl it will obviously take longer, but that is true of any language. The point being, Tk is remarkably easy to use and doesn't require a graphical GUI designer.
For a cross platform UI, you can use GTK (if using C) or QT (if using C++).
If you can live with a rather huge application package to deliver be sure to look at https://electronjs.org/ You can keep your functionality in your commandline apps and build a modern look and feel UI using HTML5 CSS JS and before thinking "this is ridiculous" consider that Microsoft's Visual Studio code is built on this and compared to GTK / wxWidgets you can do wonders with this. It isn't even hard to do but you either love it or hate it. I'm still undecided...
I'd like to be able to switch the sound output source in Mac OS X without any GUI interaction.
There are tools to do control the sound output, such as SoundSource and an applescript to open the preferences dialog.
What I am looking for is something that switches the preference instantly, like SoundSource but it has to be scriptable. The goal is to switch between my digital and analog output with one keystroke. I have a helper application that will launch a program or applescript on one keypress. All I need now is the applescript or application that switches the sound source quickly without any user interaction.
I'm willing to write some Objective-C if that is what it takes, but I'm pretty much a newbie at Cocoa development.
Do you have a one-click solution or can point me to a good tutorial on controlling sound system preferences from a Cocoa App or command line?
EDIT: I created a command-line application to do exactly this. You may download it at http://code.google.com/p/switchaudio-osx/downloads. Source code is available on the project site as well.
I created a command-line application to do exactly this.
You may download it at http://code.google.com/p/switchaudio-osx/downloads. Source code is available on the project site as well.
UPDATE (Dec. 2014): the code is now hosted on github -- https://github.com/deweller/switchaudio-osx. And works just fine in Yosemite.
Don’t think of it in terms of preferences; there’s no centralized system preference framework for this sort of thing. I believe what you need to do is use Core Audio to set the kAudioHardwarePropertyDefaultOutputDevice and kAudioHardwarePropertyDefaultSystemOutputDevice properties of the AudioSystemObject (using AudioHardwareSetProperty()).