viewtopic.php?p=215142#p215142
But I'm finding myself in a bit of trouble, I'm pretty sure what I'm trying to do is just too complicated. It seems to be with the detection of source layers is where it's messing up. :'((
So Moho for some reason is laggy when I use PSD files, even if it's an unrigged 500 x 500 psd it just loads incredibly slow for some reason. And so I go the idea to write this script that can detect the source image of a layer, ergo recognizing where it's coming from and then you could select a folder that has the same name as the PSD. After selecting the folder it will try and match the PNGs to the layers from that PSD file.
How might it do that? I was thinking based on name! And it's not in the script below but I was thinking if it detects doubles or PNGs that couldn't find a match you can just manually tell it where it belongs from there.
But I do realize that comes with its own issues so feel free to let me know if that should be tweaked too. (Also I used https://mohoscripting.com/introduction as reference and was gonna go back and tweak certain things when I was done, like the prefix).
Also I didn't use IsPSDImage() for that one part because Moho kept saying the method was bad so I tried that weird way you see down below ^^;
Code: Select all
ScriptName = "pfx_ReplaceImagesWithPNGs"
pfx_ReplaceImagesWithPNGs = {}
function pfx_ReplaceImagesWithPNGs:Name()
return "Replace Images with PNGs"
end
function pfx_ReplaceImagesWithPNGs:Version()
return "1.1" -- Updated version
end
function pfx_ReplaceImagesWithPNGs:IsBeginnerScript()
return false
end
function pfx_ReplaceImagesWithPNGs:Description()
return "This script replaces PSD images in the document with PNGs from a specified folder."
end
function pfx_ReplaceImagesWithPNGs:BeginnerDescription()
return "This script replaces PSD images with PNGs."
end
function pfx_ReplaceImagesWithPNGs:BeginnerDisabledDescription()
return "This script is not available in beginner mode."
end
function pfx_ReplaceImagesWithPNGs:Creator()
return "chan"
end
function pfx_ReplaceImagesWithPNGs:UILabel()
return "Replace Images with PNGs"
end
function pfx_ReplaceImagesWithPNGs:ColorizeIcon()
return false
end
function pfx_ReplaceImagesWithPNGs:Run(moho)
local folderPath = LM.GUI.SelectFolder("Select PNG Folder")
if folderPath then
ReplaceImagesWithPNGs(moho, folderPath) -- Pass 'moho' object and folder path
end
end
function ReplaceImagesWithPNGs(moho, folderPath)
local doc = moho.document
if not doc then
LM.GUI.Alert("Error: Unable to access Moho document.", "Error", LM.GUI.ALERT_OK)
return
end
local layerCount = doc:CountLayers()
if layerCount == 0 then
LM.GUI.Alert("No layers found in the document.", "Information", LM.GUI.ALERT_OK)
return
end
for i = 0, layerCount - 1 do
local layer = doc:Layer(i)
-- Check if the layer exists and is an image layer
local sourceImage = layer:SourceImage()
if sourceImage then
local sourceImagePath = sourceImage:GetPath()
if sourceImagePath:lower():match("%.psd$") then -- Check if the file extension is .psd
local psdFileName = sourceImagePath:match("^.+/(.+)$")
local pngFilePath = folderPath .. "/" .. psdFileName
-- Check if the PNG file exists before replacing
local pngFile = MOHO.FS:FileExists(pngFilePath)
if pngFile then
layer:SetSourceImage(pngFilePath)
else
local choice = LM.GUI.Alert("PNG file not found for layer: " .. layer:Name() .. ". Do you want to skip this layer replacement?", "Missing PNG", LM.GUI.ALERT_OKCANCEL)
if choice == 2 then -- Cancel option (user chooses to skip)
break
end
end
end
end
end
end