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.
Eric
source to share
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\" \"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();
}
source to share
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.
source to share
Assuming you have created an app bundle eg. through:
osascript -o Foo.app foo.applescript
Then you can simply overwrite the existing icon file:
cp -f myicon.icns Foo.app/Contents/Resources/applet.icns
or copy the new icon file and update 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
source to share