Automatically Create One line from multiple lines of input

I have the following code to take a long list of items, and create a single line of output separated by commas, which are wrapped at the start and end by braces. The code works with two issues that I cannot figure out how to remove.

The first issue, is insertion of double quote marks before and after the new line of output, which I can remove manually, but would like to remove automatically, or not have included in the first instance.

The second issue, is that each text item is wrapped in single quote marks and I would like this to be double quote marks.

I have also noticed that the code changes It’s to It’\''s when run. Anything inside the list of items should not be changed, such as it’s or http:// etc, if possible.

Below the code example is the output it delivers, and an example of what I am trying to achieve.

Can anyone help?



set myString to "
Test1 
Test2 
Test3 
Test4 
Test5 
Test6 
Test7 
"

set myList to paragraphs of myString

set listCount to count myList
repeat with i from 1 to my listCount
	set item i of myList to quoted form of item i of myList
end repeat

set {TID, text item delimiters} to {text item delimiters, ", "}
set newString to ("{" & myList as text) & "}"
set text item delimiters to TID

return newString


Output from the current code above is:

Desired output is to remove the double quote marks at the start and end of the string, and replace the single quote marks around each text item, with double quote marks, as shown below.

There were two extra “return” characters in your data set. One at the very top (the return after the first ") and one at the very bottom (the return just before the last "). I deleted these. Then a little reworking of the code so that you accumulated your items in a string and it works pretty well.


set myString to "Test1
Test2
Test3
Test4
Test5
Test6
Test7"

set myList to paragraphs of myString
set newString to "" as text

set listCount to count myList

repeat with i from 1 to listCount
	if i = 1 then set newString to "{" as text
	if i < listCount then
		set newString to newString & "'" & item i of myList & "', " as text
	else
		set newString to newString & "'" & item i of myList & "'}" as text
	end if
end repeat

display dialog newString

This is a solution similar to yours. Instead of adding single quotes it removes the empty lines in the repeat loop.


set myString to "
Test1 
Test2 
Test3 
Test4 
Test5 
Test6 
Test7 
"

set myList to paragraphs of myString
set trimmedList to {}
repeat with anItem in myList
	if (count anItem) > 0 then set end of trimmedList to contents of anItem
end repeat

set {TID, text item delimiters} to {text item delimiters, "\", \""}
set newString to ("{\"" & trimmedList as text) & "\"}"
set text item delimiters to TID

display dialog newString

Stefan,

I checked the time of each of our solutions running them in a repeat loop of 10,000 times. The result was that the non-TID solution was 51% faster. I’ve always wanted to see if using TID was faster than a simple brute force solution like mine. Thank you for your shorter and more elegant solution. I learn a great deal from you. Thank you.

Corrected version that produces double quoted output.


set myString to "Test1
Test2
Test3
Test4
Test5
Test6
Test7"

set myList to paragraphs of myString
set newString to "" as text

set listCount to count myList

repeat with i from 1 to listCount
	if i = 1 then set newString to "{" as text
	if i < listCount then
		set newString to newString & "\"" & item i of myList & "\", " as text
	else
		set newString to newString & "\"" & item i of myList & "\"}" as text
	end if
end repeat

display dialog newString

@haolesurferdude: Your comparison is a bit unfair because you removed the leading and trailing newline characters. :wink:

If you are interested in speed try this AppleScriptObjC version


use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

use framework "Foundation"

set myString to "
Test1 
Test2 
Test3 
Test4 
Test5 
Test6 
Test7 
"

set cocoaString to current application's NSString's stringWithString:myString
set trimmedString to cocoaString's stringByTrimmingCharactersInSet:(current application's NSCharacterSet's whitespaceAndNewlineCharacterSet)
set newString to ("{\"" & (trimmedString's stringByReplacingOccurrencesOfString:linefeed withString:"\", \"") as text) & "\"}"

display dialog newString

The script assumes linefeed as line separator. If the line separator could change this line considers return and linefeed


set newString to ("{\"" & (trimmedString's stringByReplacingOccurrencesOfString:"[\\n\\r]" withString:"\", \"" options:(current application's NSRegularExpressionSearch) range:{location:0, |length|:trimmedString's |length|()}) as text) & "\"}"

@ haolesurferdude

I brought some changes to your proposal :


set myString to "Test1
Test2
Test3
Test4
Test5
Test6
Test7"

set myList to paragraphs of myString
set newString to "{"
repeat with anItem in myList
	set newString to newString & "\"" & anItem & "\", "
end repeat
set newString to (text 1 thru -3 of newString) & "}"

display dialog newString

Yvan KOENIG running Sierra 10.12.6 in French (VALLAURIS, France) jeudi 3 aout 2017 17:51:11

Other point :
in message #1, every string ended with a space character.
in messages #2 and #4 the space characters are gone.
What is the real source and what is the real result wanted ?

I’m wondering if it would be useful to code :

set myString to "
Test1 
Test2 
Test3 
Test4 
Test5 
Test6 
Test7 
"
set myList to my decoupe(myString, {space & return, space & linefeed, return, linefeed})
if item 1 of myList is "" then set myList to rest of myList
if item -1 of myList is "" then set myList to items 1 thru -2 of myList
set newString to "{"
repeat with anItem in myList
	set newString to newString & "\"" & anItem & "\", "
end repeat
set newString to (text 1 thru -3 of newString) & "}"

display dialog newString

#=====

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

#=====

Yvan KOENIG running Sierra 10.12.6 in French (VALLAURIS, France) jeudi 3 aout 2017 19:06:28

If the blank lines and spaces are not needed, another fast option is to just hack the error message, as the conversion to quoted items has already occurred.

"
Test1 
Test2 
Test3
Test4 
Test5 
Test6 
Test7 
"'s words

try
	display dialog result --can't work
on error Err
	display dialog Err's text 12 thru -19
end try

I’m not sure that it’s a good idea.
When I run your code in France, I get :
display dialog “de convertir {"Test1", "Test2", "Test3", "Test4", "Test5", "Test6", "Test7”

A cleaner version would be :

"
Test1 
Test2 
Test3
Test4 
Test5 
Test6 
Test7 
"'s words

try
	display dialog result --can't work
on error Err
	set theBundle to ((path to library folder from system domain as text) & "Components:AppleScript.component:Contents:Resources:") as «class furl»
	set theKey to "Can't make %1 into type $2."
	set ErrMsg_loc to localized string theKey from table "Localizable" in bundle theBundle
	# 4 added instructions
	if ErrMsg_loc = theKey then # In 10.9, theKey doesn't end with "$2." but with "%2."
		set theKey to "Can't make %1 into type %2." # used by 10.9
		set ErrMsg_loc to localized string theKey from table "Localizable" in bundle theBundle
	end if
	
	--> "Impossible de convertir %1 en type $2."
	log Err
	set theDelims to items 1 thru 2 of my decoupe(ErrMsg_loc, {"%1", "$2"}) --> {"Impossible de convertir ", " en type "}
	log theDelims
	display dialog item 2 of my decoupe(Err, theDelims)
end try

#=====

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

#=====

But I would not rely upon it because it would fail if one or some of the strings contain space characters.

"
My Test1 
The Test2 
Test 3
Test4 
Test5 
Test6 
Test7 
"'s words
#…

would issue : display dialog “{"My", "Test1", "The", "Test2", "Test", "3", "Test4", "Test5", "Test6", "Test7"}” which is far from what is wanted.

Yvan KOENIG running Sierra 10.12.6 in French (VALLAURIS, France) jeudi 3 aout 2017 21:33:06

Yes, it probably isn’t—at least from a bulletproof use perspective—but I did note that it was a hack for speed. :wink: It’s not particularly important to me, but, rather than a list, your improvement only returns the lone word “string.” on my US English system.

Puzzling, when I urge my iMac to run in English, I get this history :

tell application "Script Editor"
	display dialog {"Test1", "Test2", "Test3", "Test4", "Test5", "Test6", "Test7"}
		--> error number -1700 from {"Test1", "Test2", "Test3", "Test4", "Test5", "Test6", "Test7"} to string
end tell
tell current application
	path to library folder from system domain as text
		--> "SSD 500:System:Library:"
	localized string "Can't make %1 into type $2." from table "Localizable" in bundle file "SSD 500:System:Library:Components:AppleScript.component:Contents:Resources:"
		--> "Can’t make %1 into type $2."
	(*Can’t make {"Test1", "Test2", "Test3", "Test4", "Test5", "Test6", "Test7"} into type string.*)
	(*Can’t make ,  into type *)
end tell
tell application "Script Editor"
	display dialog "{\"Test1\", \"Test2\", \"Test3\", \"Test4\", \"Test5\", \"Test6\", \"Test7\"}"
		--> {button returned:"OK"}
end tell

Yes, I added two log instructions to show the components at work.

May you post the history issued on your side ?
No need to hurry, it’s late, I will be back tomorrow.

Yvan KOENIG running Sierra 10.12.6 in French (VALLAURIS, France) Thursday, 3 August 2017 22:26:09

I’m so glad all of you found this as interesting as I did.

Thank you everyone!

My log is similar to yours—the HD name being the only difference through the localized string portion. It diverges at the point of your logged entry:

[format]tell application “AppleScript Editor”
display dialog “string.”
→ {button returned:“OK”}
end tell
Result:
{button returned:“OK”}[/format]

The code on my machine appears to be returning your first logged line, specifically text -7 thru -1. Perhaps it’s a version difference? I’m using Mavericks.

Got it. In Mavericks, the key pointing to the message used is different than the one in recent systems.

This old key is “Can’t make %1 into type %2.” (which doesn’t match the “official” rules)
Now it’s “Can’t make %1 into type $2.” (which matches the “official” rules)

When you execute the script, as it asked for the new key, ErrMsg_loc is set to this new key.
ErrMsg_loc is theKey which is “Can’t make %1 into type $2.”
theDelims is {"Can’t make ", " into type "} with a straight single quote while the error message contain a curly one.
So, when the script try to split the error message, only the second delimiter " into type " apply and item 2 of the resulting list is “string.”

As I have no time to test with 10.10 and 10.11, I use an alternate scheme to treat the problem.
The script try to get the localized version of the new key.
If the localized string is different from the key, we are on a “modern” system and all is OK
If the localized string is equal to the key, we are on an “old” system and we make a new attempt extracting the localized version of the old key.
I tested it, it works.
You will find the edited script in message #9

Yvan KOENIG running Sierra 10.12.6 in French (VALLAURIS, France) vendredi 4 aout 2017 10:48:19

Hi.

Here’s another entry. It very simply and quickly returns exactly what the OP asked for (with the trailing spaces in the first six “Test” lines preserved), though I’m not sure it’s what was actually wanted. :confused:

set myString to "
Test1 
Test2 
Test3
Test4 
Test5 
Test6 
Test7 
"

set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "\", \""
set newString to "{\"" & (myString's paragraphs from word 1 to word -1) & "\"}"
set AppleScript's text item delimiters to astid

display dialog newString

Here’s a variation on your code – if you squint hard, you could almost call it a one-liner :wink:

use framework "Foundation"
use framework "OSAKit"
use scripting additions

set myString to "
Test1 
Test2 
Test3
Test4 
Test5 
Test6 
Test7 
"

tell (current application's OSAScript's alloc()'s initWithSource:("paragraphs from word 1 to word -1 of \"" & myString & "\"")) to set {theString, attString} to its executeAndReturnDisplayValue:(reference) |error|:(missing value)
display dialog attString's |string|() as text

Crazy…

:cool:

:slight_smile:

use framework "Foundation"
use framework "OSAKit"
use scripting additions

set myString to "
Test1 
Test2 
Test3
Test4 
Test5 
Test6 
Test7 
"

display dialog (((current application's OSAScript's alloc()'s initWithSource:("paragraphs from word 1 to word -1 of \"" & myString & "\""))'s executeAndReturnDisplayValue:(reference) |error|:(missing value))'s end's |string|() as text)

:cool: :cool: :cool: :cool:

WOW - thanks everyone for an interesting thread.

I logged back in to see if anyone had posted, as I didn’t get any notifications, and now feel bad that so much has happened, and I was oblivious to it all.

I will run through some of the examples and report back findings.

Thanks again to you all.

The code from Nigel works really well. Thanks.

Now to spice things up! Would it be possible to create an application using the code below, which when run, reads the strings of data to be organised from an Excel spreadsheet, instead of having to add them into the code and then run it?

This would save even more time, and provide for greater flexibility and accuracy.