I actually managed to write a whole script that does exactly what you suggested and those steps I wrote for the action. The layer renaming part looks like this in my version:
// Rename the layer to the currently opened fileName
// Get the currently opened file
var file = app.activeDocument;
// Get the name of the file
var fileName = file.name;
// Remove the file extension from the name
fileName = fileName.replace(/\.[^\.]+$/, '');
// Get the current layer
var layer = app.activeDocument.activeLayer;
// Rename the layer
layer.name = fileName;
It works just as intended, but I just found out that the approach you've initially suggested turned out to be way slower, than just dropping already saved document into itself. The bigger the file, the bigger the difference.
So now I've been trying to write a script to do exactly that, but I'm afraid my knowledge is not enough to figure it out.
I ended up with the following one:
// Get a reference to the active document
var doc = app.activeDocument;
// Get the file name of the active document
var fileName = doc.name;
// Get the file path of the active document
var filePath = doc.path;
var convertToSmartObject = function () {
var idnewPlacedLayer = stringIDToTypeID('newPlacedLayer');
executeAction(idnewPlacedLayer, undefined, DialogModes.NO);
};
var replaceSmartObjectContents = function (fileName, smartObj) {
app.activeDocument.activeLayer = smartObj;
var idplacedLayerReplaceContents = stringIDToTypeID("placedLayerReplaceContents");
var desc3 = new ActionDescriptor();
var idnull = charIDToTypeID("null");
desc3.putPath(idnull, new File(fileName));
var idPgNm = charIDToTypeID("PgNm");
desc3.putInteger(idPgNm, 1);
executeAction(idplacedLayerReplaceContents, desc3, DialogModes.NO);
};
// Add a layer, this will now be the active layer
doc.artLayers.add();
// Convert active layer to Smart Object
convertToSmartObject();
// replace a provided layer with a provided file path
replaceSmartObjectContents(fileName, doc.activeLayer);
It creates an empty layer, turns it into a smart object and then should replace the contents of it with the same document that is opened, but in the case of this script it opens the Open file dialog box where you need to manually navigate to the file. It works, but it's semi-automatic. I cannot figure out a way to force it to grab the file without the Open dialog screen.
I actually figured it out! Read about the fullName attribute of the file and it seemed to work.
Here's the working script exactly for what I needed:
// Get a reference to the active document
var doc = app.activeDocument;
// Get the file name of the active document
var fileName = doc.name;
// Get the file path of the active document
var filePath = doc.path;
var docName = doc.fullName;
var convertToSmartObject = function () {
var idnewPlacedLayer = stringIDToTypeID('newPlacedLayer');
executeAction(idnewPlacedLayer, undefined, DialogModes.NO);
};
var replaceSmartObjectContents = function (docName, filePath) {
app.activeDocument.activeLayer = filePath;
var idplacedLayerReplaceContents = stringIDToTypeID("placedLayerReplaceContents");
var desc3 = new ActionDescriptor();
var idnull = charIDToTypeID("null");
desc3.putPath(idnull, new File(docName));
var idPgNm = charIDToTypeID("PgNm");
desc3.putInteger(idPgNm, 1);
executeAction(idplacedLayerReplaceContents, desc3, DialogModes.NO);
};
// Add a layer, this will now be the active layer
doc.artLayers.add();
// Convert active layer to Smart Object
convertToSmartObject();
// replace a provided layer with a provided file path
replaceSmartObjectContents(docName, doc.activeLayer);