Sketch Plugin - NSFileManager contentsOfDirectoryAtPath - not a function

259 views Asked by At

I'm trying to make a plugin for Sketch that allows me to automate the production of multilingual assets.

Right now, I select a list of languages files that kick off the process. I use NSOpen Panel to select these. For each language file, it makes a new page for that language, and finds text layers with a special tag and replaces the copy with the translated copy.

While it's doing this, I'd like it to check to see if there are any screenshots in another folder screenshots/lang, and store a list of the filepaths for those images. This is where I'm getting stuck at the moment.

Currently I have:

var screenShotPaths = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:mySourcePath error:NULL];

When I try to run my Plugin in sketch in its current state, the console is saying:

(Sketch Plugin)[6547]: TypeError: fileManager.contentsOfDirectoryAtPath_ is not a function. 
(In 'fileManager.contentsOfDirectoryAtPath_( mySourcePath)', 'fileManager.contentsOfDirectoryAtPath_' is undefined)

I've tried looking all over I see a lot of issues where the list isn't coming back,coming back null, etc, but I can't find anything where contentsOfDirectoryAtPath is coming back as not a function.

Is this because it's a plugin getting run by an application and navigating the filesystem is a security issue?

Any help would be much appreciated. Thank you!

2

There are 2 answers

0
Danny F. On

A little late to the game here, but hopefully someone searching for this will find this useful.

I found that you have to use the full method: contentsOfDirectoryAtURL_includingPropertiesForKeys_options_error(url,nil,nil,nil);

I'm more familiar with JS so I'm using js syntax. Hopefully someone else can translate this to Cocoa.

0
Sentry.co On

Works for me: (Sketch 55)

var fileManager = [NSFileManager defaultManager];
var paths = [fileManager contentsOfDirectoryAtPath:filePath error:null];