Re: Coercion from string to integer fails
Re: Coercion from string to integer fails
- Subject: Re: Coercion from string to integer fails
- From: "Gary (Lists)" <email@hidden>
- Date: Thu, 31 May 2007 18:49:54 -0400
"Dan Doughtie" wrote:
> I have a script that reads in a preference from a text file.
>
> The preference is basically the number of seconds since midnight.
>
> I read the number from the file as text which makes it string.
>
> I try to force it to an integer so I can do some math.
>
> No matter what I do it fails and says it can¹t make ³49900² into type
> integer. (49000 being the text from the file).
>
> This set statement falls within a Tell Finder.
>
> This is the part of the script that fails
>
> open for access file MyPrefsFile
> set myFolderToWatch to read file MyPrefsFile before " "
> set MyPrefsFolder to myFolderToWatch
> set myCharCount to count of MyPrefsFolder
> set MyPrefsInfo to read file MyPrefsFile from myCharCount + 2 to eof
> set myTimeToRun to MyPrefsInfo
> close access file MyPrefsFile
>
> set myTimeToRun to myTimeToRun as integer
> Anyone know of a smoking gun?
Dan, my test with your script worked just fine.
-- ==> OP's version [works just fine OMM]
set MyPrefsFile to "Sangha:Users:me:Desktop:prefs.txt"
open for access file MyPrefsFile
set myFolderToWatch to read file MyPrefsFile before " "
set MyPrefsFolder to myFolderToWatch
set myCharCount to count of MyPrefsFolder
set myCharCount to length of MyPrefsFolder
set MyPrefsInfo to read file MyPrefsFile from myCharCount + 2 to eof
-- class of MyPrefsInfo
--> string
set myTimeToRun to MyPrefsInfo
close access file MyPrefsFile
set myTimeToRun to myTimeToRun as integer
--> 49990
That works OMM. (Panther, not Tiger.)
Incidentally, I tried with a text string, a Unicode text string, and even
with leading spaces (and trailing spaces) in the digit-string...all worked.
Not that this solves your 'as integer' problem, but here's an alternate
version of that script. You don't need to 'open for access' just to read;
you don't need to read the file twice; you don't need to close for access;
and you probably want to catch any coercion (to integer) errors.
Without comments, this is just a wee bit shorter, too.
-- === My Version
-- === Uses the included utility handler for splitting strings [see below].
--
set MyPrefsFile to "Sangha:Users:me:Desktop:prefs.txt"
set _prefData to read file MyPrefsFile
--> "somestring_for_foldername 49990"
set {MyPrefsFolder, MySecondsInfo} to split(_prefData, space)
--> {"somestring_for_foldername", "49990"}
try
set MySecondsInfo to (MySecondsInfo as integer)
on error
set MySecondsInfo to 1 -- or, whatever...exit, alert, quit, etc.
end try
{MyPrefsFolder, MySecondsInfo}
--> {"somestring_for_foldername", 49990}
-- Utility Handler
-- Ex's:
-- split("jack&jill", "&")
--> {"jack", "jill"}
-- split("jack & jill", " & ")
--> {"jack", "jill"}
-- split("jack & jill", (space & "&" & space))
--> {"jack", "jill"}
--
to split(thisStr, thisChar)
set oTIDs to the text item delimiters
set the text item delimiters to thisChar
set allParts to text items of thisStr
set the text item delimiters to oTIDs
return allParts
end split
Another approach, Dan, might be to simply create a double-clicable script
application, which, when 'double-clicked' would ask for a folder name and a
time interval (seconds after midnight, or other clock time) and then save
those as script properties. Then, the script need not read any text file at
all. (Where does the text file come from, anyway? Is it created by script?
Is it just typed and saved by a human?)
--
Gary
_______________________________________________
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