我正在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();
}
}
答案 0 :(得分:1)
您可以创建一个文件扩展名为.webloc
的文件,然后使用URL将plist写入文件,即
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>URL</key>
<string>http://hasseg.org/setWeblocThumb/#scmRepoInfo</string>
</dict>
</plist>
编写图标比较棘手,因为Mac OS X将其存储在资源叉中。