Change application icon

I'm trying to create an app that generates both an app (with Applescript) and so far I've gotten to building a properly functioning app using (do shell script "osacompile ..."). Now all I need is a line that will automatically change the icon of the generated application as well, as I would like my own icon to be the newly created application icon, not the default Applet icon.

How can you approach this?

Thanks a ton.



source to share

3 answers

Using privealaged javascript from firefox creates an app with a custom icon, the only problem is that whenever you click on the app, it throws "unidentified author" and won't start.

but that at least shows you how to set a custom icon. credit to @djBazzieWazzie for explanation above and directories.

  _createShortcutMac : function(target, name, id, icon, location) {
      var desktop = dirSvc.get("Desk", Ci.nsIFile);
      this._createBundle(target, name, id, icon, desktop);

  _createBundle : function(target, name, id, icon, location) {
    var contents =
    "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
    "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"\">\n" +
    "<plist version=\"1.0\">\n" +
    "<dict>\n" +
    "<key>CFBundleExecutable</key>\n" +
    "<string>" + name + "</string>\n" +
    "<key>CFBundleIconFile</key>\n" +
    "<string>" + icon.leafName + "</string>\n" +
    "</dict>\n" +

    location.append(name + ".app");
    if (location.exists())
    location.create(Ci.nsIFile.DIRECTORY_TYPE, 0755);

    location.create(Ci.nsIFile.DIRECTORY_TYPE, 0755);

    var info = location.clone();
    var stream = Cc[';1'].createInstance(Ci.nsIFileOutputStream);
    stream.init(info, PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE, 0600, 0);
    stream.write(contents, contents.length);

    var resources = location.clone();
    resources.create(Ci.nsIFile.DIRECTORY_TYPE, 0755);
    icon.copyTo(resources, icon.leafName);

    var macos = location.clone();
    macos.create(Ci.nsIFile.DIRECTORY_TYPE, 0755);

    var cmd = "#!/bin/sh\nexec " + target.path + " -webapp ID";
    var script = macos.clone();
    stream.init(script, PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE, 0755, 0);
    stream.write(cmd, cmd.length);




App icons are shown based on info.plist inside the app bundle. There is a named CFBundleIconFile

key whose value is the name of the icon file in the resource folder. The easiest way is to have the AppleScript saved as an application have an applet.icns icon file and then replace it. Or you can add your own icons file to the resources folder, delete the icns file generated by osacompile and modify the info.plist file inside the package. Since you don't have the sample code that StackOverflow usually requires, I can't help you with that.



Assuming you have created an app bundle eg. through:

osascript -o foo.applescript


Then you can simply overwrite the existing icon file:

cp -f myicon.icns


or copy the new icon file and update Info.plist:

cp myicon.icns
plutil -replace CFBundleIconFile -string myicon.icns




All Articles