user3541125 user3541125 - 2 months ago 10
AppleScript Question

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

Answer

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();
  }
Comments