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

时间:2011-11-15 06:00:12

标签: java macos icons shortcuts filewriter

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

1 个答案:

答案 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将其存储在资源叉中。

请参阅example app with source in Objective-C