2

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

1 回答 1

1

您可以创建一个带有.webloc文件扩展名的文件,然后将 plist 写入带有 URL 的文件,即

<?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 中。

请参阅Objective-C 中带有源代码的示例应用程序

于 2011-11-15T06:34:57.963 回答