Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

silent2532

macrumors newbie
Original poster
Mar 19, 2010
5
0
Is it possible?

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

void typethis(char *text) {
cout << text;
}
Is it possible that in applescript i do this?
tell application "MyApp"
typethis "Will it work?"
end tell

Is it possible? If so can you show me how? or give me links for tutorial?
 

fredthefool

macrumors newbie
Jun 4, 2008
20
0
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
 

silent2532

macrumors newbie
Original poster
Mar 19, 2010
5
0
I tried your code but it doesn't show "Will it work?"
It just shows what I cout in int main()

#include <iostream>

void typethis(char *text) {
std::cout << text;
}

int main (int argc, char * const argv[]) {
std::cout << "Hello World!";
}
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
tell application myApp
typethis "Will it Work?"
end tell
Then it would show in the commandline "Will it Work?"

Or when my App is running in Terminal and I say
tell application "Terminal"
typethis "Will it work?"
end tell
 

Mac_Max

macrumors 6502
Mar 8, 2004
404
1
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;
	
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.