shortcut.java /* * Copyright (c) 2001-2005 Servertec. All Rights Reserved. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * THIS NOTICE MUST NOT BE ALTERED NOR REMOVED. * * CopyrightVersion 1.0 */ import stec.sfc.Win32.Shortcut; import java.io.File; import stec.sfc.Win32.Win32Exception; public class shortcut { public static void main(String[] args) throws Exception { if(args == null) { syntax("shortcut was not specified."); return; } boolean overwrite = false; String targetFilename = null; String arguments = null; String shortcutFilename = null; String workDirectory = null; String iconPath = null; int iconIndex = 0; int showWindow = Shortcut.NORMAL; boolean displayOnly = false; int index = 0; while(index < args.length) { if(args[index].charAt(0) != '-') { break; } if(args[index].equalsIgnoreCase("-overwrite")) { overwrite = true; } else if(args[index].equalsIgnoreCase("-help") || args[index].equalsIgnoreCase("-h") || args[index].equalsIgnoreCase("-?")) { syntax(); return; } else if(args[index].equalsIgnoreCase("-display")) { displayOnly = true; } else if(args[index].equalsIgnoreCase("-workdir")) { index++; if(index == args.length) { syntax("working_directory was not specified."); return; } workDirectory = args[index]; File file = new File(workDirectory); if(!file.exists()) { syntax("working_directory does not exist: " + workDirectory); return; } if(!file.isDirectory()) { syntax("working_directory must be a directory: " + workDirectory); return; } } else if(args[index].equalsIgnoreCase("-iconpath")) { index++; if(index == args.length) { syntax("icon_path was not specified."); return; } iconPath = args[index]; File file = new File(iconPath); if(!file.exists()) { syntax("icon_path does not exist: " + iconPath); return; } if(file.isDirectory()) { syntax("icon_path must be a file: " + iconPath); return; } } else if(args[index].equalsIgnoreCase("-iconindex")) { index++; if(index == args.length) { syntax("icon_index was not specified."); return; } String value = args[index]; try { iconIndex = Integer.parseInt(value); } catch(NumberFormatException ex) { syntax("icon_index must be numeric: " + value); return; } } else if(args[index].equalsIgnoreCase("-window")) { index++; if(index == args.length) { syntax("window was not specified."); return; } String value = args[index]; if(value.equalsIgnoreCase("normal")) { showWindow = Shortcut.NORMAL; } else if(value.equalsIgnoreCase("minimized")) { showWindow = Shortcut.MINIMIZED; } else if(value.equalsIgnoreCase("maximized")) { showWindow = Shortcut.MAXIMIZED; } else { syntax("window is not valid: " + value); return; } } else { System.out.println("ignoring unknown option: " + args[index]); } index++; } if(index == args.length) { syntax("shortcut was not specified."); return; } shortcutFilename = args[index]; File file = new File(shortcutFilename); if(displayOnly) { if(!file.exists()) { syntax("shortcut was not found: " + shortcutFilename); return; } } else { if(file.exists() && !overwrite) { syntax("shortcut already exists: " + shortcutFilename); return; } index++; if(index == args.length) { syntax("target was not specified."); return; } targetFilename = args[index]; file = new File(targetFilename); if(!file.exists()) { syntax("target was not found: " + targetFilename); return; } index++; if(index < args.length) { arguments = ""; while(index < args.length) { arguments = arguments + " " + args[index++]; } } } Shortcut shortcut = new Shortcut(); if(!displayOnly) { shortcut.setTarget(targetFilename); shortcut.setArguments(arguments); shortcut.setWorkingDirectory(workDirectory); shortcut.setIconPath(iconPath); shortcut.setIconIndex(iconIndex); shortcut.setShowWindow(showWindow); shortcut.save(shortcutFilename); } shortcut.load(shortcutFilename); System.out.println("Shortcut: " + shortcutFilename); System.out.println("Description: " + shortcut.getDescription()); System.out.println("Target: " + shortcut.getTarget()); System.out.println("Arguments: " + shortcut.getArguments()); System.out.println("Working Directory: " + shortcut.getWorkingDirectory()); System.out.println("Icon Path: " + shortcut.getIconPath()); System.out.println("Icon Index: " + shortcut.getIconIndex()); System.out.println("Hot Key: " + shortcut.getHotKey()); System.out.println("Show Window: " + shortcut.getShowWindow()); } static void syntax() { System.out.println("usage: <javavm> shortcut {<options>}... <shortcut> {<target> {<arguments>}}"); System.out.println(); System.out.println("option: -display"); System.out.println(" -help"); System.out.println(" -overwrite"); System.out.println(" -workdir <working_directoy>"); System.out.println(" -iconpath <icon_path>"); System.out.println(" -iconindex <icon_index>"); System.out.println(" -window [minimized|maximized|normal]"); System.out.println(); } static void syntax(String message) { syntax(); if(message != null) { System.out.println(message); } System.out.println(); } }