我发布了一个 Sketch 插件,它的一些功能在文件管理中具有很强的依赖性。
执行时,插件需要检查一个文件夹是否存在,如果不存在,则创建,然后管理该目录下的几个文件。
几周前,一位用户报告说插件在新的 Sketch 版本中崩溃。
出乎意料的回应:
fileExistsAtPath
返回 - 文件夹不存在 - 当实际存在于该路径中时createDirectoryAtPath
尝试创建不存在的文件夹时返回“错误”(我已经测试过文件夹是否存在)
快速示例:
要求
var document = context.document
var documentName = document.displayName()
var documentFolderPath = decodeURIComponent(document.fileURL()).replace('file:///','').replace(documentName,"")
print(documentName)
print(documentFolderPath)
var translationsFolderName = documentName.replace('.sketch','_translations')
var translationsFolderPath = documentFolderPath+translationsFolderName+'/'
print(translationsFolderName)
print(translationsFolderPath)
var fileManager = [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath:translationsFolderPath isDirectory:'YES'])
{
print(translationsFolderPath+" folder does not exist")
if(![fileManager createDirectoryAtPath:translationsFolderPath withIntermediateDirectories:'YES' attributes:nil error:nil])
{
print(translationsFolderPath+" folder can't be created")
}
}
回复
test.sketch
Users/myuser/Documents/
test_translations
Users/myuser/Documents/test_translations/
Users/myuser/Documents/test_translations/ folder does not exist
Users/myuser/Documents/test_translations/ folder can't be created
Script executed in 0.034733s
任何的想法?
谢谢!