Make AS see "-" as a character

Hello, I am making an AppleScript to convert a matrix of decimals (in string form) into something more usable in LaTeX.
In order to do so, I need to get a line of code like this:

words of "hello, world, my number is -56.4, not +42.0"
--{"hello", "world", "my", "number", "is", "56.4", "not", "+", "42.0"}

To actually return

--{"hello", "world", "my", "number", "is", "-56.4", "not", "+42.0"}
--Or at least:
--{"hello", "world", "my", "number", "is", "-", "56.4", "not", "+", "42.0"}

I think it is weird that it sees the plus but not minus.

Thanks in advance!

At least in some languages (including French and English), the character - is a word separator (+ isn’t one) so, as long as you will try to split your datas into words ” if your language behaves this way (and clearly it does) ” you will not see it.

A correct scheme would be to use text item delimiters.

set source to "hello, world, my number is -56.4, not +42.0"

log my decoupe(source, {space, ","})
(*hello, , world, , my, number, is, -56.4, , not, +42.0*)

set theItems to my decoupe(source, {", ", space, ","})
--> {"hello", "world", "my", "number", "is", "-56.4", "not", "+42.0"}

#=====

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

#=====

I defined three possible delimiters. The first one ", " is here to avoid the empty strings returned if we forget it.

Yvan KOENIG (VALLAURIS, France) mercredi 20 août 2014 18:29:59

Thanks a lot for the reply Yvan, I just realised how awful my example didn’t explain my main problem!
A better example would be

There is not in general commas in fact but I would not like anything attached to a word to be torn off, including $ or any other symbol I might use in the future. I suppose this was all about space deletion and separating the rest into a nice array, words was doing a good job for a while…

Hi,

Cocoa respective AppleScriptObjC can do that very comfortable, because it’s able to work with character sets and to remove empty list elements with one line.

Implement the trimAndCollapseCharactersOfString() handler in a script library



set additionalCharacters to ",$"
set theString to "   textafterrandomspaces        $             morerandomtext,       6.283           -42.0"
theLib's trimAndCollapseCharactersOfString(theString, additionalCharacters)

on trimAndCollapseCharactersOfString(theString, additionalCharacters)
	set cocoaString to current application's NSString's stringWithString:theString
	set mutableCharacterSet to current application's NSMutableCharacterSet's whitespaceAndNewlineCharacterSet()
	mutableCharacterSet's addCharactersInString:additionalCharacters
	set mutableArray to cocoaString's componentsSeparatedByCharactersInSet:(mutableCharacterSet's mutableCopy())
	mutableArray's removeObject:""
	return mutableArray as list
	--> {"textafterrandomspaces", "morerandomtext", "6.283", "-42.0"}

end trimAndCollapseCharactersOfString

Given the late informations, you may try :

set source to "     textafterrandomspaces            $         morerandomtext,       6.283           +12.45   -42.0"

set fakeMinus to "é" # or an other character which is not treated as a word separator
set fakePlus to "è" # or an other character which is not treated as a word separator

# replace the minus characters by a fake character
set temp to my remplace(source, "-", fakeMinus)
# replace the plus characters by a fake character
set temp to my remplace(temp, "+", fakePlus)
# split the datas into words
set theItems to words of temp
# concatenate the list items using space as separator
set temp to my recolle(theItems, space)
# replace the fake minus characters by true ones
set temp to my remplace(temp, fakeMinus, "-")
set temp to my recolle(theItems, space)
# replace the fake plus characters by true ones
set temp to my remplace(temp, fakePlus, "+")
# replace the fake minus characters by true ones
set temp to my remplace(temp, fakeMinus, "+")
# split the temp string using the space as delimiter
set theItems to my decoupe(temp, space)
--> {"textafterrandomspaces", "morerandomtext", "6.283", "+12.45", "-42.0"}

#=====

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

#=====

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
	set AppleScript's text item delimiters to oTIDs
	return t
end recolle

#=====
(*
replace 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
	set AppleScript's text item delimiters to oTIDs
	return t
end remplace

#=====

Yvan KOENIG (VALLAURIS, France) mercredi 20 août 2014 20:52:28

Thank you very much Stefan and Yvan. I was hoping for a solution as elegant as yours Stefan but sadly this script for TeXShop cannot be an applet. Yvan, thank you for writing that, I never expected you to, that is great! :slight_smile:

Here is the finished product:

--Applescript direct

tell application "TeXShop"
	set input to content of selection of front document
	set output to "\\hline\n"
	repeat with para in paragraphs of input
		try
			set newPara to my fixWords(para)
			set output to output & newPara & "\\\\ \\hline\n"
		end try
	end repeat
	display dialog output
	set content of selection of front document to output
end tell

on fixWords(source)
	set fakeMinus to "é"
	set fakePlus to "è"
	set fakeDollar to "Ã¥"
	
	-- replace the minus characters by a fake character
	set temp to my remplace(source, "-", fakeMinus)
	-- replace the plus characters by a fake character
	set temp to my remplace(temp, "+", fakePlus)
	set temp to my remplace(temp, "$", fakeDollar)
	-- split the datas into words
	set theItems to words of temp
	log theItems
	-- concatenate the list items using space as separator
	set temp to my recolle(theItems, " & ")
	-- replace the fake minus characters by true ones
	set temp to my remplace(temp, fakeMinus, "-")
	-- replace the fake plus characters by true ones
	set temp to my remplace(temp, fakePlus, "+")
	-- replace the fake $ characters by true ones
	set temp to my remplace(temp, fakeDollar, "$")
	log temp
	return temp
end fixWords

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

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
	set AppleScript's text item delimiters to oTIDs
	return t
end recolle

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
	set AppleScript's text item delimiters to oTIDs
	return t
end remplace

It seems that you didn’t understood correctly StefanK’s suggestion.

Here I applied it this way

on trimAndCollapseCharactersOfString(theString, additionalCharacters)
   set cocoaString to current application's NSString's stringWithString:theString
   set mutableCharacterSet to current application's NSMutableCharacterSet's whitespaceAndNewlineCharacterSet()
   mutableCharacterSet's addCharactersInString:additionalCharacters
   set mutableArray to cocoaString's componentsSeparatedByCharactersInSet:(mutableCharacterSet's mutableCopy())
   mutableArray's removeObject:""
   return mutableArray as list
   --> {"textafterrandomspaces", "morerandomtext", "6.283", "-42.0"}

end trimAndCollapseCharactersOfString

I saved that as trimAndCollapseCharactersOfString Lib.scptd (yes, a script package)
I carefully checked the box entitled AppleScript/Bibliothèque Objective-C in the pane opened with the button [ Contenu du paquet ] available at top-right of the script window.
This done, I moved the file so in the Library folder as :
SSD 500:Library:Script Libraries:trimAndCollapseCharactersOfString Lib.scptd:

Then I created this script :

set theLib to script "trimAndCollapseCharactersOfString Lib"

set additionalCharacters to ",$"
set theString to "   textafterrandomspaces        $             morerandomtext,       6.283           -42.0"
theLib's trimAndCollapseCharactersOfString(theString, additionalCharacters)

It may be used directly from the Editor.
I didn’t tried to embed it in your full script.
I just checked that running it I correctly got :
{“textafterrandomspaces”, “morerandomtext”, “6.283”, “-42.0”}

Yvan KOENIG (VALLAURIS, France) jeudi 21 août 2014 16:20:48

Thanks, I should have been clearer though, I have to be able to copy and paste this thing into the TeXShop macro editor, it can’t be a package or anything.

The library is installed only once.
The script itself ” which will be used in the macro editor ” contain only the four instructions which I posted in the second script.

Your script uses display dialog which is embedded in the Osax Standard Additions.
I don’t see a logical difference between a library and an Osax, except perhaps that Osax are items of the past.
I don’t own TexShop macro editor but I would be surprised to read that it’s unable to execute the proposed script.

--Applescript direct
property theLib : missing value

set theLib to script "trimAndCollapseCharactersOfString Lib"
tell application "TeXShop"
	set input to content of selection of front document
	set output to "\\hline
"
	repeat with para in paragraphs of input
		try
			set newPara to my fixWords(para)
			set output to output & newPara & "\\\\ \\hline
"
		end try
	end repeat
	display dialog output
	set content of selection of front document to output
end tell
set theLib to missing value

on fixWords(theString)
	set additionalCharacters to ",$"
	set theString to " textafterrandomspaces $ morerandomtext, 6.283 -42.0"
	theLib's trimAndCollapseCharactersOfString(theString, additionalCharacters)
	return result
end fixWords

But of course, all what I may do is to pass you some information.
You are free to refuse to test it.

Yvan KOENIG (VALLAURIS, France) jeudi 21 août 2014 18:48:13

Thank you Yvan! That’s perfect!

Thanks for the feedback.
I hope that this exemple will help users to understand that libraries ” as implemented by Mavericks ” aren’t complicated items. I am unable to design them but it’s the same for Osaxen and quite every kind of applications which I use daily.
This is why from time to time, I answer questions with old fashioned scripts which I am able to write hoping that more expert guys will deliver more efficient answers using the newly delivered tools.
Most of the time, this kind of answer is delivered by Shane Stanley.
I was really glad to see one written by StefanK which already helped some of us with fine command line tools.
The fact that Shane’s works aren’t embedded by Apple in the delivered system is just . a shame.

Yvan KOENIG (VALLAURIS, France) vendredi 22 août 2014 17:03:41