I have almost finished my program so I thought I would post what I have since I don't know when I'll get back to it.
As far as I can tell all that I have to do to make this work is to change the changeCommand method to work even if TrExec= ( the command used to preview a screensaver ) comes before Exec= as it is searching for Exec= which is in TrExec=. I would fix the problem now but it is late and bad things happen when I try to program tired. So here it is ( in java ) for anyone that is curious or would like to make any suggestions ( just please don't run it, I am not posting the .class file because this is unfinished and needs to run as root, so if it breaks it could be very bad and I take no responsability ) that being said, to run it run
Code:
sudo java ConfigSaver ~/.screensaver
Code:
import java.io.*;
public class ConfigSaver{
public static String pathToGnomeThemes = "/usr/share/gnome-screensaver/themes/";
public static void main(String[] args){
String xscreensaver = readFile(args[0]);
// finds the names of all the screensavers by looking at thier .desktop filenames
String[] screenSavers = getFileNames(pathToGnomeThemes);
// strips the .desktop from the filename ( leaving only the name of the screensaver )
for(int i=0; i< screenSavers.length; i++){
String temp = screenSavers[i];
temp = temp.substring(0, temp.length()-8);
screenSavers[i] = temp;
}
// edits the .desktop files to update the command
for(int i=0; i< screenSavers.length; i++){
String saverName = screenSavers[i];
String command = getCommand(xscreensaver, saverName);
if(command != null){
String saverConfig = readFile(pathToGnomeThemes + saverName + ".desktop");
String newSaverConfig = changeCommand(saverConfig,command);
writeFile(pathToGnomeThemes + saverName + ".desktop", newSaverConfig);
}
}
}
public static String readFile(String filePath){
try {
BufferedReader input = new BufferedReader(new InputStreamReader(new FileInputStream(filePath)));
String text=input.readLine();
String line = input.readLine();
while(line !=null){
text+= "\n";
text+=line;
line = input.readLine();
}
return text;
}
catch(IOException e) {
e.printStackTrace();
return null;
}
}
public static boolean writeFile(String filePath,String text){ // returns true if successfull, false if write failed.
try{
String toOut = text;
PrintStream output = new PrintStream(new FileOutputStream(filePath));
output.print(toOut);
output.close();
}
catch(IOException e){
e.printStackTrace();
return false;
}
return true;
}
public static String[] getFileNames(String directory){
return new File(directory + ".").list();
}
public static String getCommand( String xscreensaver, String saverName){
int startOfCommand = xscreensaver.indexOf(saverName);
if(startOfCommand == -1){
try{
return null;//return javax.swing.JOptionPane.showInputDialog(saverName + " doesn't seem to have a corrosponding xscreensaver, enter the command you would like gnome-screensaver to use then hit OK \n or hit cancel to not change the command");
}
catch(Exception e){ // I only expect an exception to be thrown if this is being run on the open source JVM that does not implement most of swing
System.out.println(saverName + " was not modified because there doesn't seem to be a corrosponding xscreensaver ( this is normal and nothing to worry about )");
return null;
}
}
String temp = xscreensaver.substring(startOfCommand);
int endOfCommand = temp.indexOf("\\n\\");
String command = temp.substring(0,endOfCommand);
// remove any hard returns from the command
while( command.indexOf("\n") != -1 ){
temp = command.substring(0,command.indexOf("\n"));
command = temp + command.substring(command.indexOf("\n")+1);
}
// remove any \s from the command
while( command.indexOf("\\") != -1 ){
temp = command.substring(0,command.indexOf("\\"));
command = temp + command.substring(command.indexOf("\\")+1);
}
// remove any tabs from the command
while( command.indexOf(" ") != -1 ){
temp = command.substring(0,command.indexOf(" "));
command = temp + command.substring(command.indexOf(" ")+1);
}
// remove any consecutive spaces (removes only one space so, for instance "-option 1" would become "-option 1" and "-option 1" would also become "-option 1"
while( command.indexOf(" ") != -1 ){
temp = command.substring(0,command.indexOf(" "));
command = temp + command.substring(command.indexOf(" ")+1);
}
return command;
}
public static String changeCommand(String saverConfig, String command){
if(saverConfig.indexOf("Exec=")< saverConfig.indexOf("TryExec")){
String configBeforeCommand = saverConfig.substring(0,saverConfig.indexOf("Exec=") -1);
String temp = saverConfig.substring(saverConfig.indexOf("Exec="));
String configAfterCommand = temp.substring(temp.indexOf("\n"));
saverConfig = configBeforeCommand + "Exec=" + command + configAfterCommand;
}
else{
int startIndexOfCommand = saverConfig.indexOf("TryExec=") + 8;
String temp = saverConfig.substring(startIndexOfCommand);
startIndexOfCommand += temp.indexOf("Exec="); //-1;
temp = saverConfig.substring(startIndexOfCommand);
String configBeforeCommand = saverConfig.substring(0,startIndexOfCommand);
String configAfterCommand = temp.substring(temp.indexOf("\n"));
saverConfig = configBeforeCommand + "Exec=" + command + configAfterCommand;
}
return saverConfig;
}
}
[edit] updated code to working version