Search code examples
cocoapluginssketch-3

Unexpected response from NSFileManager in Sketch Plugin


I published a Sketch Plugin with some functionalities with a strong dependency in files management.

When execute, plugin needs to check if a folder exists, if not, create, and then manage several files inside this directory.

Some weeks ago, one user reported plugin was crashing in a new Sketch version.

Unexpected response from:

  • fileExistsAtPath return -folder does not exist- when actually exists in that path
  • createDirectoryAtPath return 'error' when trying to create a non-existing folder (I've tested both when folder exists and not)

Quick example:

Request

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")
    }
}

Response

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

Any idea?

Thanks!


Solution

  • Your file path is not rooted (doesn't start with /)