thierryc / Sketch-Find-And-Replace

Sketch plugin to do a find and replace on text within layers
Apache License 2.0
710 stars 37 forks source link

Include symbol overrides in find and replace text #20

Open adamdiestelkamp opened 7 years ago

adamdiestelkamp commented 7 years ago

This is a great plugin, would love to see it expanded to be able to include the text in symbol overrides also.

billhamway commented 7 years ago

Yep, find and replace in symbol overrides would be a huge win!

thierryc commented 7 years ago

Do you have any advice to do this? I try to find some documentation about text in symbols overrides to do it !

Please share your knowledge about this feature. I am ready to Fork.

seandellis commented 7 years ago

I'd love this feature as well.

@thierryc The best discussion I've seen about accessing and changing text within symbols is on the Sketch Developers discussion board.

thierryc commented 7 years ago
  1. detect if is a layer or symbol.
  2. if it is a symbol get overrides (array)
  3. for each override find and replace
  4. replace overrides

How to get and set overrides ? Do you know any plugins working with symbols and overrides?

seandellis commented 7 years ago

@thierryc I'm not aware of any plugins that work with symbols and overrides. :(

I'm not very familiar with javascript, but in that same discussion a user shared this code. It might help with working with overrides.

// HACKY
function nsArrayToArray(nsArray) {
  var result = [];
  for(var i = 0; i < nsArray.count(); i++) {
    result[i] = nsArray.objectAtIndex(i);
  }
  return result;
}

function fieldsForInstance(symbolInstance) {
  var master = symbolInstance.symbolMaster();
  return nsArrayToArray(master.children());
}

function setSymbolOverride(instance, fieldName, value) {
  log('updating override ' + fieldName +' with: ' + value);
  var existingOverrides = instance.overrides() || NSDictionary.dictionary();
  var values = NSMutableDictionary.dictionary();
  if (existingOverrides.objectForKey(0)){
    var existingValues = existingOverrides.objectForKey(0);
    values = NSMutableDictionary.dictionaryWithDictionary(existingValues)
  }
  var field = fieldsForInstance(instance).find(function(f){ return f.name() == fieldName; });
  values.setObject_forKey(value, field.objectID());
  var overrideValuesWrapper = NSMutableDictionary.dictionary();
  overrideValuesWrapper.setObject_forKey(values, 0);
  instance.applyOverrides_allSymbols(overrideValuesWrapper, false);
}

var instance = context.selection[0];
setSymbolOverride(instance, 'title', 'New Title set by plugin')

If it doesn't, take a look at the entire discussion thread.

http://sketchplugins.com/d/154-applyoverrides-allsymbols-depreciated-in-43/20

seandellis commented 7 years ago

@thierryc I'm still trying to figure out how to write plugins. If you have any problems, I'd recommend asking questions on the Sketch Plugins discussion board. There's lots of friendly and helpful developers on there.

thierryc commented 7 years ago

Thank You Sean. Let me figure out.

I also start a discussion "Set and get Symbol Overrides" in sketchplugins.com. http://sketchplugins.com/d/224-set-and-get-symbol-overrides

thierryc commented 7 years ago

I push a pull request https://github.com/mscodemonkey/Sketch-Find-And-Replace/pull/28

Try it; https://github.com/anotherplanet-io/Sketch-Find-And-Replace

seandellis commented 7 years ago

@thierryc Wow! That was quick. Great job!

I tested a simple document and it worked great, but had problems on a more complicated document.

I've added a bug report on your pull request. https://github.com/mscodemonkey/Sketch-Find-And-Replace/pull/28

thierryc commented 7 years ago

Thank you ! I Try to fix it today.

thierryc commented 7 years ago

I fix it ! @seandellis Do you confirm ? Please try it !

screen shot 2017-05-17 at 15 17 21
seandellis commented 7 years ago

Hurray! This is fantastic!

That fixed it. Thanks so much @thierryc !

thierryc commented 7 years ago

Done!

kaiser-jakob commented 6 years ago

has this made it into the master?

kaiser-jakob commented 6 years ago

sorry for the question. it does work. I had regexp support selected and a string with parentheses :P

thierryc commented 6 years ago

Do you have an example of your 2 strings ?