Formatting output to text file

Hey folks, I’m a php dev who has fumbled in here and needs a little assistance. This is my first post, so please let me know if I’m in the wrong place, or breaking etiquette.

My issue is very simple, I need to track the user, time logged on and time logged off in a text file. This mac is hooked to a machine used for research and we bill based on the amount of time logged on. Once I get the text file formatted I can write the rest in php.

My basic script looks like this

logon:

do shell script "whoami  >> $MacHD/LogApplicationUse/OpenDoc.txt"
do shell script "date  >> $MacHD/LogApplicationUse/OpenDoc.txt"

logoff:

#!/bin/bash
osascript -e 'do shell script "date  >> $MacHD/LogApplicationUse/OpenDoc.txt"'
osascript -e 'do shell script "var=out; echo $var >> $MacHD/LogApplicationUse/OpenDoc.txt"'

This works great for output like this:

user
time in
time out
user
time in
time out

But I’d really like it in this format:

user, time in, time out
user, time in, time out

I started goofing around and came up with this (untested):

Code:

set user to do shell script "whoami"
set log_in to do shell script "date"
set line to user & ", " & log_in ", "
do shell script "line >> $MacHD/LogApplicationUse/OpenDoc.txt"

for my login, which should get me halfway, but even if it works, I’m still stuck with my logout script starting on a new line:

user, time in,
time out
user, time in,
time out

Can someone point me in the right direction here? The >> appends to the file, however it does so with a line break:

will write over the existing data

will append, but it does so with a carriage return

Thanks in advance for any suggestion you all might have, my searches have been less than fruitful.

Are you aiming for something like this?:

set LogFile to (choose file with prompt "Choose the file to edit:") -- insert path to file

(* insert the code or something that 
will check for the file that is opened
or something else *)
set user to do shell script "whoami"
set log_in to do shell script "date"
AppendLogin(user, log_in, LogFile)

(* insert the code or something that 
will check for the file that is closed
or something else *)
set log_out to do shell script "date"
AppendLogout(log_out, LogFile)

(* ===== HANDLERS ===== *)
on AppendLogin(theUserName, timeLoginAsString, fileToEdit)
	-- Data to right format
	set stringToAppend to (return & theUserName & ", " & theLoginAsString) as string
	
	-- Append string to data
	set OA to open for access fileToEdit with write permission
	set originalData to read OA
	set dataToWrite to (originalData & stringToAppend) as string
	write dataToWrite to OA starting at 0
	close access OA
end AppendLogin

on AppendLogout(timeLogoutAsString, fileToEdit)
	-- Data to right format
	set stringToAppend to (", " & timeLogoutAsString) as string
	
	-- Append string to data
	set OA to open for access fileToEdit with write permission
	set originalData to read OA
	set dataToWrite to (originalData & stringToAppend) as string
	write dataToWrite to OA starting at 0
	close access OA
end AppendLogout

Hope it works,
ief2

PS: You can always split the file in two files or something like that.
PPS: You can run scripts thru the terminal or something if you’re doing something in PHP. Maybe you could do a shell script thru PHP. In applescript this would be

do shell script "osascript " & aPOSIXPath

Hi,

what’s about to use AppleScript read/write skills


set {short user name:user} to system info
set log_in to (current date) as text

--in 
writeToDisk from user & ",  " & log_in & ", "

--out
writeToDisk from ((current date) as text) & return

on writeToDisk from theData
	try
		set logFile to (path to startup disk as text) & "LogApplicationUse:OpenDoc.txt"
		set ff to open for access file logFile with write permission
		write theData to ff starting at eof
		close access ff
		return true
	on error
		try
			close access file logFile
		end try
		return false
	end try
end writeToDisk

I didn’t know AS could access text files directly, that’s probably a lot faster than working through a text app when you just need plaintext files. Is there also a dictionary for this where i can see the available terminology and what it does (like access, appending, eof etc.)? Anyway thanks for the example

Thanks fellas, I’m still hammering out the specifics of both of your suggestions, but when I get it finalized, I’ll be sure to post the results.

I don’t think there are special options like appending a string. But it you could make your own “append-handler”, “delete words-handler”, “replace-handler” and so on.

The commands open for access, close access, read, write, get eof and set eof can be found in the File Read/Write-section of the StandardAdditions-dictionary.

There are: the parameter starting at eof of the write command does it :wink:

Ow yeah, forgot that for a minute :P. Despite I wrote it in my answer :rolleyes:.
That’s maybe because I just learned from your earlier post. :slight_smile: