Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Read values from file



On Sunday, August 31, 2003, at 12:00 AM, Rhon Fitzwater wrote:

Hi everyone,

I need to know how I can read from the file below(contents are below) when my program launches and set some the text fields to the values below. So text field one would be set to "string one", text field two would be set to "string two", etc. etc. I know how to set the values, but I do not know how to read from the file below and just extract the values I need.

String Manipulation ISN'T Applescript's Strong Point. It's fairly awkward in AS., I think. (Or just verbose, sometimes the same thing.)

My gut feeling is to tell you to do the actual work in another scripting language - Perl, Python, even (gasp) awk/sed/grep. Then spit the values back to Applescript maybe like so:

timer, 1, 1.0, 0.0, 0.501960784314
ws1, string one, 0.0, 1.0, 0.0

Doing it like this you can run the result of your scripts through Applescript's Text Item Delimiters and get a list, simply and easily get the values. (Hint: set applescript's text item delimiters to {","} )

Of course, that's me. I tend to call other scripting languages to do the heavy lifting fairly often (be it in Cocoa, or AppleScriptStudio).

BUT I will try to give you a hint in AppleScript.

(begin untested, uncompiled code)

set fileRef to open for access myFile with read permissions
set everyLine to read fileRef with delimiters {";"}
close access fileRef

repeat with each in everyLine
set meaning to offset of "=" in each

set value to characters (meaning) thru (count of each) of each
-- "1.0", in the case of the first R value
--it's left as an exercise to the reader to try and figure out how to parse the first line of every
--set (timer = {).

-- and that's the gist of the parsing bit...
end repeat

(end untested, uncompiled code)

I guess it really isn't *that* bad, it just seems you have a problem that *might* be more easily solved with even regular expressions (didn't we used to have an OSAX that did that under 9? where did that go to...) - or just a language that isn't quite as verbose as Applescript.

Anyway, I hope that was *some* help to you Rhon.

Later,
_Ryan Wilcox


Does anyone have a simple example of how I can do this? Please post or email me if you do.

Thanks,

-Rhon
<file contents>
{
timer = {
value = "1";
R = 1.0;
G = 0.0;
B = 0.501960784314;


[snip]
_______________________________________________
applescript-studio mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/applescript-studio
Do not post admin requests to the list. They will be ignored.

References: 
 >Read values from file (From: Rhon Fitzwater <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.