Skip to content

Instantly share code, notes, and snippets.

@turbobabr
Last active January 8, 2018 10:55
Show Gist options
  • Save turbobabr/3ed0986a53d31f177e1c to your computer and use it in GitHub Desktop.
Save turbobabr/3ed0986a53d31f177e1c to your computer and use it in GitHub Desktop.
// Converts JSON string to JavaScript Object.
function jsonToObject(json) {
var str=[[NSString alloc] initWithString:json];
return [NSJSONSerialization JSONObjectWithData:str.dataUsingEncoding(NSUTF8StringEncoding) options:0 error:nil];
}
// Converts JavaScript Object to JSON string.
function objectToJson(obj,prettyPrint) {
var prettyPrint = prettyPrint || false;
var error=MOPointer.alloc().init();
var jsonData = [NSJSONSerialization dataWithJSONObject:obj options:((prettyPrint) ? NSJSONWritingPrettyPrinted : 0) error:error];
if (!jsonData) {
// print(error.value());
return null;
} else {
return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}
}
function ModelStorage(obj,useNameStorage) {
return {
set: function(key,value,remove) {
var remove = remove || false;
if(!useNameStorage) {
if (obj.cache() === null) {
return this;
}
[[obj cache] setObject:value forKey:key];
} else {
var name = obj.name();
if(this.nameContainsData(name)) {
var parts=this.extractDataParts(name);
if(parts[1].length>0) {
var data=jsonToObject(parts[1]);
if(data===null) return null;
var newData={};
for(var objKey in data) {
if(objKey!=key) {
newData[objKey]=data[objKey];
}
}
if(!remove) { newData[key]=value; }
obj.setName(parts[0]+"[!$#]"+objectToJson(newData)+"[!$#]"+parts[2]);
}
} else {
if(!remove) {
var data={};
data[key]=value;
obj.setName(name+"\n"+"[!$#]"+objectToJson(data)+"[!$#]");
}
}
}
return this;
},
get: function(key) {
if(!useNameStorage) {
if(obj.cache()===null || obj.cache()===undefined) {
return null;
}
return obj.cache().objectForKey(key);
} else {
var parts=this.extractDataParts(obj.name());
if(parts.length<2) return null;
if(parts[1].length>0) {
var data=jsonToObject(parts[1]);
if(data===null) return null;
return data[key];
}
return null;
}
},
nameContainsData: function(str) {
return str.split("[!$#]").length>1;
},
extractDataParts: function(str) {
return str.split("[!$#]");
},
normalized: function(str) {
var parts=str.split("[!$#]");
if(parts.length>2) {
return (parts[0]+parts[2]).replace(/\n/g, '');
}
return str;
},
normalizeName: function() {
obj.setName(this.normalized(obj.name()));
},
remove: function(key) {
if(useNameStorage) {
this.set(key,undefined,true);
} else {
// TO-DO: The Finest Code Goes here! :)
}
return this;
}
};
};
// Entry-Point.
(function(){
var layer=selection.firstObject();
if(layer) {
print("Layer's name before:");
print(layer.name())
print(" ");
var pluginAMetaID="com.turbobabr.annotation-kit";
var pluginBMetaID="com.SomeAwesomeCompany.ReallyUsefulPlugin";
// Plugin A sets its metadata.
ModelStorage(layer,true).set(pluginAMetaID,{
toolType: "Annotation.Arrow",
scale: 5,
description: "This is a very informative description..."
});
// Plugin B sets its metadata.
ModelStorage(layer,true).set(pluginBMetaID,{
id: "EFA3F9E9-FB79-4DB6-B250-56ABF908BD65",
status: "Really tired... :("
});
// Plugin A accesses its metadata.
var dataA=ModelStorage(layer,true).get(pluginAMetaID);
if(dataA!=null) {
print("Plugin A metadata:")
print("Tool Type: "+dataA.toolType);
print("Scale Factor: "+dataA.scale);
print("Description: "+dataA.description);
print(" ");
}
// Plugin B accesses its metadata.
var dataB=ModelStorage(layer,true).get(pluginBMetaID);
if(dataB!=null) {
print("Plugin B metadata:")
print("ID: "+dataB.id);
print("Status: "+dataB.status);
print(" ");
}
print("Layer's name after:");
print(layer.name())
print(" ");
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment