Remove tab indents in Pages [noob]

I’ve searched this and found nothing. I imagine that’s because I couldn’t identify the right search criteria, and I assume what I want would be something quite simple. (Or maybe not?)

So I’ve drafted a paper in my favourite mindmapping app, and exported the mindmap to a .txt file. When I open the latter in Pages (or any other text app, for that matter) I have a heap of paragraphs most of which have a first line indent of at least one tab-space - some as many as maybe five tab-spaces. What I want is to quickly reformat the whole thing so that every line of every paragraph is fully left-justified - i.e. all tab spaces removed.

In other words, I’d like an applescript that would automate ‘Find - Replace all with <>’.

Any suggestions? Thanks.

Model: MBP late 2011
AppleScript: 2.2.4
Browser: Safari 536.30.1
Operating System: Mac OS X (10.8)

Hello.

If I have understood you right, you want to remove runs of tabs from the start of the lines. The script below does that, and saves the file you have picked with a name of original name.new.text in the same folder.

set oldfile to POSIX path of (choose file)
set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "."}
set stem to text items 1 thru -2 of oldfile
set AppleScript's text item delimiters to tids
set newfile to stem & ".new.txt" as text
do shell script "sed -n 's/^[	]*//p' " & quoted form of oldfile & " > " & quoted form of newfile

Thanks very much, McUsrII. That’s amazingly prompt.

Unfortunately it produced a strange result for me, though. The new file still has all the initial tabs, and it also has a blank space after every character.

Is there something I’ve done wrong? Thanks.

Hello

This short script is supposed to fit your needs.


tell application "Pages" to tell document 1
	set body text to my remplace(body text, tab, space)
end tell

#=====
(*
replaces every occurences of d1 by d2 in the text t
*)
on remplace(t, d1, d2)
	local oTIDs, l
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d1}
	set l to text items of t
	set AppleScript's text item delimiters to d2
	set t to "" & l
	set AppleScript's text item delimiters to oTIDs
	return t
end remplace

#=====

KOENIG Yvan (VALLAURIS, France) lundi 8 juillet 2013 10:00:16

Well.

I really supposed that you had saved the file as a text file. Try copy the text to the clipboard, open a terminal window, and issue the command “cat >file.text” when it “hangs” hit command v, and when you see your text has been entered, then hit ctrl-C, then rerun the script.

I hope this works for you.

McUsrII - that didn’t change the result. But perhaps that’s because I was trying to run it as a Hazel action?

Anyway . Yvan - your’s worked a treat. Thanks very much :slight_smile:
Now … would you mind me craving further indulgence, if you have time? Something that would be even better still, but perhaps more complex, would be if the script would replace the first tab indent in a paragraph with a single linespace, and simply remove following tabs (if any). Could that be done?

Thanks in hopeful anticipation …

I apologize but my English is far from perfect.
If I understand well, you wish that every leading tab is replaced by a line break (return or line feed)
and that other tabs are removed.

If these tabs are removed, the text will be something like :

Value1Value2Value3Value4

OtherValue1OtherValue2OtherValue3OtherValue4

Is it really what you want ?

Here is a script which replace leading tabs by line breaks
and replace other tab characters by space ones.


tell application "Pages" to tell document 1
	set character 1 of body text to return & character 1 of body text
	set body text to my remplace(body text, return & tab, return & return)
	set body text to my remplace(body text, tab, space)
	set character 1 of body text to ""
end tell

#=====
(*
replaces every occurences of d1 by d2 in the text t
*)
on remplace(t, d1, d2)
	local oTIDs, l
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d1}
	set l to text items of t
	set AppleScript's text item delimiters to d2
	set t to "" & l
	set AppleScript's text item delimiters to oTIDs
	return t
end remplace

#=====

I know that we may use lists of delimiters but I’m not at ease with this “new” feature.

KOENIG Yvan (VALLAURIS, France) lundi 8 juillet 2013 11:28:30

Yvan,

That is just awesome. Thank you so very much. Works superbly . Although in the process of running it, I’ve discovered that as well as having leading tabs I also have some leading spaces, which I also don’t want.

So after you’ve been so kind already, could I crave one further addition to the script … So to adapt your own words, I’d like “every leading tab replaced by a line break (return or line feed) and other tabs and leading spaces removed”.

Thanks humbly again.


property tab2space : true
# if you really want to suppress the tab characters without replacing them by space set the property value to false
property keepReturn : true
# true = the linefeeds will be replaced by returns
# false = the returns will be replaced by linefeeds

if keepReturn then
	set {lb1, lb2} to {return, linefeed}
else
	set {lb1, lb2} to {linefeed, return}
end if

tell application "Pages" to tell document 1
	# according to keepReturn setting replace characters lb2 by characters lb1
	set body text to my remplace(body text, lb2, lb1)
	
	set character 1 of body text to lb1 & character 1 of body text
	# remove every space char which is at beginning of a paragraph before a tab
	repeat while body text contains (lb1 & space & tab)
		set body text to my remplace(body text, lb1 & space & tab, lb1 & tab)
	end repeat
	# remove every space char which is at beginning of a paragraph just after a tab
	repeat while body text contains (lb1 & tab & space)
		set body text to my remplace(body text, lb1 & tab & space, lb1 & tab)
	end repeat
	# remove every group of tab characters which is at beginning of a paragraph by a single tab
	repeat while body text contains (lb1 & tab & tab)
		set body text to my remplace(body text, lb1 & tab & tab, lb1 & tab)
	end repeat
	# replace tab character at beginning of paragraphs by a lb1
	set body text to my remplace(body text, lb1 & tab, lb1 & lb1)
	# get rid of othe tab characters
	if tab2space then
		set body text to my remplace(body text, tab, space)
	else
		set body text to my remplace(body text, tab, "")
	end if
	set character 1 of body text to ""
end tell

#=====
(*
replaces every occurences of d1 by d2 in the text t
*)
on remplace(t, d1, d2)
	local oTIDs, l
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d1}
	set l to text items of t
	set AppleScript's text item delimiters to d2
	set t to "" & l
	set AppleScript's text item delimiters to oTIDs
	return t
end remplace

#=====


Have fun

KOENIG Yvan (VALLAURIS, France) lundi 8 juillet 2013 16:37:03

Yvan! You’re a genius!! (As well as very very kind to me.)

That’s just fantastic, thank you so much for everything. Words fail … :slight_smile:

No more improvements needed.

I may agree with the second proposal but not with the first one.

I’m 70 years old so I’m too old to be genius in an Apple Store and, for the true meaning of genius I’m not matching the requirements.

KOENIG Yvan (VALLAURIS, France) mardi 9 juillet 2013 09:50:50

Well then, let’s just say you’re a fine citizen at 70.