user3541125
user3541125

Reputation: 491

Changing App Icon

I'm trying to make an application that generates and app (with Applescript), and so far I've gone as far as generating the normally functioning app by using the (do shell script "osacompile...") command. Now, all I need is a string that will automatically also change the icon of the generated application, as I would like my own icon to be the newly generated application's icon rather than the default Applet icon.

What are some ways to approach this?

Thanks a ton.

Eric

Upvotes: 3

Views: 2720

Answers (3)

Simon Byrne
Simon Byrne

Reputation: 7874

Assuming you've created an app bundle, e.g. via:

osascript -o Foo.app foo.applescript

Then you can either simply overwrite the existing icon file:

cp -f myicon.icns Foo.app/Contents/Resources/applet.icns

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

rm Foo.app/Contents/Resources/applet.icns
cp myicon.icns Foo.app/Contents/Resources/
plutil -replace CFBundleIconFile -string myicon.icns Foo.app/Contents/Info.plist

Upvotes: 3

Noitidart
Noitidart

Reputation: 37318

Using privealaged javascript from firefox this creates the app with custom icon, the only problem is, whenever you click on the app it throws "unidentified author" and doesnt run.

but this at least shows you how to set a custom icon. credit to @djBazzieWazzie for explaining above plus 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\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\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" +
    "</plist>";

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

    location.append("Contents");
    location.create(Ci.nsIFile.DIRECTORY_TYPE, 0755);

    var info = location.clone();
    info.append("Info.plist");
    var stream = Cc['@mozilla.org/network/file-output-stream;1'].createInstance(Ci.nsIFileOutputStream);
    stream.init(info, PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE, 0600, 0);
    stream.write(contents, contents.length);
    stream.close();

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

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

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

Upvotes: 0

dj bazzie wazzie
dj bazzie wazzie

Reputation: 3542

Icons of applications are shown based on the info.plist inside the application bundle. There is a key named CFBundleIconFile whose value is the name of the icon file in the resource folder. The simplest way is that an AppleScript saved as application has the icon file applet.icns and replace it afterwards. Or you can add your own icon file to the resource folder, remove the icns file created by osacompile and change the info.plist file inside the bundle. Because you have no example code, which is normally required on StackOverflow, I can't help you further than this.

Upvotes: 1

Related Questions