Tuesday, November 19, 2019

#1 2019-10-19 08:24:58 am

hsp
Member
Registered: 2012-07-14
Posts: 19

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

Applescript:

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.

Offline

 

#2 2019-10-19 09:56:13 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 712

Re: Need help for an unknown number of lines

Hi, hsp. Try the try. smile

Applescript:


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


Model: MacBook Pro
macOS Mojave -- version 10.14.4
Safari -- version 12.1
Firefox -- version 70.0

Offline

 

#3 2019-10-19 10:11:00 am

hsp
Member
Registered: 2012-07-14
Posts: 19

Re: Need help for an unknown number of lines

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

Last edited by hsp (2019-10-19 10:34:03 am)

Offline

 

#4 2019-10-19 10:36:19 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3635

Re: Need help for an unknown number of lines

You may try :

Applescript:

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

Last edited by Yvan Koenig (2019-10-19 11:07:19 am)

Offline

 

#5 2019-10-19 11:04:10 am

hsp
Member
Registered: 2012-07-14
Posts: 19

Re: Need help for an unknown number of lines

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

Offline

 

#6 2019-10-19 11:08:28 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3635

Re: Need help for an unknown number of lines

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

Offline

 

#7 2019-10-19 11:15:44 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3635

Re: Need help for an unknown number of lines

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

Applescript:

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.

Last edited by Yvan Koenig (2019-10-19 12:51:59 pm)

Offline

 

#8 2019-10-19 11:21:25 am

hsp
Member
Registered: 2012-07-14
Posts: 19

Re: Need help for an unknown number of lines

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

Offline

 

#9 2019-10-19 11:30:40 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3635

Re: Need help for an unknown number of lines

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

Last edited by Yvan Koenig (2019-10-19 11:31:22 am)

Offline

 

#10 2019-10-19 11:46:27 am

hsp
Member
Registered: 2012-07-14
Posts: 19

Re: Need help for an unknown number of lines

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

Offline

 

#11 2019-10-19 12:57:33 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3635

Re: Need help for an unknown number of lines

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

Offline

 

#12 2019-10-19 04:16:14 pm

hsp
Member
Registered: 2012-07-14
Posts: 19

Re: Need help for an unknown number of lines

Thanks Yvan for everything

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)