Select specific parts from text

Hello guys,

I would like to create a script where it gets the contents from clipboard, selects only specific parts of it and return only them in clipboard in a slightly different layout.

For example the source (contents in clipboard) are having the following format:

Name
Abc
Surname
Xyz
Email
abc@abc.com
Date of Birth
01/01/1990
Hiring Date
31/12/2018
Job Role
Accountant
Marital Status
Married
Telephone number
0123456789
Address of Residence
* London, M11 1N1
* United Kingdom
Salary
40,000

Is it feasible to simplify this text and get only specific contents and copy them to the clipboard in the following format?

Full Name: Abc Xyz
Email: abc@abc.com
Phone: 0123456789
Hire: 31/12/2018
Address: London, M11 1N1 / United Kingdom

Thank you guys in advance!

Something like this:


set aSource to "Name
Abc
Surname
Xyz
Email
abc@abc.com
Date of Birth
01/01/1990
Hiring Date
31/12/2018
Job Role
Accountant
Marital Status
Married
Telephone number
0123456789
Address of Residence
        *         London, M11 1N1
        *         United Kingdom
Salary
40,000"

set aText to ""
tell aSource
	set aText to aText & "Full Name: " & paragraph 2 & space & paragraph 4 & return
	set aText to aText & "Email: " & paragraph 6 & return
	set aText to aText & "Phone: " & paragraph 16 & return
	set aText to aText & "Hire: " & paragraph 10 & return
	set aText to aText & "Address: " & (text items 19 thru -1 of paragraph 18)
	set aText to aText & " / " & (text items 19 thru -1 of paragraph 19)
end tell

NOTE: do not copy, but use Open this Scriplet in your Editor, to get my code correctly

Hey man,

thank you so much!

It works, however sometimes the lines are not always static, having other fields between them or reverted order (ie first the surname and then the name) or sometimes the email address is on top.

So unfortunately I have to look for the keywords I assume…


set aSource to "Name
Abc
Surname
Xyz
Email
abc@abc.com
Date of Birth
01/01/1990
Hiring Date
31/12/2018
Job Role
Accountant
Marital Status
Married
Telephone number
0123456789
Address of Residence
        *         London, M11 1N1
        *         United Kingdom
Salary
40,000"

set aText to "Full Name: "
tell aSource
	tell (paragraphs)
		set aCount to count it
		repeat with i from 1 to aCount
			if item i is "Name" then
				set nameIndex to i
				exit repeat
			end if
		end repeat
		repeat with i from 1 to aCount
			if item i is "Surname" then
				set surnameIndex to i
				exit repeat
			end if
		end repeat
		repeat with i from 1 to aCount
			if item i is "Email" then
				set emailIndex to i
				exit repeat
			end if
		end repeat
		repeat with i from 1 to aCount
			if item i is "Telephone number" then
				set phoneIndex to i
				exit repeat
			end if
		end repeat
		repeat with i from 1 to aCount
			if item i is "Hiring Date" then
				set hireIndex to i
				exit repeat
			end if
		end repeat
		repeat with i from 1 to aCount
			if item i is "Address of Residence" then
				set addressIndex to i
				exit repeat
			end if
		end repeat
	end tell
	set aText to aText & paragraph (nameIndex + 1) & space & paragraph (surnameIndex + 1) & return
	set aText to aText & "Email: " & paragraph (emailIndex + 1) & return
	set aText to aText & "Phone: " & paragraph (phoneIndex + 1) & return
	set aText to aText & "Hire: " & paragraph (hireIndex + 1) & return
	set aText to aText & "Address: " & (text items 19 thru -1 of paragraph (addressIndex + 1)) & " / "
	set aText to aText & (text items 19 thru -1 of paragraph (addressIndex + 2))
end tell

Thank you sir for your help, but I receive this error:

Can’t make text items 19 thru -1 of paragraph 18 of “aSource” into type Unicode text.

As I sad above,
NOTE: do not copy, but use Open this Scriplet in your Editor, to get the code correctly

Are you sure that you ran exactly what was posted by KniazidisR ?

I’m quite sure that the answer is no.
I got the described error with aSource defined as :

set aSource to "Name
Abc
Surname
Xyz
Email
abc@abc.com
Date of Birth
01/01/1990
Hiring Date
31/12/2018
Job Role
Accountant
Marital Status
Married
Telephone number
0123456789
Address of Residence"

So, to take care of the fact that the given data may fail to match the original description I edited the script as :


set aSource to "Name
Abc
Surname
Xyz
Email
abc@abc.com
Date of Birth
01/01/1990
Hiring Date
31/12/2018
Job Role
Accountant
Marital Status
Married
Telephone number
0123456789
Address of Residence
* London, M11 1N1
* United Kingdom
Salary
40,000"
(*
set aSource to "Name
Abc
Surname
Xyz
Email
abc@abc.com
Date of Birth
01/01/1990
Hiring Date
31/12/2018
Job Role
Accountant
Marital Status
Married
Telephone number
0123456789
Address of Residence
        *         London, M11 1N1
        *         United Kingdom
Salary
40,000"
*)
set aText to "Full Name: "
tell aSource
	tell (paragraphs)
		set aCount to count it
		repeat with i from 1 to aCount
			if item i is "Name" then
				set nameIndex to i
				exit repeat
			end if
			-- if there is no item "Name" nameIndex is not defined
		end repeat
		repeat with i from 1 to aCount
			if item i is "Surname" then
				set surnameIndex to i
				exit repeat
			end if
			-- if there is no item "Surname" surnameIndex is not defined
		end repeat
		repeat with i from 1 to aCount
			if item i is "Email" then
				set emailIndex to i
				exit repeat
			end if
			-- if there is no item "Email" emailIndex is not defined
		end repeat
		repeat with i from 1 to aCount
			if item i is "Telephone number" then
				set phoneIndex to i
				exit repeat
			end if
			-- if there is no item "Telephone number" phoneIndex is not defined
		end repeat
		repeat with i from 1 to aCount
			if item i is "Hiring Date" then
				set hireIndex to i
				exit repeat
			end if
			-- if there is no item "Hiring Date" hireIndex is not defined
		end repeat
		repeat with i from 1 to aCount
			if item i is "Address of Residence" then
				set addressIndex to i
				exit repeat
			end if
			-- if there is no item "Address of Residence" addressIndex is not defined
		end repeat
	end tell
	try
		set aText to aText & paragraph (nameIndex + 1) & space & paragraph (surnameIndex + 1) & return
	on error errMsg
		-- tell me to log "Point 1 " & errMsg
		set aText to aText
	end try
	try
		set aText to aText & "Email: " & paragraph (emailIndex + 1) & return
	on error errMsg
		-- tell me to log "Point 2 " & errMsg
		set aText to aText
	end try
	try
		set aText to aText & "Phone: " & paragraph (phoneIndex + 1) & return
	on error errMsg
		-- tell me to log "Point 3 " & errMsg
		set aText to aText
	end try
	try
		set aText to aText & "Hire: " & paragraph (hireIndex + 1) & return
	on error errMsg
		-- tell me to log "Point 4 " & errMsg
		set aText to aText
	end try
	try
		set aText to aText & "Address: " & my strip(paragraph (addressIndex + 1) as string) & " / "
	on error errMsg
		tell me to log "Point 5 " & errMsg
		set aText to aText
	end try
	try
		set aText to aText & my strip(paragraph (addressIndex + 2) as string)
	on error errMsg
		tell me to log "Point 6 " & errMsg
		set aText to aText
	end try
end tell

on strip(aPara)
	set knt to 0
	repeat with aChar in aPara
		set knt to knt + 1
		if aChar is not in {"*", space} then exit repeat
	end repeat
	return text items knt thru -1 of aPara
end strip

With these changes there is no error but the original errors were not due to KniazidisR but to the fact that your datas don’t match your description.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 17 avril 2020 14:16:55

The difference is the spaces around the asterisk (“*”) in the address fields. Copying/'pasting the text of the code copies the spaces of aSource wrongly. As I sad, always is better to use “Open this Scriplet in your Editor” functionality of site. To get code as is, without errors.

Yvan,

To get the same error as the OP, you don’t have to delete anything from the address fields yourself. It is enough to do what the OP does. You say to him, “Do not copy”, no - he copies.

I edited message #7.
With this version, the script does the job even if the headers of the address components don’t match exactly the original format.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 17 avril 2020 17:10:56

Awesome guys, you were all absolutely right!!!

Indeed it was my fault, as you mentioned I didn’t copy correctly the data!

It works perfectly and you saved me so much valuable time!

Thank you so much both for your patience and your valuable help!

#StaySafe