将自定义图标分配给 Mac 桌面元素
我正在 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();
}
}
I am writing an application in Swing that involves creating an Internet shortcut on the desktop for a particular site. It works fine in Windows. Mac allows me to create the shortcut, but does not allow me to assign it my custom icon. How can I assign an icon to the URL file on a Mac programmatically?
This is my code:
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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以创建一个带有
.webloc
文件扩展名的文件,然后使用 URL 将 plist 写入该文件,即编写图标比较棘手,因为 Mac OS X 将其存储在资源叉中。
请参阅带有 Objective-C 源代码的示例应用。
You can create a file with a
.webloc
file extension, then write a plist to the file with the URL, i.e.Writing the icon is trickier, as Mac OS X stores it in the Resource Fork.
See example app with source in Objective-C.