I'm working on a Java GUI and I'm trying to make a button that can start a FaceTime call with a given phone number. Here is an oversimplified version of the java code.
String cellNum = "18001234567";
try {
Runtime.getRuntime().exec("open /Users/faris/Desktop/call.app --args " + cellNum);
} catch (IOException e) {
e.printStackTrace();
}
After researching how do to this, I copied part of an AppleScript app I found online that I named call.app and modified it so it takes in an input argument phone number rather than manually entering it into the script. I've run the program with an actual phone number entered instead of the input variable and it works fine so I know that the problem lies with passing the argument.
call.app
on run args
set input to first item of args
open location "tel://" & input & "?audio=yes"
delay 1
tell application "System Events"
key code 36
end tell
end run
This is the error I get every time from the AppleScript.
Can’t get item 1. (-1728)
I've never used AppleScript before so I'm completely lost currently. Haven't found anything similar anywhere on SO. Any advice would be appreciated very much.
Cause:
Error -1728 in AppleScript is "Can't get «script»", indicating that the first item of args is a script object reference. This means that the command-line argument is not getting passed to your AppleScript's run handler.
Solution:
Instead of saving your AppleScript as an applet, save it (export it) as either a .scpt or an .applescript file. Then substitute your exec(...) Java command for this:
exec("osascript /path/to/applescript " + cellNum);
Related
I'm writing a plugin in Intellij Idea which executes commands on thirdParty product side and prints the output of the commands afterwards. Everything should be done in self-written ToolWindow of Intellij Idea. The idea is to create an instance of ConsoleView with ProcessHandler.
For example:
I input "some command" in consoleView
Then I'm pressing "Enter" and command is sent to external API which executes this command
API returns the result
I take this result and print it to consoleView.
So the major question is: I already have an instance of consoleView (TerminalExecutionConsole) and I am able to print output of the commands into my consoleView. (Currently I'm using simple textField to write the command and button to send this command to external API). I would like to type commands directly in TerminalConsoleView and execute them by pressing "Enter". Is there any way to type commands into user-defined consoleView directly like it was done in Intellij Idea "Terminal" toolWindow?
I already tried GeneralCommandLine but it doesn't do what I needed.
Could you please advise any tips or share any example how to do this?
Explanation of the screenshot_1:
I typed "TIME" command into the text field
Click "Send" button
API processed the command and returned the result of execution.
I captured the output and print it to consoleView using notifyTextAvailable(message) function of processHandler object
screenshot_1
The following code :
fun main(args: Array<String>) {
print("Write anything here: ")
val enteredString = readLine()
println("You have entered this: $enteredString")
}
gives the following error in KotlinPlayground :
Write anything here: You have entered this: null
Here, the user doesn't get an opportunity to enter input. After the initial print statement gets executed, the compiler is not waiting for the user to give input and skips to the next print statement. Why is it happening? I have tried the same in several other online Kotlin compilers but I am getting the same error.
There's no error. readLine just returns null (because Kotlin Playground doesn't have a console to read from), and it's printed as expected.
E.g. on https://ideone.com/ you can say what to use for input and it'll print that line (though its Kotlin version is pretty old).
Because Kotlin playground have no user input console and this is not an error. For user input you can either use https://ideone.com/ or
https://www.jdoodle.com/compile-kotlin-online/
I will recommend you to use the second on which is jdoodle. It is pretty much faster to run and read user input and almost use the latest version of Kotlin and JRE.
And if you like to play (run) with command line argument then it will good for you with jdoodle.
This can be done in Kotlin Playground by using JS as the destination platform - click on the JVM dropdown (top left, to the right of the kotlin version) and change to JS.
Now you can call JavaScript - only using constant strings, e.g.
fun main() {
val name = js("prompt('Please enter your name', 'Bob')")
println("Hello $name")
}
For another example, using a common function, see https://pl.kotl.in/MkhfYNS47
Note: if you try this:
fun promptName(default: String = "") {
return js("prompt('Please enter your name', '$default')")
}
You will get a compilation error - Argument must be string constant.
Note that JS IR as an option ran much slower and had an issue with default type needing to be stated for the linked code
so I'm new to using IntelliJ and I've tried googling but to no avail.
I'm creating a simple java program that basically prints hello and gets the user input (name) and prints it... Just to get the ball rolling. Normal Hello World prints fine..
But as soon as I add any [args] in it just crashes? Is there a way I can type the input in?
public class Main {
public static void main(String[] args) {
System.out.println("Hello, " + args[0] + "!");
}
}
You need to provide at least 1 argument if you access args[0] otherwise you get ArrayIndexOutOfBoundsException.
Why ? because the args[] is empty without any arguments passed so accessing the first one will throw the exception
How do you input commandline argument in IntelliJ IDEA?
There's an "edit configurations" item on the Run menu. In that panel, you can create a configuration and then you can choose the Class containing main().
add VM parameters and command-line args, specify the working directory and any environment variables.
you are done.
Sorry guys figured it out:
Go to Run
Edit Configurations > on the left side make sure you're in your Main class or whatever class you're using
Enter what you want in the program arguments. i.e. "James"
I have this chess engine Rybka.exe, that i have to execute in java
Here is an example how you run Rybka:
Once you clicked on it, console opens and waits for input.
So then you enter "uci" and you press enter and you wait for it to load (approx. 1 sec) and then you have to enter a few more lines as options and stuff.
The problem is that I don't know how to pass those commands from java to Rybka. The fact is that those commands need to be entered one at a time, because you have to wait for some to execute.
This is how I tried to open it.
Code:
Process p1 = Runtime.getRuntime().exec("Rybka.exe");
This works, because you can see that Rybka.exe is active in task manager, but I don't know how to pass commands to it.
a) how to bind a windows console application with java application?
link provided by the courtesy of Google search query:
https://www.google.pl/search?q=java+binding+console+to+an+app&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a
b) in short:
InputStream is = p1.getInputStream();
OutputStream os = p1.getOutputStream();
(supplied by the obvious http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Process.html)
Have you tried passing parameters/commands as below?
Runtime.getRuntime().exec("Rybka.exe parameter1,parameter2");
I currently convert an application to use CreateProcessW() instead of Runtime.exec() as I need the information it provides. However any call to CreateProcessW() fails with the error code 5 (ACCESS DENIED). I have been unable to find out why this happens as Runtime.exec() runs fine in the same case.
My error could be in one of the following code snippets, the method call and the jna interface.
public ProcessInfo createProcess(String dir, String name){
ProcessInfo pi = new ProcessInfo();
StartupInfo start = new StartupInfo();
mem.CreateProcessW(new WString(name),
null,
null,
null,
false,
0,
null,
new WString(dir),
start.getPointer(),
pi.getPointer());
return pi;
}
My definition of CreateProcessW
boolean CreateProcessW(WString apname,
char[] comline,
Pointer p,
Pointer p2,
boolean inheritHandles,
int createFlags,
String environment,
WString directory,
Pointer startinf,
Pointer processInfo);
Additional Info:
Runtime.exec() succeeds with the given Strings
The size of StartupInfo is set
Testenvironment used: WinXP SP3 and Netbeans 6.9.1
Example parameters used:
Name: moviemk.exe
Dir: C:\Programme\Movie Maker\
Also tested with different paths, so not a whitespace problem
Thanks
Update:
As it turns out the error was caused by my calling code switching around working dir and exe path after I checked them. Because of the resulting access denied I actually thought that it at least found the exe. I will add an IllegalArgumentException to take care of that problem.
Since I had the additional error with the exe being relative to the working dir I will accept that answer. Thanks to all for helping.
CreateProcessW's first parameter has to be either a full path or a path relative to the current directory. It can't be a path relative to the working directory parameter, which seems like what you're expecting it to do.
Try passing C:\Programme\Movie Maker\moviemk.exe as the name parameter
The first parameter lpApplicationName of the CreateProcess function will be used as NULL typically and the second parameter lpCommandLine should contain the command line starting with the program name which you want to start.
Just fry to switch the first and the second parameters which you use currently by the CreateProcessW call.
What is the full path you are entering? Runtime.exec might be quoting the argument internally, and you could be running into this situation:
http://support.microsoft.com/kb/179147
Maybe there is a prefix to the path that exists and is causing it to try to execute a folder or other file?
Try putting quotes around the entire path and see if that helps.