liverare Posted October 25, 2017 Share Posted October 25, 2017 (edited) You can make your jar files copy themselves across to the OSBot script directory. This is mostly useful if you plan on giving someone your script in the form of a jar file, and you can try it out yourself with my Glassblowing script. Note: Nothing will happen if you try to run the jar file from within your script folder. The jar cannot delete itself. This is possible launching a batch process, but that's hacky and more effort than it's worth. When you export your project to a jar, make the code below your main class: Save as MoveToOSBotScriptFolder.java import java.awt.Desktop; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import javax.swing.JOptionPane; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; /** * Move Jar file from wherever it currently is to OSBot's script folder. * * @author LiveRare * */ public class MoveToOSBotScriptFolder { public static final String OSBOT_SCRIPT_DIR = System.getProperty("user.home") + File.separatorChar + "osbot" + File.separatorChar + "scripts" + File.separatorChar; public static final File OSBOT_SCRIPT_FILE = new File(OSBOT_SCRIPT_DIR); public static final String THIS_FILE_DIR = MoveToOSBotScriptFolder.class.getProtectionDomain() .getCodeSource().getLocation().getFile().replaceAll("%20", " "); public static final File THIS_JAR_FILE = new File(THIS_FILE_DIR); public static final File OSBOT_SCRIPT_JAR_FILE = new File(OSBOT_SCRIPT_FILE, THIS_JAR_FILE.getName()); public static void main(String[] args) { try { if (!isRunningJarAlreadyInScriptsFolder()) { setSystemLAF(); if (alreadyExists() ? requestPermissionToOverwriteFile() : requestPermissionToMoveFile()) { if (deleteJarFileInScriptFolder() && createJarFileInScriptFolder()) { copyFileAcross(); openOSBotScriptFolder(); } } } } catch (IOException e) { e.printStackTrace(); } } private static boolean isRunningJarAlreadyInScriptsFolder() { return THIS_JAR_FILE.equals(OSBOT_SCRIPT_JAR_FILE); } private static void setSystemLAF() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) { e.printStackTrace(); } } private static boolean requestPermissionToMoveFile() { return JOptionPane.showConfirmDialog(null, String.format("Do you want to move:\n\n%s\n\nto\n\n%s", THIS_JAR_FILE, OSBOT_SCRIPT_FILE), "Confirm", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION; } private static boolean requestPermissionToOverwriteFile() { return JOptionPane.showConfirmDialog(null, String.format("%s already exists.\nDo you want to replace it?", OSBOT_SCRIPT_JAR_FILE.getName()), "Confirm", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION; } private static boolean alreadyExists() { return OSBOT_SCRIPT_JAR_FILE.exists() && OSBOT_SCRIPT_JAR_FILE.isFile(); } private static void copyFileAcross() throws FileNotFoundException, IOException { byte[] buffer = new byte[1024]; int length; try ( FileInputStream in = new FileInputStream(THIS_JAR_FILE); FileOutputStream out = new FileOutputStream(OSBOT_SCRIPT_JAR_FILE); ) { while ((length = in.read(buffer)) > 0) { out.write(buffer, 0, length); } } } private static boolean deleteJarFileInScriptFolder() { return !OSBOT_SCRIPT_JAR_FILE.exists() || OSBOT_SCRIPT_JAR_FILE.delete(); } private static boolean createJarFileInScriptFolder() throws IOException { return OSBOT_SCRIPT_JAR_FILE.exists() || OSBOT_SCRIPT_JAR_FILE.createNewFile(); } private static void openOSBotScriptFolder() { try { if (Desktop.isDesktopSupported()) { Desktop.getDesktop().browse(OSBOT_SCRIPT_FILE.toURI()); } } catch (IOException e) { e.printStackTrace(); } } } Edited October 25, 2017 by liverare Something happened when editing thread last time. o_o 2 Quote Link to comment Share on other sites More sharing options...
TheWind Posted October 25, 2017 Share Posted October 25, 2017 (edited) I just save all my scripts to the script folder and it overwrites them through eclipse, but nonetheless a nice snippet. Edited October 25, 2017 by TheWind Quote Link to comment Share on other sites More sharing options...
liverare Posted October 25, 2017 Author Share Posted October 25, 2017 5 minutes ago, TheWind said: I just save all my scripts to the script folder and it overwrites them through eclipse, but nonetheless a nice snippet. I only compile a jar file when I intend to distribute a script. Otherwise, my Eclipse will compile my script and save the compiled files to OSBot's script folder too. Quote Link to comment Share on other sites More sharing options...
TheWind Posted October 25, 2017 Share Posted October 25, 2017 15 minutes ago, liverare said: I only compile a jar file when I intend to distribute a script. Otherwise, my Eclipse will compile my script and save the compiled files to OSBot's script folder too. How would you run a local script if you don't make a jar for it? 1 Quote Link to comment Share on other sites More sharing options...
liverare Posted October 25, 2017 Author Share Posted October 25, 2017 (edited) 1 hour ago, TheWind said: How would you run a local script if you don't make a jar for it? You don't need to. OSBot didn't always support jar files. Here: https://osbot.org/forum/topic/131666-setting-up-eclipse/ Edited October 25, 2017 by liverare Quote Link to comment Share on other sites More sharing options...