Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

I have written a command line c program. How do I get it to run when I double click a file

I am using iMac with Yosemite.


I have written a command line program in C++ which plays special midi files. I want to open a terminal, load the program and play the special midi file when the special file is double clicked. The files are distinguished by extensions like .m50 or .mes which are not normally used by standard programs.


When I double click a file in finder which it does not recognise it asks me to select a program which can process the file. When I try to tell it to use my program it will not recognise it and will not accept my instructions.


The program is fully functional and plays the special files from the command line.


I have been using this technique in Windows for years but it appears to not work properly on an iMac.


Please tell me how to make it work.

iMac, OS X Yosemite (10.10.4)

Posted on Jul 5, 2015 10:26 AM

Reply
Question marked as Best reply

Posted on Jul 6, 2015 11:57 AM

Hello csw900,

I am not sure for the exact terminal command to run c++, but I do know, for C.

You can try running:

cc (File Directory Location)


The output executable would be typically called a.out


Tell me if it works!

10 replies

Jul 6, 2015 12:29 PM in response to csw900

Write a bash script. Type in the same syntax that you used to run your C++ program on the command line and play your audio file. Make it executable:


chmod +x mytune.sh


Preface your program name as ./mytune.sh so your program can be found by the shell. You may also need to precede your music file with its local path too.


If your Bash script is named mytune.sh, then rename it to mytune.command. When you click on this file in the FInder, it will launch, run your C++ program, and play your music file. In order for this to work, the first line of the script must be #!/bin/bash. When it is done, the Terminal window closes.

Jul 7, 2015 12:05 AM in response to XCH1

Thanks XCH1, I am afraid you have misunderstood my problem! My program is already compiled and located

as an executable at /usr/local/bin. You can download my program from http://midimusic.github.io/tech

it comes complete with an install script and a load of .dylibs which are all installed by the install script.

You can download some of the music files (e-rolls) from either the Wurlitzer archive or the Estey archive

on the same site.


When I double click on a music file the finder asks how to use the file but only allows me to select apps.

My program is not an app. If I select display the file Info. it displays and I can opt to open it with programs

other than apps. However it does not show files in /usr/local/bin or allow me to otherwise find my program.


It does show files in /opt but this is not on the PATH so if I locate it there my program will not execute

from the command line.


I hope this clarifies the situation, many thanks for responding.

Jul 7, 2015 12:29 AM in response to VikingOSX

Thanks for trying VikingOSX. Your solution may very well work if there were only one music file and I was the

only person who needed to play it. I have given a lot more information which yo can see in my response to XCH1.

If you download my program install it (have a good look at my install script before running it) and download a few e-rolls

you will quickly find that you cannot play the e-rolls by double clicking them but they do play from the command line.


I want the e-rolls to play by double clicking them as the do in Windows (or by some other convenient method which

does not involve users in using the command line directly)


I have had a look at an "appify" script which may turn my program into an .app . I have not yet tried it but have doubts.

Could this be a solution. I have also successfully compiled my program in Xcode but have not been able to get a running

program out of it. The documentation leaves a lot to be desired in this area.


Please have a few more thoughts, thanks for responding.

Jul 7, 2015 12:41 PM in response to XCH1

This is potentially a good idea but I have not so far been able to make an apple script open my program.


This is my code using the Droplet template:

================================================

(* TO FILTER FOR SPECIFIC FILES, ENTER THE APPROPRIATE DATA IN THE FOLLOWING LISTS: *)

property type_list : {"MIDI"} -- e.g.: {"PICT", "JPEG", "TIFF", "GIFf"}

property extension_list : {"m25"} -- e.g.: {"txt", "text", "jpg", "jpeg"}, NOT: {".txt", ".text", ".jpg", ".jpeg"}

property typeIDs_list : {"public.midi"} -- e.g.: {"public.jpeg", "public.tiff", "public.png"}

...droplet code unchanged.....


...finally


-- this sub-routine processes files

on process_file(this_item)

run "/usr/local/bin/eplay125mac this_item"

-- NOTE that during execution, the variable this_item contains a file reference in alias format to the item passed this sub-routine

-- FILE PROCESSING STATEMENTS GO HERE


end process_file

================================================

This code compiles and makes eplayDroplet.app

I have told my .m25 files to run with eplayDroplet.app and this has been accepted by the system.

eplayDroplet.app does not complain when I drop an .m25 file on to it --- but it does not run my program!!


Any suggestions? There is no example in the Apple Script handbook to indicate it can run

anything but apps and scripts (my program is neither).


Perhaps I need to tell it to open the Terminal app but where do I go from there? Again there is no example.

Apple script is a foreign language to me.


HELP!!

Jul 8, 2015 5:30 AM in response to XCH1

This is the code I have ended up with:


on process_file(this_item)

activate application "Terminal"

tell application "Terminal"

do script "eplay125mac " & POSIX path of this_item

end tell

-- tell application "Terminal" this quits before playing even starts!!

-- quit

-- end tell

end process_file


Your idea has solved my problem and I can now play my files either by double clicking them or by

dropping them onto eplayDroplet.app. In both cases the playDroplet.app opens a dialog which I have

to click on before it proceeds - There is no code in the app to produce this dialog so how do I stop

it from opening this initial dialog?


Also I would like the Terminal to quit AFTER playing has completed. A simple quit instruction as

I have shown above simply quits before playing even starts which is worse than useless. How do

I tell that my program has closed? This would be the time to quit Terminal.


Many thanks for your time.

Jul 9, 2015 3:02 AM in response to XCH1

I got rid of the opening dialog by writing my own script from scratch and also added my own icon instead

of using apple's icon.


A simple delay is no good because the time it takes to play music varies so much!!


However your suggestion of looking at "busy" gave me the clue and my final code is:

on process_m25(this_item)

activate application "Terminal"

tell application "Terminal"

set w to do script "eplay125mac " & POSIX path of this_item

repeat

delay 1 -- I understand this to be 1 second but it works and I have not checked

if not busy of w then exit repeat

end repeat

quit

end tell

end process_m25


I think that now covers everything and anyone else with the same problem would do well

to consider using code similar to mine above.

Thanks for your helpful ideas XCH1

I have written a command line c program. How do I get it to run when I double click a file

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.