user3541125 user3541125 - 1 year ago 112
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.


Answer Source

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\" \"\">\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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download