

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class ScreenBrightness extends Application {

	public void runapplication(double params, long interval) throws IOException {
		double brightnesslevel = params;
		
		FileWriter fw = new FileWriter(new File("/Library/StartupItems/LlamaApp/Brightness.scpt"));
		
		fw.write("\nChangeBrightness("+brightnesslevel+")");
		fw.write("\n on ChangeBrightness(BrightnessValue)");
		fw.write("\ntell application \"System Preferences\" ");
        fw.write("\nset current pane to pane \"com.apple.preference.displays\"");
        fw.write("\nend tell");
        fw.write("\ntell application \"System Events\" to tell process \"System Preferences\"");
        fw.write("\ntell radio button \"Display\" of tab group 1 of window 1 to if value is 0 then");
        fw.write("\n         repeat until value is 1");
        fw.write("\n             click ");
        fw.write("\n     end repeat ");
        fw.write("\n end if ");
        fw.write("\ntell slider 1 of group 2 of tab group 1 of window 1 to set value to BrightnessValue");
        fw.write("\nend tell");
        fw.write("\ntell application \"System Preferences\" to quit");
        fw.write("\nend ChangeBrightness");
		
        fw.close();
        
        Runtime rtime = Runtime.getRuntime();
        String[] commands = {"osascript","/Library/StartupItems/LlamaApp/Brightness.scpt"};
        rtime.exec(commands);
		
	}

}
