No responses So I tried it partially to solve it myself…
So I’ll try to explain.
Create new project with the checkbox for Document Based Application checked
Goto the targets group in your project and double click DocBaseApplication
goto the properties tab
Because we are only reading unicode Text-files we will change the document types to extensions txt and OS Types to TEXT
Now fill in the code in the MyDocument.applescript…
script MyDocument
property parent : class "NSDocument"
property uiTextView : missing value --< Bound in IB from File's owner to a NSTextView in myDocuments.xib
property fileContents : "" --< document default text (empty)
on init()
continue init()
-- Add your subclass-specific initialization here.
-- If an error occurs here, return missing value.
return me
end init
on windowNibName()
-- Override returning the nib file name of the document
-- If you need to use a subclass of NSWindowController or if your document supports multiple NSWindowControllers, you should remove this method and override -makeWindowControllers instead.
return "MyDocument"
end windowNibName
on windowControllerDidLoadNib_(aController)
continue windowControllerDidLoadNib_(aController)
-- Add any code here that needs to be executed once the windowController has loaded the document's window.
--> Here we assign the text we read from the file to the TextView or in case of an empty document the default string
if fileContents is not "" then uiTextView's setString_(fileContents)
end windowControllerDidLoadNib_
on dataOfType_error_(typeName, outError)
-- Insert code here to write your document to data of the specified type. If the given outError is not missing value, ensure that you set contents of outError when returning missing value.
-- You can also choose to override -fileWrapperOfType:error:, -writeToURL:ofType:error:, or -writeToURL:ofType:forSaveOperation:originalContentsURL:error: instead.
--> Here we are creating an NSData obkect from the TextView's string
try
set fileData to uiTextView's |string|'s dataUsingEncoding_(current application's NSUnicodeStringEncoding)
set outError to missing value --< NO ERROR
end try
if outError is not missing value then
set contents of outError to my NSError's errorWithDomain_code_userInfo_(my NSOSStatusErrorDomain, my unimpErr, missing value)
end if
return fileData --< Here we return de NSData we've created from the TextView's string
end dataOfType_error_
on readFromData_ofType_error_(mNSData, typeName, outError) --< Here we change data to a variable
-- Insert code here to read your document from the given data of the specified type. If the given outError is not missing value, ensure that you set contents of outError when returning false.
-- You can also choose to override -readFromFileWrapper:ofType:error: or -readFromURL:ofType:error: instead.
--> Here we create a NSString from the NSData we've read from the file
try
set fileContents to current application's NSString's alloc()'s initWithData_encoding_(mNSData, current application's NSUnicodeStringEncoding)
set outError to missing value --< NO ERROR
end try
if outError is not missing value then
set contents of outError to my NSError's errorWithDomain_code_userInfo_(my NSOSStatusErrorDomain, my unimpErr, missing value)
end if
return true
end readFromData_ofType_error_
end script
first add a property uiTextView that is bound to a NSTextView in IB on the MyDocuments.xib
add a property fileContents with an empty string
in the handler dataOfType_error_
you add the try block
try
set fileData to uiTextView's |string|'s dataUsingEncoding_(current application's NSUnicodeStringEncoding)
set outError to missing value --< NO ERROR
end try
and change the return to fileData
return fileData --< Here we return de NSData we've created from the TextView's string
in the handler readFromData_ofType_error_
you change the first parameter to mNSData (just the name of a variable)
you add the other try block
try
set fileContents to current application's NSString's alloc()'s initWithData_encoding_(mNSData, current application's NSUnicodeStringEncoding)
set outError to missing value --< NO ERROR
end try
And so you’re ready! Build and run.