Displaying contents of log thru array in tableview/tablecolumn

Hello all. Thanks in advance for your assistance. Please assume I am new to both Applescript and ApplescriptObjc/Xcode, and go easy on me :). I am attempting to build an ApplescriptObjc application in Xcode that acts on some system files (whose actions add contents to a log file), and shows the contents of that log file in a tableview below the action buttons. TLDR; I am attempting to build an application for something normally only done manually through Terminal.

The context of my Applescript appdelegate is below. When the appREF button is clicked, the log file is accessed and read, and its contents are attempted to be passed to the array controller. With the script below, I am able to gather the contents of the log file and assign it as the txt variable. For troubleshooting and testing, I entered a couple of display dialogs to try to understand what is happening with each variable, and the display dialogs show the contents of the log file, which each line showing. When the log file is passed to the array controller and ultimately the table view, the first line of the log file is the only thing shown in the table. However using the display dialog after the contents have been sent to the array controller, the log’s full (what fits in the display window) shows again.

How can I get the full contents of the log to show, instead of just the first line, given my code below? Thanks again for any insight. I have removed the code for the other application functional items for simplicity.


script com_loyaltyarm_AppDelegate
property parent : class "NSObject"
    
property appLog : {}
    
    on appREF_(sender)
        set unixpath to "/Library/app/dir/place/loc1/log" as string
        log 1
        tell application "Finder"
                set foo to (open for access (unixpath))
                set txt to (read foo for (get eof foo))
                close access foo
            display dialog txt
        end tell
        set my appLog to txt as list
        display dialog appLog
    end appREF_
	
        on applicationWillFinishLaunching_(aNotification)
		-- Insert code here to initialize your application before any files are opened
	end applicationWillFinishLaunching_
	
	on applicationShouldTerminate_(sender)
		-- Insert code here to do any housekeeping before your application quits 
		return current application's NSTerminateNow
	end applicationShouldTerminate_
	
end script

Hi,

you probably want to separate the text by the line endings.
[text] as list works only if text item delimiters are set to return or linefeed (depending on the used separator).

It’s more reliable to use paragraphs


set unixpath to "/Library/app/dir/place/loc1/log"
log 1
set txt to (read unixpath)
display dialog txt
set my appLog to paragraphs of txt
display dialog appLog

the read command without the for parameter reads the whole text and open for access (and the Finder) is not needed.
Although AppleScript works only with HFS paths (colon separated) the read command accepts also a POSIX path.

Nevertheless I recommend to use the Cocoa method stringWithContentsOfFile_encoding_error() of NSString

Stefan,

Thanks for your speedy and effective reply! I updated the code as you suggested and the log file contents display as expected. I would assume that the error I get in the debug console about ‘The operation couldn’t be completed’ speaks to the improved reliability of the Cocoa method you described (several searches on OSStatus error -1750 yield situations similar to mine, writing of large log files all at once to a file, error occurs over 100 lines). This error occurs while clicking the appREF button.

(Excuse my terminologies…) If I were to implement the Cocoa method, I would call the NSString class in a property declaration, as well as define the tableview as a property. (In typing that I’m wondering if I even need to do both.) Then reference the method you speak of as property’s stringWithContentsOfFile_encoding_error() where the () will contain the filepath?

Thanks again in advance. The error isn’t a show stopper, but in learning I’d rather get used to doing things best practice the first time and appreciate the guidance.

-Nick

the ASOC equivalent is


set unixpath to "/Library/app/dir/place/loc1/log"
set UTF8StringEncoding to current application's NSUTF8StringEncoding
set txt to current application's NSString's stringWithContentsOfFile_encoding_error_(unixpath, UTF8StringEncoding, missing value)
set newlineCharacterSet to current application's NSCharacterSet's newlineCharacterSet()
set textParagraphs to txt's componentsSeparatedByCharactersInSet_(newlineCharacterSet) -- as list (maybe)
log textParagraphs
set my appLog to textParagraphs

Thanks Stefan. I’m noticing a difference in displayed using the two codes. The first obvious difference, is the existence of the error with the first sampling, and non-existence of error with the second. The major difference being that using the first code sample, I am able to get the log file contents per line, with no extra spaces. The second code sample (ASOC) puts the same log file contents into the tableview, but with an extra line skipped in between each line.

For the first code example:
Date: Time - some log text
Date: Time - some log text
Date: Time - some log text

For the second (ASOC):
Date: Time - some log text

Date: Time - some log text

Date: Time - some log text

I’m thinking that the way the (newlineCharacterSet) reads each line return is at fault, as I’ve replaced it with various other tasks of NSCharacterSet per your example with negative results (spacing is worse). Any thoughts on whether or not this is fixable?

is this a standard UTF-8 encoded log file?

Maybe the paragraph separator of AppleScript considers more character combinations as a single line break.
You can also try


 . componentsSeparatedByString_("\n") -- or \r or other combinations (there are some Unicode characters)

I’d recommend to look into the log file with an hex editor to figure out the proper line break separator

It does – it looks like it uses getLineStart:end:contentsEnd:forRange:, which unfortunately can’t be used from ASObjC.

I used Hex Fiend to examine the log file and it appears to be MacRoman encoding.

This fixed it. The log file now appears without any line spacing as I indicated in my last post.

Thanks a ton for your help!

Just wanted to post a link to the ‘final’ project…

https://github.com/loyaltyarm/mac/tree/master/Syncafee