Extra space at end of sentence?

Good morning all of you awesome people. Thanks for your help.

Here is a dilemma I have. I thought I’d ask the group. I searched here, on google, and in action lists, and maybe I was too tired, but I didn’t find anything.

I coordinate trainings with psychologists. We have multiple slide decks with information and each has their own reference list in APA format. But now we have to put these references into one list.

I volunteered to do so because I knew that Drafts app would be great for this with sort, pre/append, and other actions. I took the 7 Word documents, each about 3 pages or so long, with references and copy/pasted into Drafts to sort. Easy peasy.

Not easy. First of all, many people that use Word don’t use styles or carriage returns or tabs and will add spaces everywhere to move text. What I ended up with was a mixing of references. At over 54,000 characters and nearly 8,000 words, this was an issue. I was able to fix this easy enough, but the thing that kept me up that some of the text had no spaces at the end of a sentence and before the paragraph return, some had one space, some had two spaces, some had seven.

When I sort a markdown list, some of which with less than 2 spaces after a sentence, it caused havoc with sorting. I tried to append an extra space, but it added them after the carriage return. I tried to insert emojis so that I could replace them with two spaces. I tried markdown return cleanup actions, and other actions, but nothing worked.

I also noted that after going through essentially 21 pages of references and tediously checking for spaces and returns, that if I sorted it, I couldn’t CMD-Z to undo it. I must have gone through the entire list three times, making changes, only to sort it, find a mistake, and be unable to unsort it. So I eventually learned to copy/past into a NEW draft and sort that one.

What I imagined would take me only a few minutes on an iPhone turned into over an hour of work on my desktop.

Here is an example. Notice the differences at the end of the sentence for number of spaces. Did I miss a great action that is out there that strips the carriage returns and extra white spaces and replaces with two spaces and a return?

DC: American Psychiatric Association.
Ahern, J., Worthen, M., Masters, J., Lippman, S. A., Ozer, J., & Moos, R. (2015). The challenges of Afghanistan and Iraq veterans’ transition from military to civilian life and approaches to reconnection. PLoS ONE, 10(7). The Challenges of Afghanistan and Iraq Veterans’ Transition from Military to Civilian Life and Approaches to Reconnection
Alexander, M., Ray, M. A., Hebert, J. R., Youngstedt, S. D., Zhang, H., Steck, S. E., . . . Burch, J. B. (2016). The National Veteran Sleep Disorder Study: Descriptive epidemiology, and secular trends, 2000-2010. SLEEP, 16(7), 1399-1410. http://dx.doi.org/10.5665/sleep.5972

I’m going by the description here as your example has been reformatted by the forum (post between triple backticks to retain the original format).

It reads like you have some lines of text that end with 0, 1, or 2 spaces followed by a newline character, and you would like them all to end with two spaces and a newline character.

Assuming that is the case, if you put this in a script step in an action, I think it should do what you want.

// Save a version of the draft as a backup
draft.saveVersion();

// Remove spaces at line ends (end of line spaces -> no spaces)
draft.content = draft.content.replace(/( *)\n/gm, "\n");

// Add double spaces to all line ends (0 spaces -> 2 spaces)
draft.content = draft.content.replace(/\n/gm, "  \n");

// Update the draft
draft.update();

It uses “regular expressions” to do the find and replace to effectively eliminate all spaces at the end of lines to get to an even ground, and then add two spaces to all line ends. Note, it also backs up to a saved draft version at the start so you should be able to “undo” by restoring the old version.

Does that help?

2 Likes

This was what I needed.

Finally getting around to this after work trainings and taking the kiddos to Disneyland. But thanks @sylumer for your help!