read and write files

An old compiled app suddenly doesn’t work. No idea why so I attempt to convert from ASS to ASOC.
Opening and reading (writing and closing) text files should be easy but this is C, so it’s next to impossible. The old Finder commands I used don’t seem to work so I attempt to simply open a file and read it as a string. No problem? Wrong!
I already have a sublcass of NSObject I call NSHack. I have successfully used fileExistsAtPath:isDirectory: by passing an NSString to it through ASHack.


- (NSArray *) myFileExistsAtPath:(NSString *)path
{
	NSFileManager *fileManager = [NSFileManager defaultManager];
	BOOL isDir = YES;
	BOOL theResult = [fileManager fileExistsAtPath:path isDirectory:&isDir];
	NSString *bool1;
	NSString *bool2;
	if (theResult == YES)
	{
		bool1 = [NSString stringWithString:@"YES"];
	}
	else 
	{
		bool1 =  [NSString stringWithString:@"NO"];
	}
	if (isDir == YES)
	{
		bool2 =  [NSString stringWithString:@"YES"];
	}
	else 
	{
		bool2 = [NSString stringWithString:@"NO"];
	}
	NSArray *theArray = [NSArray arrayWithObjects:bool1, bool2, nil];
	return theArray;
}


	property ASHack : class "ASHack" of current application

		set theResult to ASHack's myFileExistsAtPath_(thePath)

No problem here at all. However, attempting to pass the same, valid, path to ASHack’s read file always gives an error.

Emptying the routine of all statements except an NSLog still gives the error message.


- (NSString *) myReadFile:(NSString *)path
{
	NSLog(@"myReadFile");
	NSStringEncoding	encoding = NSUTF8StringEncoding;
	NSFileHandle *file = [[NSFileHandle fileHandleForReadingAtPath:path] retain];
	NSData *theData = [file readDataToEndOfFile];
	NSString *dataStr = [[NSString alloc] autorelease];
	dataStr = [dataStr initWithData:theData encoding];
	return dataStr;
}

What is going wrong here?
There must be a better way to access the simple text of a text document “ please help.

That says you’re calling a class method (the + sign), but your method is an instance method. You need to make an instance of the class in IB and connect ASHack to that, or make the method a class method.

But life will probably be a lot easier if you use the normal AS read/write commands – you just need to put them inside a “tell current application” block.

set filePath to "/path/to/file.txt"
set theEncoding to current application's NSUTF8StringEncoding
set theString to current application's class "NSString"'s stringWithContentsOfFile_encoding_error_(filePath, theEncoding, missing value)

Thank’s for the “tell current application” information. With the almost total lack of information on ASOC, where can I read about this? When to use “my”? etc.

Regarding the ASHack class, I see where I went wrong in not using an instance. The name I chose for the variable was too close to the class name and I glossed over it when looking for the error. I needed someone to point it out. Thanks.

Right here :slight_smile:

I suppose I should also mention the sessions in my sig below…