Someone requested more info, so here's a more complete script that can be placed in a folder-level JavaScript file:
var ASGJ_buttonImportIconAll = app.trustedFunction(function (doc, f_name) {
// Get a reference to the specified field
var f = doc.getField(f_name);
// If the specified field does not exist...
if (!f) {
app.beginPriv();
app.alert({
cMsg: "The specified field [" + f_name + "] does not exist.",
nIcon: 0, // Error icon
nType: 0, // OK
cTitle: "Field does not exist"
});
app.endPriv();
return;
}
// If the specified field is not a button...
if (f.type !== "button") {
app.beginPriv();
app.alert({
cMsg: "The specified field [" + f_name + "] is not a button.\r\rField type: " + f.type + "\r\rCannot import icon.",
nIcon: 0, // Error icon
nType: 0, // OK
cTitle: "Field is not a button"
});
app.endPriv();
return;
}
// If the button layout is label only...
if (f.buttonPosition === position.textOnly) {
app.beginPriv();
app.alert({
cMsg: "The specified button [" + f_name + "] has a Text-only layout.\r\rCannot import icon.",
nIcon: 0, // Error icon
nType: 0, // OK
cTitle: "Button layout is text-only"
});
app.endPriv();
return;
}
// Prompt user to select a file to use as the source for the button icon
app.beginPriv();
var oFilespec = app.browseForDoc({
cFileFilter: 2,
cWindowTitle: "Select an image"
});
app.endPriv();
// Deal with the selected file
if (typeof oFilespec !== "undefined") {
// Attempt to use the selected file as the source of the button icon
app.beginPriv();
var nReturn = f.buttonImportIcon({
cPath: oFilespec.cPath
});
app.endPriv();
switch (nReturn) {
case -1 :
app.beginPriv();
app.alert({
cMsg: "The selected file: " + oFilespec.cPath + "\r\rcould not be opened." ,
nIcon: 0, // Error icon
nType: 0, // OK
cTitle: "Cannot open selected file"
});
app.endPriv();
break;
case -2 :
app.beginPriv();
app.alert({
cMsg: "The selected page is invalid.\r\rCannot import icon.",
nIcon: 0, // Error icon
nType: 0, // OK
cTitle: "Selected page invalid"
});
app.endPriv();
break;
default :
// Import was successful or user canceled
// so there's nothing to do
}
}
});
ASGJ_buttonImportIconAll(this, event.target.name);