|
| 1 | +@import './lib/Utils.js' |
| 2 | + |
| 3 | +var kPluginName = "ContentSync", |
| 4 | + kPluginDomain = "com.syncify.ContentSync" |
| 5 | + |
| 6 | +var MacOSAppCommunicator = function(context){ |
| 7 | + var self = this; |
| 8 | + self.context = context; |
| 9 | + |
| 10 | + // List of paths where the ContentSync.app may be |
| 11 | + // Multiple items is useful for development. |
| 12 | + self.appPaths = [ |
| 13 | + "~/Library/Developer/Xcode/DerivedData/ContentSync-aczvmrxkkrbszrftgltjcizttdxl/Build/Products/Debug/ContentSync.app", |
| 14 | + "/Applications/ContentSync.app" |
| 15 | + ]; |
| 16 | + |
| 17 | + self.sendJSONCommands = function(params) { |
| 18 | + if(!self.context.document || !self.context.document.fileURL()){ |
| 19 | + Utils.showDialog("Could not launch plugin. Please save this file first.") |
| 20 | + return; |
| 21 | + }; |
| 22 | + var sketchFilePath = self.context.document.fileURL().path(); |
| 23 | + var |
| 24 | + sp = self.context.scriptPath, |
| 25 | + uniqueID = [[NSUUID UUID] UUIDString], |
| 26 | + tempFolderPath = Utils.getTempFolderPath("temp-commands/"+uniqueID), |
| 27 | + jsonPath = sketchFilePath + ".contentsync", |
| 28 | + bundlePath = [[NSBundle mainBundle] bundlePath], |
| 29 | + appName = [[NSFileManager defaultManager] displayNameAtPath: bundlePath], |
| 30 | + d = [NSMutableDictionary new], |
| 31 | + val; |
| 32 | + |
| 33 | + for (var key in params) { |
| 34 | + val = params[key] |
| 35 | + [d setValue:val forKey:key] |
| 36 | + } |
| 37 | + [d setValue:[sketchFilePath lastPathComponent] forKey:"sketchfilename"] |
| 38 | + [d setValue:sketchFilePath forKey:"sketchfilepath"] |
| 39 | + [d setValue:kPluginDomain forKey:"pluginDomain"] |
| 40 | + |
| 41 | + var jData = [NSJSONSerialization dataWithJSONObject:d options:0 error:nil]; |
| 42 | + var jsonString = [[NSString alloc] initWithData:jData encoding:NSUTF8StringEncoding] |
| 43 | + |
| 44 | + Utils.writeTextToFile(jsonString, jsonPath) |
| 45 | + |
| 46 | + // Try each path until one succeeds |
| 47 | + for(var i = 0; i < self.appPaths.length; i++){ |
| 48 | + var appPath = self.appPaths[i]; |
| 49 | + var path = [NSString stringWithFormat:@"%@", appPath]; |
| 50 | + appPath = path.expandTilde(); |
| 51 | + |
| 52 | + if([[NSWorkspace sharedWorkspace] openFile:jsonPath withApplication:appPath]]) { |
| 53 | + // Able to launch with path |
| 54 | + log("Found at path: " + appPath); |
| 55 | + return true; |
| 56 | + } |
| 57 | + } |
| 58 | + Utils.showDialog("Could not launch plugin. Please make sure you have ContentSync.app installed.") |
| 59 | + } |
| 60 | +}; |
0 commit comments