通过Java使用Quicktime录制

通过Java使用Quicktime录制,java,applescript,quicktime,Java,Applescript,Quicktime,我想使用Java使用Quicktime进行录制,但在尝试输入文件夹和文件时出现了一些问题。我认为这可能是一个时间问题,所以推迟了,但它仍然发生。开始录制的代码是: public static void startQTrecord(){ //Activate QT try{ String[] go = { "osascript", "-e", "tell application \"QuickTime Player\" to act

我想使用Java使用Quicktime进行录制,但在尝试输入文件夹和文件时出现了一些问题。我认为这可能是一个时间问题,所以推迟了,但它仍然发生。开始录制的代码是:

public static void startQTrecord(){     
        //Activate QT
        try{
            String[] go = { "osascript", "-e", "tell application \"QuickTime Player\" to activate" };
            Process process = Runtime.getRuntime().exec(go);
            process.waitFor();

        } catch (IOException ex) {
            System.out.println(ex.toString());
        } catch (InterruptedException ex) {
            System.out.println(ex.toString());
        }    
        //Start new recording
        try{
            String[] go = { "osascript", "-e", "tell application \"QuickTime Player\" to start new movie recording" };
            Process process = Runtime.getRuntime().exec(go);
            process.waitFor();    
        } catch (IOException ex) {
            System.out.println(ex.toString());
        } catch (InterruptedException ex) {
            System.out.println(ex.toString());
        }
要停止并保存的代码是:

public static void closeQT(){
        //Go to QT
        try {
            String[] go = {"osascript", "-e", "tell application \"QuickTime Player\" to activate"};
            Process process = Runtime.getRuntime().exec(go);
            process.waitFor();
        } catch (IOException ex) {
            System.out.println(ex.toString());
        } catch (InterruptedException ex) {
            System.out.println(ex.toString());
        }
        //Stop the recording
        try {
            String[] go = {"osascript", "-e", "tell application \"QuickTime Player\" to stop document \"movie recording\""};
            Process process = Runtime.getRuntime().exec(go);
            process.waitFor();
        } catch (IOException ex) {
            System.out.println(ex.toString());
        } catch (InterruptedException ex) {
            System.out.println(ex.toString());
        }
        //Save
        try {
            sleep(100);
            String[] go = {"osascript", "-e", "tell application \"System Events\" to keystroke \"s\" using {command down}", "-e", "delay 1"};         
            Process process = Runtime.getRuntime().exec(go);
        process.waitFor();
        } catch (IOException ex) {
            System.out.println(ex.toString());
        } catch (InterruptedException ex) {
            System.out.println(ex.toString());
        }
        //Enter the save directory
        try {
            sleep(100);
            String[] go = {"osascript", "-e", "tell application \"System Events\" to keystroke \"G\" using {command down, shift down}", "-e", "delay 1"};
            Process process = Runtime.getRuntime().exec(go);
            process.waitFor();
        } catch (IOException ex) {
            System.out.println(ex.toString());
        } catch (InterruptedException ex) {
                System.out.println(ex.toString());
        }
        try {
            sleep(100);
            String gofolder = "tell application \"System Events\" to keystroke \"" + folder + "\"";
            String[] go = {"osascript", "-e", gofolder, "-e", "delay 1", "-e", "tell application \"System Events\" to keystroke return", "-e", "delay 1"};
            Process process = Runtime.getRuntime().exec(go);
            process.waitFor();
        } catch (IOException ex) {
            System.out.println(ex.toString());
        } catch (InterruptedException ex) {
                System.out.println(ex.toString());
        }
        try {
            sleep(100);
            String[] go = {"osascript", "-e", "tell application \"System Events\" to keystroke\""+file+"\"", "-e", "tell application \"System Events\" to keystroke return", "-e", "delay 1"};         
            Process process = Runtime.getRuntime().exec(go);
            process.waitFor();
        } catch (IOException ex) {
            System.out.println(ex.toString());
        } catch (InterruptedException ex) {
            System.out.println(ex.toString());
        }
        /////////////////////////////////////////////
        //Wait for save then quit
        boolean keepWaiting = true;
        while(keepWaiting){
            try{
                Runtime rt = Runtime.getRuntime();
                String[] commands = {"osascript", "-e", "delay 5", "-e", "tell application \"QuickTime Player\" to quit"};
                Process proc = rt.exec(commands);
                proc.waitFor();
                BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
                String s = null;
                // read any errors from the attempted command
                if(stdError.readLine() == null){keepWaiting = false;}
            } catch (IOException ex) {
                System.out.println(ex.toString());
            } catch (InterruptedException ex) {
                System.out.println(ex.toString());
            }
        }

    }
有时它可以正常工作,但有时它会进入文件夹窗口,无法粘贴到正确的文本中

有没有关于为什么这不起作用或者有没有更好的方法的想法


谢谢

最好使用appleScript的保存命令,而不是使用击键命令

public static void closeQT(){
        //Pause the recording, save and quit
        try {
            String[] go = {"osascript", "-e", "set tFile to \""+fullPosixPathOfFile+"\" as posix file", "-e", "do shell script \"touch \" & quoted form of posix path of tFile", "-e", "tell application \"QuickTime Player\"", "-e", "pause document 1", "-e", "save document 1 in tFile", "-e", "stop document 1", "-e", "close document 1 saving no", "-e", "quit", "-e", "end tell"};
            Process process = Runtime.getRuntime().exec(go);
            process.waitFor();
        } catch (IOException ex) {
            System.out.println(ex.toString());
        } catch (InterruptedException ex) {
            System.out.println(ex.toString());
        }
    }
将要保存电影的文件路径放入
fullposixaphffile
变量中


下面是一个实用的AppleScript示例

set tFile to (path to desktop folder as string) & "MyMovie.mov" -- save the movie to the desktop

do shell script "touch " & quoted form of POSIX path of tFile  --  "QuickTime Player" need an existing file (otherwise permissions issue), so the touch command create an empty file 
tell application "QuickTime Player"
    launch
    set newMovieRec to new movie recording
    tell newMovieRec
        start
        delay 10 -- duration in seconds of this new movie
        pause
        save in (file tFile) -- The term "file" is necessary when the type of the path is HFS (path with colon character --> "diskName:folder1:subFolder2:myMovie.mov")
        --save in tFile -- use this line when the tFile variable contains a posix file (a posix path converted to a posix file)
        stop
        close saving no
    end tell
    quit
end tell

要检查Quicktime的版本,请执行以下操作:

tell application "QuickTime Player"
    set v to version
    considering numeric strings
        set newVersion to v > "7.6.6" -- change it to the old version which not work
    end considering
    if newVersion then
        -- code to save
    else
        -- code to save as
    end if
end tell

谢谢当我尝试此操作时,我收到一条错误消息“文档无法保存。您没有权限。”尽管对我要保存到的所有文件夹具有读/写访问权限。有什么想法吗?因为
tFile
包含一个字符串(HFS路径,带有冒号字符的路径),脚本需要
文件
术语-->
保存在(文件tFile)
。我在回答中改变了它。如果使用posix路径,请将其转换为posix文件,如以下示例-->
将tFile设置为“/Users/myUserName/Desktop/MyMovie.mov”作为posix文件
我仍然收到相同的消息:(只需稍加修改即可-将“save”行更改为“save in”(tFile as posix文件)。谢谢!!这适用于新版本的QuickTime,但旧版本没有“保存”命令-它们使用“另存为”。如何修改applescript以适应这两种情况?