Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #target Illustrator
- // script.name = joinText.jsx;
- // script.description = joins broken text pieces (usually from PDF's created with other applications) back into words and or sentences
- // script.requirements = an open document with some POINT text to join
- // script.parent = carlos canto // 11/10/13;
- // script.elegant = false;
- // Usage: The script targets all text in a document by default, when run, it will read the position of all frames and determine if 2 adjacent frames
- // are close enough to be joined into a word, far enough to be considered another word of the same sentece or way too far to be a separate frame.
- //
- // press and hold Ctrl/Cmd before running the script to process Selected text frames only (text inside groups are NOT targeted)
- // press and hold Shift before running the script to change space detection settings
- function main () {
- try {var idoc = app.activeDocument;}
- catch (e) {alert ('open a document and try again'); return };
- var tfObj=idoc.textFrames;
- var letterSensitivity = 4;
- var wordSensitivity = 2;
- var sensitivity = [letterSensitivity, wordSensitivity];
- if(ScriptUI.environment.keyboardState.shiftKey) {
- var msg = '(4,2) should work for most font types & sizes...,';
- msg = msg+'\r- decrease 1st number (3,2) to detect wider letter spacing';
- msg = msg+'\r- decrease 2nd number (4,1) to detect wider word spacing';
- msg = msg+'\r- increase 2nd number (4,4) to make two separate frames';
- strRes = prompt (msg, sensitivity.join (','), 'Letter/Word/Frame Spacing Sensitivity Options');
- if (strRes!=null) {// quit if pressed Cancel
- sensitivity = strRes.split (',');
- }
- else {alert ('cancelled by user'); return};
- }
- if(ScriptUI.environment.keyboardState.ctrlKey || ScriptUI.environment.keyboardState.metaKey) { // metaKey = cmd key on a mac
- //alert('ctrl key');
- var sel = idoc.selection;
- tfObj = null;
- tfObj = [];
- for (t=0; t<sel.length; t++) {
- var itext = sel[t];
- if(itext.typename=="TextFrame" && itext.kind=="TextType.POINTTEXT")
- tfObj.push(itext);
- }
- }
- // check version and switch order accordingly
- var ver = Number((app.version).substr (0, 2));
- var x = -1; // CS5
- if (ver<15)
- x = 1; // CS4
- var objectProperties = []; // array to hold top, left, and each frame
- // get each text frame properties
- for (var i=0; i<tfObj.length; i++){
- var tf = tfObj[i];
- var tfTop = Math.round(tf.position[1]);
- var tfLeft = Math.round(tf.position[0]);
- objectProperties.push([tfTop, tfLeft, tf]);
- }
- var objCount = objectProperties.length;
- var pBar = new ProgressBar('joinText Script');
- pBar.reset('Processing...sorting text frames', objCount);
- // sort by top then by left
- var sortedArr = objectProperties.sort(mysortfunction);
- pBar.close();
- // get rid of top and left, so final array has only text frames
- var tFrames = [];
- for (var j=0; j<sortedArr.length; j++){
- tFrames.unshift (sortedArr[j][2]);
- }
- joinFrames (tFrames, sensitivity);
- // Sort function by dcp @ stackoverflow.com
- function mysortfunction(a, b) {
- pBar.hit();
- var o1 = a[0]; // sort by first Field (top)
- var o2 = b[0];
- var p1 = a[1]; // then sort by second Field (left)
- var p2 = b[1];
- if (o1 != o2) {
- if (o1 < o2) return x; // for CS4 we need to use 1, and -1 for CS5
- if (o1 > o2) return -x;
- return 0;
- }
- if (p1 < p2) return -1;
- if (p1 > p2) return 1;
- return 0;
- } // end sort function
- } // end main function
- ///////////////////////////////////////////////////////////////////////////////////////////////////////
- function joinFrames (tFrames /*array of textFrames*/, sensitivity /*array [4,2] */) {
- var letterSensitivity = sensitivity[0];
- var wordSensitivity = sensitivity[1];
- var removeFrames = [];
- var frameCount = tFrames.length;
- var pBar = new ProgressBar('joinText Script');
- pBar.reset('Processing...joining frames', frameCount);
- for (k = 1 ; k<frameCount ; k++, pBar.hit(k) ) { //loop thru textFrames
- var rtFrame = tFrames[k-1];
- var ypos = Math.round (rtFrame.position[1]); // get y
- var rtbounds = rtFrame.geometricBounds;
- var ltFrame = tFrames[k];
- if (Math.round(ltFrame.position[1]) == ypos) {
- var ltbounds = ltFrame.geometricBounds;
- var letterSpacing = rtFrame.textRange.characterAttributes.size/letterSensitivity;
- var wordSpacing = rtFrame.textRange.characterAttributes.size/wordSensitivity;
- if (ltbounds[2]+letterSpacing >= rtbounds[0]) {
- ltFrame.contents = ltFrame.contents+rtFrame.contents;
- removeFrames.push(rtFrame);
- }
- else {
- if (ltbounds[2]+wordSpacing >= rtbounds[0]) {
- ltFrame.contents = ltFrame.contents+" "+rtFrame.contents;
- removeFrames.push(rtFrame);
- }
- }
- }
- }
- var removedCount = removeFrames.length;
- pBar.reset('Processing...deleting incomplete frames', removedCount);
- for (e=0; e<removedCount; e++, pBar.hit(e) )
- removeFrames[e].remove();
- pBar.close();
- var helpmsg = '\r\rHelp: \rScript works on all text by default, press & hold \rCtrl/Cmd before launching the script \rto join selected text frames only.';
- var settingsmsg = '\r\rDefault Settings should work for most scenarios, \rif you ever need to fine tune spacing detection, \r(i.e. for Justified paragraphs) hold Shift before \rlaunching the script to bring up Settings Dialog.';
- alert('Initial frame count: ' + frameCount + ' \rFinal frame count: ' + (frameCount-removedCount)+ helpmsg + settingsmsg);
- }
- // progress bar by Marc Autret
- function ProgressBar(/*str*/title)
- {
- var w = new Window('palette', ' '+title, {x:0, y:0, width:340, height:60}),
- pb = w.add('progressbar', {x:20, y:12, width:300, height:12}, 0, 100),
- st = w.add('statictext', {x:10, y:36, width:320, height:20}, '');
- st.justify = 'center';
- w.center();
- this.reset = function(msg,maxValue)
- {
- st.text = msg;
- pb.value = 0;
- pb.maxvalue = maxValue||0;
- pb.visible = !!maxValue;
- w.show();
- };
- this.hit = function(value) {/*st.text = '\tProcessing... ' + value; */++pb.value;};
- this.hide = function() {w.hide();};
- this.close = function() {w.close();};
- };
- main ();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement