Re: tabs to spaces
Re: tabs to spaces
- Subject: Re: tabs to spaces
- From: Scott Haneda <email@hidden>
- Date: Mon, 1 Dec 2008 22:23:25 -0800
Thanks so much, you just summed up basic list manipulation better than
any of the google searches and tutorials I have yet found.
On Dec 1, 2008, at 8:46 PM, Mark J. Reed wrote:
On Mon, Dec 1, 2008 at 7:55 PM, Scott Haneda <email@hidden> >
Something like this:
dasdaads dasdas dasdas das
ds dsd dasd dsad dd ds
set lines to {}
set lines to paragraphs of my_data
You don't need the first "set". Just
set lines to paragraphs of my_data
will create the lines variable.
If I then wanted to iterate though the lines and convert each tab
seperated
chunk to a list as well
repeat with a_line in lines
-- What do i do here?
-- I think this is it, but it is not working well for me
set AppleScript's text item delimiters to tab
set cells to text items of a_line
set text item delimiters to ""
end repeat
OK, you're creating a list of fields for each line, but you're not
storing them anywhere permanent - the "cells" variable is overwritten
each time.
Yeah, I had tried the set foo to foo & something_else
but had what I think we auto coercion issues, where lists were trying
to be converted to strings, and problems were happening. Thanks for
the clarification.
There's no need to set and reset the text item delimiters (TID) each
time through the loop, either.
set oldTID to text item delimiters -- don't assume you know what
they are
I am a little confused about TID's. Why all this setting, resetting,
and restoring? If within the same script, I do not see why you need
to. Is it to be said, a TID that is set, is set that way, and stored
that way even after the script has finished execution?
If that is the case, I get it, if that is the case, where is this TID
"cookie" stored?
set text item delimiters to tab
set rows to {} -- need this one since we're appending later
repeat with a_line in lines
set cells to text items of a_line
set end of rows to cells
end repeat
set text item delimiters to oldTID
rows
What is the rows var that is sitting at the end about? Is this just a
way to debug so I can see output in Script Editor?
Actually, this brings up a nice issue, how do you write print/echo
statements to end up in the output window of Script Editor? Right
now, I use display dialog, but that gets old, clicking OK, in a repeat
loop that is large.
Currently, AS is a bit of a trouble for me, as I find it hard to debug
what is going on, as I can not see what is happening in the code as it
is massaged along.
Maybe I need a better editor, but I am not aware of one.
The "set end of rows" is the key - that appends a new element to the
"rows" list, which is itself a list of fields.
I played with this the other night, but my resetting was stomping on
it, and I was ending up with only the last row. Makes sense. So then
I went with the end of foo style, but in research, read that was
massively slow, compared to working on a "copy" or "reference", at
which point, I thought best to come here and chat it up a bit.
You could also do it
in one swell foop
without the "cells" variable:
set end of rows to text items of a_line
Cool, I will give that a try as well.
From there, is there a way to split() in AS, or suggestion on
simply way to
go back to \r separated lines, with a single space between each
item of the
words in the list.
I think you mean join(), but yes, text item delimiters work both ways.
If you do
Yeah, sorry, slip of the brain.
some_list as text
you get back the elements of the list joined by the current value of
the text item delimiters. So:
set text item delimiters to space
set out_lines to {}
repeat with row in rows
set end of out_lines to row as text
end repeat
set text item delimiters to return
out_lines as text
So I can set TID's to "somestring" and it will use that as the
seperator? Or am I limited to the keywords like tabs, spaces and
paragraphs?
Also, is there way to return the clipboard with a style attribute,
such as
setting it to Monaco?
Yes, but I don't know it. :) I used to, but that mechanism has been
deprecated, and I haven't used the new one. I'm sure someone else can
help you there, though.
Can you give me some function names to start searching on? My current
searches are only yielding results relating to AS dicts within an app,
but not as a standalone text modifier within a scripts return result.
Thanks again for your help.
--
Scott
_______________________________________________
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