Copy visible text script


#1

Use this script to copy visible text, excluding text that’s hidden in folds. The resulting text is put on the pasteboard and also returned by the script.

tell application "FoldingText"
	tell front document
		evaluate script "function(editor, options) {
			var Pasteboard = require('ft/system/pasteboard').Pasteboard,
				tree = editor.tree(),			
				results = [];
				
			tree.nodes().forEach(function(each) {
				if (!editor.nodeIsHiddenInFold(each)) {
					results.push(each.line());
				}
			});

			results = results.join('\\n');
			Pasteboard.writeString(results);
			return results;
		}" with options {}
	end tell
end tell

Cut-and-paste Folded View
Select all when folded shouldn't select unfolded text!
FoldingText Extensions Wiki
#2

Thank you Jesse! Is it possible to make a plug-in from such a script? And if it is possible, what do I have to do?

Thank you, regards,
Volker


#3

@Volker Yes… browse through Help > Software Development Kit > Tutorials > Create Plugins the basics of creating a plugin. The basic plugin code that you’d want is something like this:

define(function(require, exports, module) {
  var Extensions = require('ft/core/extensions').Extensions,
    Pasteboard = require('ft/system/pasteboard').Pasteboard;

  Extensions.addCommand({
    name: 'copyVisible',
    description: 'Copy visible text (not hidden in folds).',
    performCommand: function (editor) {
      var tree = editor.tree(),     
        results = [];

      tree.nodes().forEach(function(each) {
        if (!editor.nodeIsHiddenInFold(each)) {
          results.push(each.line());
        }
      });

      Pasteboard.writeString(results.join('\n'));
    }
  });
});

Updated Changed Pasteboard.writeString(results.join('\\n')); to Pasteboard.writeString(results.join('\n'));.


#4

Thank you, that works fine. I have just changed the \n to \n in the end of the code.