Columns in .txt files & decimal places

Hello everyone,
I have a script that uses a series of dialog boxes to set 3 variables. The script then writes them to a text file (log.txt) I use to log the results in the following way:

timestamp: variable1 & " " & variable2 & " " & variable3

Variables 1 and 2 are strings and variable 3 is a number. Due to the varying length of variables 1 and 2 the columns in the text file are out of line, what's the best way of lining up the columns? Also, I would like variable 3 to always have 2 decimal places, and if possible be alligned by the decimal, how do I do that?

iMac G5 w/ iSight, Mac OS X (10.4.7), 20", 1GB RAM, LaCie 160GB FW HD

Posted on Aug 13, 2006 10:01 AM

Reply
8 replies

Aug 13, 2006 12:09 PM in response to Conny

Hi,

Here's a couple of ideas:

1) Set the timestamp to

variable1 & tab & variable2 & tab & variable3 & return

2) Use a fixed-pitch font in the text file (Courier for example).

3) Lining up the decimal points depends on the maximum number of characters you expect there to be before the decimal point.

Untested, assuming your maximum is 3:

set variable3 to variable3 as text
set applescript's text item delimiters to "."
set first_bit to text item 1 of variable3
set last_bit to text 1 thru 2 of text item 2 of variable3
set first bitcount to (count characters in first_bit)
set variable3 to first_bit & "." & last_bit
if first bitcount = 2 then set variable3 to " " & variable3 --one space
if first bitcount = 1 then set variable3 to " " & variable3 -- two spaces
set timestamp to variable1 & tab & variable2 & tab & variable3 & return

Hope some of that helps.

H

Also note that the above doesn't round the decimal places: it simply strips off the excess characters of the number string.

Message was edited by: HD

Aug 13, 2006 12:07 PM in response to Conny

You first need to decide how long you want each variable to be and then pad the variables accordingly.

Assuming the first two variables are strings and the third is a number you can write two handlers, one for strings and one for numbers, like:

to padString(theString, theLength, alignment)
-- First make sure we need to pad at all
-- In this case I return the entire string, but you could truncate it
if length of theString > theLength then return theString
-- now pad the string by appending a space at one end or the other
repeat until length of theString = theLength
if alignment = "right" then
set theString to space & theString
else
set theString to theString & space
end if
end repeat
return theString
end padString


on padNumber(theNum, decimalPlaces)
-- multiply the number up and truncate it to an integer
set theNum to theNum * (10 ^ decimalPlaces) as integer
-- then divide it back down
set theNum to theNum / (10 ^ decimalPlaces)
return theNum as text
end padNumber

set variable1 to (my padString("some string", 20, "left"))
set variable2 to (my padString("some other string", 30, "left"))
set variable3 to (padString(padNumber(123.456, 2), 10, "right"))


Note how this uses the same padString handler with a parameter which tells it which direction to pad in.

Also realize that there are many ways of achieving the same result, including more efficient ones (from a code execution speed point of view) than mine. However, since you're likely to be dealing with tens of passing characters this is probably sufficient.

Aug 13, 2006 1:15 PM in response to Camelot

Wow! that was quick.
Thank you both for your input; my script now works how I want it to.
I do have two more questions though.
Camelot, you mention truncating the string, how do I that?
And... I know it’s probably too complicated for me now, but I'll ask anyway. Is there a way of displaying the number of remaining characters in the dialog box? Variable2 is the longest, it's a description and I've set the length to 30 characters.
Thank you again.

Aug 13, 2006 2:04 PM in response to Conny

>Camelot, you mention truncating the string, how do I that?

You can truncate a string by simply asking AppleScript for a subset of the characters in it. For example:

set someString to "abcdefg"
set truncatedString to text 1 through 5 of someString
--> "abcde"


So, in this case you could say:

if length of theString > theLength then return text 1 through theLength of theString


>And... I know it’s probably too complicated for me now, but I'll ask anyway. Is there a way of displaying the number of remaining characters in the dialog box? Variable2 is the longest, it's a description and I've set the length to 30 characters.

Do you mean if the string is truncated you want to know how much was lost? sure, try this:

to padString(theString, theLength, alignment)
-- If the string is too long truncate it
if length of theString > theLength then
-- work out the bit that's lost
set lostText to text (theLength + 1) through end of theString
-- work out what to keep
set theString to text 1 through theLength of theString
-- and alert the user
display dialog "Input was too long. " & return & length of lostText & " characters (\"" & lostText & "\") were truncated from the end."
else
-- now pad the string by appending a space at one end or the other
repeat until length of theString = theLength
if alignment = "right" then
set theString to space & theString
else
set theString to theString & space
end if
end repeat
end if
return theString
end padString

Aug 13, 2006 2:30 PM in response to Camelot

Again, thank you for your help.
That's perfect now; the script does exactly what I want.
With the remaining characters bit I would like to display the number of characters remaining I could type before the truncation. So if I've typed 16 characters of the 30 character limit it would tell me that I've got 14 characters left before the message gets chopped.
It probably is too advanced for me and I'm more than happy with how the script works now.
Thank you all for your help.
Happy scripting

Aug 13, 2006 8:18 PM in response to Conny

Conny,

If you're using "display dialog" in a standard AppleScript then there's no way I know of to decrement your character counter while the dialog is being displayed. Once the dialog has been displayed there's really no way to interact with it until the user clicks a button to dismiss it.

About the best you could do would be to warn the user of the maximum allowable length... something like:

display dialog "Enter the description (max 30 chars):" default answer "" buttons {"Cancel", "OK"}...

There may be third-party AppleScript plug-ins that allow you to display dialog boxes with some level of interactivity, but I don't know of any to suggest.

On the other hand, if you moved your project to an Xcode/AppleScript Studio application you could do what you're wanting. AppleScript Studio gives you much much richer user interface capabilities.

Steve

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Columns in .txt files & decimal places

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.