Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include symbol overrides in find and replace text #20

Open
adamdiestelkamp opened this issue Jan 23, 2017 · 16 comments
Open

Include symbol overrides in find and replace text #20

adamdiestelkamp opened this issue Jan 23, 2017 · 16 comments

Comments

@adamdiestelkamp
Copy link

adamdiestelkamp commented Jan 23, 2017

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

@billhamway
Copy link

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

@thierryc
Copy link
Owner

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
Copy link
Collaborator

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
Copy link
Owner

  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
Copy link
Collaborator

seandellis commented May 16, 2017

@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
Copy link
Collaborator

@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
Copy link
Owner

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
Copy link
Owner

I push a pull request
#28

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

@seandellis
Copy link
Collaborator

@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. #28

@thierryc
Copy link
Owner

Thank you ! I Try to fix it today.

@thierryc
Copy link
Owner

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

screen shot 2017-05-17 at 15 17 21

@seandellis
Copy link
Collaborator

Hurray! This is fantastic!

That fixed it. Thanks so much @thierryc !

@thierryc
Copy link
Owner

thierryc commented Jul 4, 2017

Done!

@thierryc thierryc closed this as completed Jul 4, 2017
@kaiser-jakob
Copy link

has this made it into the master?

@kaiser-jakob
Copy link

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

@thierryc
Copy link
Owner

thierryc commented Dec 5, 2017

Do you have an example of your 2 strings ?

@thierryc thierryc reopened this Dec 5, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants