Re: applescript & TextEdit : selecting minus sign (-) for type conversion?
Re: applescript & TextEdit : selecting minus sign (-) for type conversion?
- Subject: Re: applescript & TextEdit : selecting minus sign (-) for type conversion?
- From: Yvan KOENIG <email@hidden>
- Date: Fri, 17 Apr 2009 12:10:08 +0200
Le 17 avr. 2009 à 11:01, Laura Burns a écrit :
Hi everyone , my first post :
I have a text file with a large quantity of real numbers , both
positive and negative
and want to write an applescript to get TextEdit to round them to
integers. (then rewrite a copy file - this i can do)
** but I'm losing the minus sign information **!!
using this code:
----------------------------------------------------------------------
--
tell application "TextEdit"
--file is open already
set the_doc_text to text of the front document
end tell
-- loop through lots of values with integer count W.....
set the_word to word W of the_doc_text
set the_int to the_word as integer
-- and write the_int etc.
----------------------------------------------------------------------
--
if i have as a text file :
37.147 -4.041 -3.4591 10.805 -0.439 -1.733
i get:
37 4 3 10 0 1
----------------------------------------------------------------------
--
the minus sign is not selected by the "word" applescript command-
how can i get those signs back in my output ?
What you get is perfectly normal.
When you extract the words, you drop the sign minus which is one of
the numerous words delimiters.
Try to cut your text using AppleScript's text item delimiters.
CAUTION
In your message there are non-breakable spaces between the numbers.
You will have to strip them to coerce the items as integers.
--[SCRIPT]
-----------------------------------------------------------------
tell application "TextEdit"
--file is open already
set the_doc_text to text of the front document
end tell
set the_paragraphs to paragraphs of the_doc_text
set deci to character 2 of (0.5 as text)
repeat with p from 1 to count of the_paragraphs
set paragraphP to item p of the_paragraphs
set AppleScript's text item delimiters to " "
set theList to text items of paragraphP
set AppleScript's text item delimiters to ""
repeat with W from 1 to count of theList
set the_item to item W of theList
try
set n to ((my remplace(the_item, ".", deci)) as real) as integer
on error
set n to ""
end try
if n is not "" then set item W of theList to n
end repeat
set AppleScript's text item delimiters to " "
set item p of the_paragraphs to theList as text
set AppleScript's text item delimiters to ""
end repeat
set AppleScript's text item delimiters to return
set the_doc_text to the_paragraphs as text
set AppleScript's text item delimiters to ""
the_doc_text
on remplace(t, d1, d2)
local l
set AppleScript's text item delimiters to d1
set l to text items of t
set AppleScript's text item delimiters to d2
set t to l as text
set AppleScript's text item delimiters to ASCII character (202)
set l to text items of t
set AppleScript's text item delimiters to ""
return l as text
end remplace
--[/SCRIPT]
Yvan KOENIG (from FRANCE vendredi 17 avril 2009 12:10:02)
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden