Automatically Create One line from multiple lines of input

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.

I’m afraid I can’t help with Excel. But you could possibly help any potential helpers by clarifying what the output should be and what the input could be. Stefan’s second script, mine, and Shane’s all produce exactly the output you requested in your first post from the input given. In this, the trailing space is removed from "Test7 ", but not from the other lines. This is a bit strange and the other scripts are intelligent attempts to guess at what you may actually have meant. It would help to have some clarity on this point. Also, the ‘word’ solutions will lose any non-alphanumeric characters at the beginning of the first line or at the end of the last, so it would be useful to know if the lines could contain any punctuation or spaces which need to be kept.

Select a single column of your data starting with the first row of data thru the last row of data in Excel. Then, run the following AppleScript:


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

tell application "Microsoft Excel"
	activate
	set rowCount to count of rows of selection
	set myString to "" as text
	repeat with j from 1 to rowCount
		set myString to myString & (value of row j of selection) as text
		if j < rowCount then
			set myString to myString & return as text
		end if
	end repeat
end tell

activate
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)

For python there is openpyxl so you can read the spreadsheet file and return the wanted data back to AppleScript without use of spreadsheet software like Microsoft Excel or Numbers.

UPDATE: PM 2017-08-15

This code does as intended, with one small problem when used in the real world with longs strings of text, and more than 45 lines! The text that appears in the output box is so long I can’t copy all of it!

Would it be possible to offer a ‘copy to clipboard’ button, or output the text in another manor that will make copying the final result full proof?

This works perfectly. Thank you.

Thanks for the tip. I am using Microsoft Excel so the script haolesurferdude created works well for me, and saves so much more time.

However, your suggestion is great for anyone not using, or having access to, or even wanting to use Microsoft Excel or Numbers.

Thanks everyone for your input and assistance. :slight_smile:

Can anyone help me with the issue?

The OP showed quite clear input text + quite clear text that he would like to receive as a result. So, here are a bunch of massive solutions, besides this simple one:


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

set myList to paragraphs 2 thru -1 of (text 1 thru -3 of myString)