I’m working to update our plugin to use the current API’s export, but I’m running into snags with the new added scale suffix. I’ve found a few discussions pointing back to this thread that recommends using a move item function to fix the exported file name.
Why the fuss?
We have hundreds of assets that get are being scaled and exported across hundreds of files. Sometimes replacing older assets in the process. If a file needs a scale suffix, we will explicitly call for it in the filename. Having Sketch export an icon-60@2x@0.2x.png
file won't work.
Trying to calculate suffix…
I’ve got a rough function to compute the scale suffix:
// Calculates Sketch scale value that will be injected into filename as a suffix.
function estimatedScaleSuffix(scaleValue){
let scaleSuffix = ''
if(scaleValue > 1){
scaleSuffix ='@'+Math.floor(scaleValue)+'x'
}else if(scaleValue < 1){
if(scaleValue === 0.25){
scaleSuffix ='@0.2x' //Fixes rounding conflict between 0.3 value toFixed returns and Sketch's value
}else{
scaleSuffix ='@'+scaleValue.toFixed(1)+'x'
}
}else{
scaleSuffix=''
}
return scaleSuffix
}
Is there a better way to compute this?
Trying to rename assets
With the suffix calculated, I was able to target the exports assets:
let actualFile = exportFolder+’/‘+exportName+estimatedScaleSuffix(scalePercentage)+'.'+fileType
let requestedFile = exportFolder+'/'+exportName+'.'+fileType
NSFileManager.defaultManager().moveItemAtPath_toPath_error(actualFile, requestedFile, nil)
That works on an initial export, but If I try to re-export over existing files it will fail to move the assets. Resulting in redundant files (e.g. an old icon.png
next to an icon@0.2x.png
that should have replaced it).
Is there a way to force the new file to overwrite on move?