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. 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
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:
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
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.
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
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
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)
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.
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.
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?
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)