我正在用 Swing 编写一个应用程序,它涉及在桌面上为特定站点创建Internet 快捷方式。它在 Windows 中运行良好。Mac 允许我创建快捷方式,但不允许我为其分配自定义图标。如何以编程方式将图标分配给 Mac 上的 URL 文件?
这是我的代码:
import java.io.*;
public class MACutils {
private MACutils() {
}
public static void createInternetShortcutOnDesktop(String name,
String target, String icon) throws IOException {
String username = System.getProperty("user.home");
System.out.println(username);
String path = username + "/Desktop" + "/" + name + ".URL";
createInternetShortcut(name, path, target, icon);
}
public static void createInternetShortcut(String name, String where,
String target, String icon) throws IOException {
FileWriter fw = new FileWriter(where);
fw.write("[InternetShortcut]\n");
fw.write("URL=" + target + "\n");
if (!icon.equals("")) {
fw.write("IconFile=" + icon + "\n");
// icon has the path to my .png/.icns image
fw.write("IconIndex=0");
}
fw.flush();
fw.close();
}
}