Reading file created in Treminal


When the top half of the code below is run in isolation, a file is created containing the uptime for the computer. When the second half is added, however, the file is not created - with anIOExeption as the result. The same result is obtained when the two halves of the code are separated into two methods.

Can anybody tell me how to read the Uptime file?

Dim a as New AppleScriptMBS
Dim lines(-1) as String

lines.Append “On run”
lines.Append "do shell script ““uptime > /Users/Shared/Uptime.txt”” "
lines.Append “End run”

a.Compile Join(lines,EndOfLine.Macintosh)

Dim f As FolderItem
f = Volume(0).Child(“Users”).Child(“Shared”).Child(“Uptime.txt”)

Dim SourceStream As TextInputStream
Dim uptimeString As String

SourceStream = TextInputStream.Open(f) // Creates IOException (file not found)
uptimeString = SourceStream.ReadLine

MsgBox “uptimeString”

Any help will be greatly appreciated.


Why are you using AppleScript to execute the first command and not in a regular shell?
I would do this instead:
Works perfectly fine here :slight_smile:

  Dim filePath as FolderItem = Volume(0).Child("Users").Child("Shared").Child("Uptime.txt")
  Dim SourceStream As TextInputStream
  Dim uptimeString As String
  Dim sh As Shell
  If filePath <> Nil Then
    sh = New Shell
    sh.Execute("uptime > " + filePath.ShellPath)
    If sh.ErrorCode = 0 Then // Success.
      SourceStream = TextInputStream.Open(filePath)
      uptimeString = SourceStream.ReadLine
      MsgBox uptimeString // Removed the " as that would only show that perticular string, not the variable.
      //command failed.
    end if
    //filePath error.
  end if

You can get the uptime of your computer without calling “uptime” too.

Dim minutes As Integer
MsgBox "Your computer has been on for "+Str(minutes)+" minutes."

Hi Albin,

Thanks very much for your help. I’ll certainly be using your last solution.