View Full Version : Xcode C++ help! Opening external programs using C++
unknown.exe
Nov 30, 2007, 10:13 PM
Sup, I'm writing a C++ program that has all of these built in programs(just simple conversion or game progs), and I'm putting one in that opens up external programs like Safari or Word. I know how to do it in windows (#include <stdlib.h>... system("C:\\folder\\program.exe"). But SYSTEM doesn't work in mac or xcode... please can someone tell me how to do this?:apple:
Cromulent
Nov 30, 2007, 10:50 PM
Sup, I'm writing a C++ program that has all of these built in programs(just simple conversion or game progs), and I'm putting one in that opens up external programs like Safari or Word. I know how to do it in windows (#include <stdlib.h>... system("C:\\folder\\program.exe"). But SYSTEM doesn't work in mac or xcode... please can someone tell me how to do this?:apple:
system("/path/to/executable");
unknown.exe
Nov 30, 2007, 11:37 PM
system("/path/to/executable");
Excuse me, but what do you mean? Like system("Macintosh HD/Applications/Word")................or are there technical terms I need to use like C:\ drive in windows? Like a technical name for the HD?
unknown.exe
Nov 30, 2007, 11:46 PM
system("Macintosh HD/Applications/VLC");
this is what I have in my program, but it says
sh: line 1: Macintosh:command not found
i tried using my home directory...
system ("markspangler/Applications/VLC");
but it tells me "no such directory"....
any more help?
(do i need a specific #include file?)
kainjow
Dec 1, 2007, 12:21 AM
If you want to open Safari, use this:
system("open /Applications/Safari.app");
The "open" command works as if you double-clicked the file.
If you were to use this instead:
system("/Applications/Safari.app/Contents/MacOS/Safari");
You would get a new instance of Safari. Much like opening multiple instances of IE in Windows. But this isn't Mac behavior so use "open" instead.
Cromulent
Dec 1, 2007, 12:26 AM
Excuse me, but what do you mean? Like system("Macintosh HD/Applications/Word")................or are there technical terms I need to use like C:\ drive in windows? Like a technical name for the HD?
Unix based operating systems have a different path structure to Windows.
So C:\Stuff\Random\test.exe is exactly the same as /Stuff/Random/test.app in Mac OS X.
Edit : To clarify. The root of the hard drive in Windows is C: the root of the hard drive in OS X is /. All full paths in OS X start with / as all full paths in Windows start with C:\.
tiikeli
Dec 1, 2007, 04:10 AM
Invokes the command processor to execute a command. Once the command execution has terminated, the processor gives the control back to the program, returning an int value, whose interpretation is system-dependent.
Is this _really_ what you want to do? The normal way would be to fork the process and then call one of the exec functions, eg. execve.
gnasher729
Dec 1, 2007, 04:33 AM
system("Macintosh HD/Applications/VLC");
this is what I have in my program, but it says
sh: line 1: Macintosh:command not found
i tried using my home directory...
system ("markspangler/Applications/VLC");
but it tells me "no such directory"....
any more help?
(do i need a specific #include file?)
This doesn't work, because what the system sees is the line
Macintosh HD/Applications/VLC
which is interpreted as the command "Macintosh" with a parameter "HD/Application/VLC". Using the "system" command is difficult and can be extremely dangerous. The same code would also not work on any computer where the hard disk isn't called "Macintosh HD".
Typing "LaunchApplication" into spotlight should show you a document "Launch Services Reference", which contains the recommended way to launch applications. For example, the call "LSOpenItemsWithRole" will "open" any number of items, including applications, as if you had double-clicked their icon, and it will do the Right Things.
unknown.exe
Dec 1, 2007, 11:10 AM
Thank you all for the suggestions, the open function worked well... thanks:apple:
vBulletin® v3.8.6, Copyright ©2000-2012, Jelsoft Enterprises Ltd.