gpt4 book ai didi

java - 将自定义图标分配给 Mac 桌面元素

转载 作者:行者123 更新时间:2023-11-30 11:50:05 25 4
gpt4 key购买 nike

我正在用 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();
}
}

最佳答案

您可以创建一个文件扩展名为 .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 将其存储在 Resource Fork 中。

参见 example app with source in Objective-C .

关于java - 将自定义图标分配给 Mac 桌面元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8132123/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com