Sketch 插件中 NSFileManager 的意外响应

Unexpected response from NSFileManager in Sketch Plugin

我发布了一个 Sketch 插件,其中一些功能对文件管理有很强的依赖性。

插件执行时需要检查文件夹是否存在,如果不存在则创建,然后管理该目录下的多个文件。

几周前,一位用户报告插件在新的 Sketch 版本中崩溃。

意外响应来自:

快速示例:

请求

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

有什么想法吗?

谢谢!

您的文件路径不是根目录(不是以 / 开头)