Hi,
I just begin to test AppleScript Studio and Xcode. I checked the example “Plain Text Edior” and after some hours I found how to open and Save txt file in UTF8. But then I create e new Project used “AppleScript Document based application”, and found that the Open Files and Save File is not the same as in the “Plain Text Editor” example.
I have search the forum, but not found something that I could use about this… Is there some links or code how to Save and Open a txt file like the “Plain Text Edior” example, but for the “data representation model” ?
Thanks,
SEO
Model: Mac Intel and 10.4.8
Browser: Safari 419.3
Operating System: Mac OS X (10.4)
what does ‘not the same’ mean? Is it simply that the dialog doesn’t allow selecting text files?
Have you added text documents to the allowed document types? (Xcode → Menu ‘Edit active target …’ → Tab ‘Properties’ → ‘Document Types’)
I have checked the Example “Plain Text Editor” … and the code in Document.applescript
Now I am trying to write it from zero … So I create a New project in Xcode and then the Document.applescript file do not look like as in the example.
Here is the new Document.applescript file.
on data representation theObject of type ofType
(*Return the data that is to be stored in your document here.*)
end data representation
on load data representation theObject of type ofType with data withData
(* The withData contains the data that was stored in your document that you provided in the "data representation" event handler. Return "true" if this was successful, or false if not.*)
return true
end load data representation
And I wanted it to Open and Save Plain text as UTF8…
(I have changed the plist file, extensions and set the type to TEXT)
I have search on the web now for two days… but not found something… I just wanted to use the “Created Document Project from Xcode” … I think it should work as a “Simple Text Editor” if I just could Save and Open from the “Document Window” and the “NSTextWiew”
there are two pais of handlers that can be used to read/save a document in applescript studio. ‘data representation’ is the more convenient one since it cares for almost everything - file opening/writing/reading/closing. Only a few lines of script would make them do their job:
on data representation theObject of type ofType
return (contents of text view "editor" of scroll view "editor" of window of theObject as string)
end data representation
on load data representation theObject of type ofType with data withData
set contents of (text view "editor" of scroll view "editor" of window of theObject) to (withData as string)
return true
end load data representation
BUT - it doesn’t store it’s data as UTF-8 (or ASCII or other Unicode)
That’s why Apple used the other handler pair in the Plain Text Editor example: read from file/write to file.
To make the handlers of apple’s example read and write UTF-8, I think you just need to change ‘as string’ to ‘as «class utf8»’ in the read and the write script lines.
Hi and thanks for your response.
It looks that I never should have a lucky day with this… I tested with your code, but nope errors and errors (script).
I also tested the other variant (that works in the editor example), but nope, also script errors and when i muck with it, then the main menu is lost…
I have to wait to next years, and then I should give it a new try…
Regards,
SEO
I just tried the ‘read from file/write to file’ handlers writing «class utf8» in a very simple test and it works regardlessly.
Here is what I have done:
Interface Builder:
add a text view to your window - name text view and scroll view ‘editor’
select ‘Files Owner’, uncheck the ‘data representation’ handlers and check ‘read from file/write to file’ instead
Xcode:
add txt/TEXT to the document types
modify ‘Document.applescript’:
on read from file theObject path name pathName of type ofType
set theFile to open for access (pathName as POSIX file)
set theData to read theFile as «class utf8»
close access theFile
set contents of text view "editor" of scroll view "editor" of window of theObject to theData
return true
end read from file
on write to file theObject path name pathName of type ofType
set theData to contents of text view "editor" of scroll view "editor" of window of theObject
set theFile to open for access (pathName as POSIX file) with write permission
write theData to theFile as «class utf8»
close access theFile
return true
end write to file
That’s it. If you still don’t succeed, send me a pm with your email adress, then I’ll send you my test project.