Xcode C++ help! Opening external programs using C++

Discussion in 'Mac Programming' started by unknown.exe, Nov 30, 2007.

  1. unknown.exe macrumors member

    unknown.exe

    Joined:
    Sep 22, 2007
    Location:
    Somewhere on Earth
    #1
    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:
     
  2. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #2
    system("/path/to/executable");
     
  3. unknown.exe thread starter macrumors member

    unknown.exe

    Joined:
    Sep 22, 2007
    Location:
    Somewhere on Earth
    #3
    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?
     
  4. unknown.exe thread starter macrumors member

    unknown.exe

    Joined:
    Sep 22, 2007
    Location:
    Somewhere on Earth
    #4
    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?)
     
  5. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #5
    If you want to open Safari, use this:

    Code:
    system("open /Applications/Safari.app");
    The "open" command works as if you double-clicked the file.

    If you were to use this instead:

    Code:
    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.
     
  6. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #6
    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:\.
     
  7. tiikeli Guest

    Joined:
    Dec 1, 2007
    #7
    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.
     
  8. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #8
    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.
     
  9. unknown.exe thread starter macrumors member

    unknown.exe

    Joined:
    Sep 22, 2007
    Location:
    Somewhere on Earth
    #9
    Thank you all for the suggestions, the open function worked well... thanks:apple:
     

Share This Page