Need help for an unknown number of lines

Hi All,

The other day I ask help for a script because I am a real dummy with AppleScripts. I have deleted that post and I try by myself to write one. I apologize because is not well coded.

This script takes the content of the clipboard, do some replacements into the text, then retains only line 1, then 5 and following (line 6 is exist, etc…). But I do not know how to do if line 6 and following do not exist.

Here is a sample of the text set to the clipboard:

La fourmi d’Argentine forme une super colonie en Europe
Proposé par Oresias le 18/10/2019 dans
Animaux
26
Très commune, la fourmi d’Argentine forme sur chaque continent une seule super-colonie, c’est-à-dire que ses zones de nidification sont interconnectées. En Europe, la colonie de fourmis d’Argentine s’étend sur environ 6000 km. Introduite au XIXe siècle, cette espèce invasive extermine les espèces déjà en place et bouleverse l’écosystème.
Les fourmis qui “élèvent” les pucerons sur vos plantes dans les jardins ? Ce sont elles. Les fourmis qui viennent chercher le sucre dans vos placards ? Ce sont certainement elles également. Elles ont un comportement destructeur envers de nombreux fruitiers. Au-delà du bouleversement de l’écosystème, elles sont également accusées de véhiculer des maladies nosocomiales. Néanmoins, elles peuvent aussi être bénéfiques : Elles protègent nos charpentes en luttant efficacement contre la prolifération des termites.

Here is my dirty script

set theText to the clipboard
set new_string to ""
repeat with this_char from 1 to (count theText)
	if item this_char of theText is not "\\" then
		set new_string to new_string & item this_char of theText
	else
		set new_string to new_string & "/"
	end if
end repeat


set AppleScript's text item delimiters to "« "
set theItems to text items of theText
set AppleScript's text item delimiters to "''"
set newText to theItems as text
set AppleScript's text item delimiters to ""

set theText to newText

set AppleScript's text item delimiters to " »"
set theItems to text items of theText
set AppleScript's text item delimiters to "''"
set newText to theItems as text
set AppleScript's text item delimiters to ""

set theText to newText

set AppleScript's text item delimiters to ASCII character 34
set theItems to text items of theText
set AppleScript's text item delimiters to "''"
set newText to theItems as text
set AppleScript's text item delimiters to ""

set theText to newText

set AppleScript's text item delimiters to " ?"
set theItems to text items of theText
set AppleScript's text item delimiters to "?"
set newText to theItems as text
set AppleScript's text item delimiters to ""

set theText to newText

set AppleScript's text item delimiters to " !"
set theItems to text items of theText
set AppleScript's text item delimiters to "!"
set newText to theItems as text
set AppleScript's text item delimiters to ""

set theText to newText

set AppleScript's text item delimiters to " :"
set theItems to text items of theText
set AppleScript's text item delimiters to ":"
set newText to theItems as text
set AppleScript's text item delimiters to ""

set theText to newText

set AppleScript's text item delimiters to " ;"
set theItems to text items of theText
set AppleScript's text item delimiters to ";"
set newText to theItems as text
set AppleScript's text item delimiters to ""

set theText to newText

set AppleScript's text item delimiters to "  "
set theItems to text items of theText
set AppleScript's text item delimiters to " "
set newText to theItems as text
set AppleScript's text item delimiters to ""

set theText to newText

set _data to paragraphs of (newText)
set text item delimiters to return
set _newData to (item 1 of _data as string) & return & return
set _newData5 to (item 5 of _data as string) & return & return
set _newData6 to (item 6 of _data as string)


set the clipboard to _newData & _newData5 & _newData6

Thanks in advance and regards.

Hi, hsp. Try the try. :slight_smile:


set theText to the clipboard as text
set new_string to ""
repeat with this_char from 1 to (count theText)
	if item this_char of theText is not "\\" then
		set new_string to new_string & item this_char of theText
	else
		set new_string to new_string & "/"
	end if
end repeat


set AppleScript's text item delimiters to "« "
set theItems to text items of theText
set AppleScript's text item delimiters to "''"
set newText to theItems as text
set AppleScript's text item delimiters to ""

set theText to newText

set AppleScript's text item delimiters to " »"
set theItems to text items of theText
set AppleScript's text item delimiters to "''"
set newText to theItems as text
set AppleScript's text item delimiters to ""

set theText to newText

set AppleScript's text item delimiters to ASCII character 34
set theItems to text items of theText
set AppleScript's text item delimiters to "''"
set newText to theItems as text
set AppleScript's text item delimiters to ""

set theText to newText

set AppleScript's text item delimiters to " ?"
set theItems to text items of theText
set AppleScript's text item delimiters to "?"
set newText to theItems as text
set AppleScript's text item delimiters to ""

set theText to newText

set AppleScript's text item delimiters to " !"
set theItems to text items of theText
set AppleScript's text item delimiters to "!"
set newText to theItems as text
set AppleScript's text item delimiters to ""

set theText to newText

set AppleScript's text item delimiters to " :"
set theItems to text items of theText
set AppleScript's text item delimiters to ":"
set newText to theItems as text
set AppleScript's text item delimiters to ""

set theText to newText

set AppleScript's text item delimiters to " ;"
set theItems to text items of theText
set AppleScript's text item delimiters to ";"
set newText to theItems as text
set AppleScript's text item delimiters to ""

set theText to newText

set AppleScript's text item delimiters to "  "
set theItems to text items of theText
set AppleScript's text item delimiters to " "
set newText to theItems as text
set AppleScript's text item delimiters to ""

set theText to newText

set _data to paragraphs of (newText)
set text item delimiters to return
set _newData to (item 1 of _data as string) & return & return

set {_newData5, _newData6} to {"", ""}

try
	set _newData5 to (item 5 of _data as string) & return & return
end try

try
	set _newData6 to (item 6 of _data as string)
end try


set the clipboard to _newData & _newData5 & _newData6

Hi KniazidisR,

Forget my message below, I found the solution by moving the & return & return at the beginning of _newData6.

Thanks again

===========

Hi KniazidisR,

Thanks for your quick reply.

It works, the only problem is that if _newData6 does not exists, I have two blank lines at the end of the file. Can this be corrected easily ?

Again thanks for your help.

Regards

You may try :

set theText to the clipboard as text

set theText to my remplace(theText, "\\", "/")
set theText to my remplace(theText, {"« ", " »", quote}, "''")
set theText to my remplace(theText, " ?", "?")
set theText to my remplace(theText, " !", "!")
set theText to my remplace(theText, " :", ":")
set theText to my remplace(theText, " ;", ";")
set theText to my remplace(theText, "  ", " ")

set _data to paragraphs of theText

if (count _data) < 5 then
	set theText to item 1 of _data
else if (count _data) < 6 then
	set theText to my recolle({item 1 of _data, item 5 of _data}, return & return)
else
	set theText to my recolle({item 1 of _data, item 5 of _data, item 6 of _data}, return & return)
end if
set the clipboard to theText

#=====

on recolle(l, d)
	local oTIDs, t
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set t to l as text
	set AppleScript's text item delimiters to oTIDs
	return t
end recolle

#=====
(*
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 as text
	set AppleScript's text item delimiters to oTIDs
	return t
end remplace

#=====

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) samedi 19 octobre 2019 18:35:48

Thanks Yvan,

Works great.

Because I do not know how many lines can follow line 5, did there is a way to automate this by a counter and a variable. This will avoid hard coding _newline5, _newline6, _newline7, etc…

Thanks and regards

If you look again above you will find a entirely reworked code.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) samedi 19 octobre 2019 19:08:18

I just understand that your original data may contain more than 6 paragraphs so I edited again the script.

et theText to the clipboard as text

set theText to my remplace(theText, "\\", "/")
set theText to my remplace(theText, {"« ", " »", quote}, "''")
set theText to my remplace(theText, " ?", "?")
set theText to my remplace(theText, " !", "!")
set theText to my remplace(theText, " :", ":")
set theText to my remplace(theText, " ;", ";")
set theText to my remplace(theText, "  ", " ")
repeat with i from 65 to 90
	set theText to my remplace(theText, ": " & character id i, ": " & character id (i + 32)) # A detail was hurting my poor eyes
end repeat

set _data to paragraphs of theText

if (count _data) < 5 then
	set theText to item 1 of _data
else
	set theText to my recolle({item 1 of _data, (get items 5 thru -1 of _data)}, return & return)
end if
set the clipboard to theText

#=====

on recolle(l, d)
	local oTIDs, t
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set t to l as text
	set AppleScript's text item delimiters to oTIDs
	return t
end recolle

#=====
(*
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 as text
	set AppleScript's text item delimiters to oTIDs
	return t
end remplace

#=====

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) samedi 19 octobre 2019 19:15:24

Edited to drop the variable nbr.

Wow Yvan, thanks a lot, this is perfect and also maybe help me to understand a little bit better how to create a script.

Thanks again and regards
Henri

A detail in your sample data was hurting my poor eyes so I added an instruction in message #8

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) samedi 19 octobre 2019 19:30:32

:wink: Thanks Yvan, I also see this and into my data it was already corrected. But I will keep this in case this case happened again.

Nice evening and regards from Evian-les-Bains

From my point of view, correcting an erroneous E was not sufficient.
I edited the code in message #7 so that it correct every uppercase (non accentuated) inserted after a colon.

I took the opportunity to show you what is the correct syntax to define a character from its Unicode value.
ASCII which you used in your original message is deprecated for years.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) samedi 19 octobre 2019 20:54:46

Thanks Yvan for everything