Controlling a C++ app using applescript?

Discussion in 'Mac Programming' started by silent2532, Mar 19, 2010.

  1. silent2532 macrumors newbie

    Joined:
    Mar 19, 2010
    #1
    Is it possible?

    Like when I make a program in C++ like this

    Is it possible that in applescript i do this?
    Is it possible? If so can you show me how? or give me links for tutorial?
     
  2. fredthefool macrumors newbie

    Joined:
    Jun 4, 2008
    #2
    To made an application scriptable, as it's needed for controlling it via the "tell" statements, is not trivial. But there is an easy way to execute binaries via the "do shell script" command. If your binary is able to handle parameters, given on the command line, you can call it and submit the values from inside your AppleScript code:


    Code:
    set myCommand to "/PATH/TO/YOUR/BINARY " & "Will it work?"
    do shell script (myCommand)
    
    This assumes your code is compiled to an executable, which will be found at the proper path and will be able to handle the submitted value(s): Three strings! -- for the executing shell will separate them because of the whitespaces.

    In my opinion, it's best practice to separate the command string's definition from the executing "do shell script" line, for in this case you're able to alter the string, e.g. exchanging the parameters in dependence of interactive user input.

    I don't know how familiar you are with coding in C++ and handling parameters, so ask for help if needed.



    Ah -- and i forgot: The Output of a called shell script will "vanish" in the deeps of the system's processes, because there is no shell window, where you can see the output. Try to catch the output instead of assigning it to a variable, you will be able to display:
    Code:
    set myValue to do shell script (myCommand)
    display dialog myValue
    
     
  3. silent2532 thread starter macrumors newbie

    Joined:
    Mar 19, 2010
    #3
    I tried your code but it doesn't show "Will it work?"
    It just shows what I cout in int main()

    When i used your applescript is only showed "Hello World!"
    And I mean a running application
    for example myApp is a stay open like in applescript and I want to call it anytime I want to show it like when i type
    Then it would show in the commandline "Will it Work?"

    Or when my App is running in Terminal and I say
     
  4. Mac_Max macrumors 6502

    Joined:
    Mar 8, 2004
    #4
    Your code won't work because you're not actually accepting any arguments or processing them. You also need to include the string library.

    The following is what I would do:

    Code:
    
    #include <iostream>
    #include <string>
    using namespace std;
    
    string input = "Please Enter a String"; //default error message
    
    void typethis(string text) {
    	cout << text << endl;
    }
    
    int main (int argc, char *argv[]) { 
    	
        if (argc != 2){ 
    		
    	/*argc is the number of indexes in argv[] entered i.e. 
    	 in the terminal the command "kill 556" would mean argc == 2.
    	 argv[0] = the program itself, kill and argv[1] = the int 556
    	*/	
    	 
    		typethis(input); //if we have too many inputs we'll error out	
    	
    	} else {
    		
    		try {
    			typethis(argv[1]); 
                    /*argv[1] will be our string. We place it into a try-catch
                   to make sure we don't have issues with something unexpected
                   that cout can't handle*/
    		}
    		catch (exception ex){ //standard exception handling
    			
    			typethis("Error: Data Entered was not a valid string. " 
    					 + input);
                   //inform the user of an error
    			
    		}	
    		
    	}
    	
    
    	return 0;
    	
    }
    
    
     

Share This Page