How do you run a .command file with cocoa programming language? - cocoa

I'm brand new to Cocoa programming and was wondering how to run a .command file through my program. I've seen use of NSTask but being new I don't quite understand all of the lines and can't seem to get it to work. I don't need to monitor the terminal results or retrieve the data that terminal puts out, I just need to run it.

The most simple way is:
system("/path/to/my/script.command");
This is not Cocoa, this is Posix.

Related

How Ruby Is Run

I come from an object oriented programming background, and have picked up Ruby as a hobby. It looks like a great language. My questions are:
Can you use any ol' test editor, write your ruby file, save it with a .rb extension and open it in the terminal?
Is this the most common method of using Ruby (Or other script languages), rather than with an IDE?
How do I close external programs on my Mac with ruby? What I will be doing with Ruby has a lot to do with opening files and commanding them. I was using AppleScript but want to convert. I've tried:
system open "John/Applications/TextEdit.app"
And it didn't close.
I'm on Windows but I'll try to answer your questions as good as possible, most of the stuff is OS independent.
For questions 1 and 2: You can use one of the following ways to edit and run Ruby scripts.
IRB (Interactive RuBy shell included with Ruby)
websites like http://tryruby.org/levels/1/challenges/0
An IDE such as:
RedMine
KomodoEdit
Eclipse with plugin (not that easy to configure)
not really necessary because Ruby code is short and easy to remember, kind of pseudocode, the advantage is in the beginning when you don't know the commands and structures and for debugging
voordeel = debuggen is makkelijker
Editor with a Run-optie like Textpad, Notepad++, Sublime Text..
The last one is my favorite, there are packages for Ruby but Ruby support is in the basic installation, you can edit your code with syntax-coloring and suggestions, run your code and the result is captured in a separate tab. It is the most widely used way of Ruby coding also.
In Windows I use the following way to run external programs and capture the result. I believe it to be working on a Mac also. The external shell and program is closed after the last end.
answer = ""
command = %Q{java -jar test.jar #{$parameter1} #{$parameter2}"}
IO.popen(command+" 2>&1") do |pipe|
pipe.sync = true
while str = pipe.gets
answer << str
end
end
#the answer variable holds all the output lines

Run bash files in Cocoa Applescript (ApplScriptObjC) (Xcode)

I'm trying to make a program consisting of an AppleScript interfacing with bash files (so that I get the fancy Cocoa GUI). I assume this is a simple task, because one of the file types in the new file-menu (cmd+n) is shell scripts (.sh).
I've done a few half-assed tries to get it working (do shell script "sh file.sh"), but I can't figure out how to run those files.
Are there any gurus out there who can help me? :3
If anything is unclear, feel free to ask questions.
If you just want to launch shell scripts from the Cocoa gui, you don't need the AppleScriptObjC bridge, make a Cocoa program in Objective-C and use the NSTask class: https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSTask_Class/Reference/Reference.html.

MacRuby: How to write an app/script that generates native Mac apps?

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.

How can I make a GUI frontend to a command line tool in OSX?

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.

is there a scripting solution for determining the default application path for a file on the Mac?

For a given extension, for example ".psd", I'd like to be able to determine the default application path for opening this file, for example "/Applications/Adobe Photoshop CS4.app".
I've looked into the Launch Services API, and there are clearly programmatic ways to get this information. Unfortunately for my particular scenario, only a scripting solution (Applescript or shell script) will do.
I've also looked at "lsregister -dump". It seems to be unwise to rely on parsing this information, since there are no guarantees as to the stability of the output format.
I've been solving this problem in the past with Creator Codes, but since Apple seems to be phasing them out since Snow Leopard I'm trying to eliminate dependence on Creator Codes.
thanks
Launch Services is the one and only place to get that information. You can write a scripting addition that will expose its functionality to AppleScript, but then you have to install that on whatever machine you plan to run on.
System Events does give you this in Leopard
alt text http://img.skitch.com/20091222-eessetxeqbai2mnwduygtm1cd5.png

Resources