Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

ApplescriptObjc Full Document Based Application

I am working on a document editor in Xcode, using ApplescriptObjc, but I'm having an issue. I cannot figure out how to create an NSDocument integrated application that can work on the level of something like TextEdit. I would really appreciate it if someone could either point me to a good tutorial, or documentation, specific to ApplescriptObjc, or some sample code for me to go off of. Thanks!

MacBook Pro (Retina, 13-inch, Mid 2014), OS X El Capitan (10.11.2)

Posted on Feb 2, 2016 5:12 PM

Reply
Question marked as Best reply

Posted on Feb 9, 2016 3:00 PM

I doubt you are going to find much AppleScriptObjC specific information, as most tutorials and documentation are in Objective-C (or Swift), and AppleScript just isn't that popular for more serious application development. Your best bet would be to download the TextEdit sample code and convert the parts you need.

9 replies

Feb 2, 2016 7:17 PM in response to red_menace

Thanks. I really appreciate that. If you don't mind, I also had a small question on a similar topic. I was asking about NSDocument because I am attempting to make an HTML editor in ApplescriptObjc. Even if it takes some time to implement NSDocument, I have gotten an app that takes HTML code as input and make an output in Safari. My only concern is that the code creates an html files using the terminal's echo command, and I always get a blank, rich text enabled, html file. The weird thing is that is still displays properly in Safari. For example, this would be used as input to the echo command —

\<html\>\<body\>Hello World\<\/body\>\<\/html\>


My text file is still blank.. Another weird thing is that the echo command input is single lined, as you can see, but the text view input is multi lined, as is the result in Safari. This is all very strange, but in the end I get a result in Safari. I would just like to take my input from a text view to an html file with all of the tags still in place. The system seems to use the HTML tags as text formatting.... Anyway, thanks for the link in your previous reply!

Feb 2, 2016 7:48 PM in response to red_menace

Well, I am doing things a bit differently. I'm taking text straight from a text view as user input, putting in backslashes in the appropriate places to make it easier red by terminal, and just righting it to the HTML file with echo. Here is my code —


on runCode_(sender)

set htmlcode to codeBox's |string|()

set htmlcode to htmlcode as text

set invalidList to {"<", ">", "/"}

repeat with charReplace in invalidList

set htmlcode to replace_chars(htmlcode, charReplace, "\\" & charReplace)

end repeat

set lines1 to paragraphs of htmlcode

-- choose from list lines1

set newlist to {}

repeat with theitem in lines1

set newval to ((theitem) as text)

set end of newlist to newval

log newval

end repeat

--choose from list newlist

set htmlcode to newlist as string

display alert htmlcode

do shell script"echo "&htmlcode&" > ~/library/Application\\ Support/HTMLCode.html"

-- do shell script "textutil -convert html ~/library/Application\\ Support/HTMLCode.txt"

tellapplication"Safari"toopen location "/Users/"&whoami&"/Library/Application Support/HTMLCode.html"

end runCode_


Note that the commented lines are lines that I was previously using, and have disabled for future convenience. I have tried textutil to instead save the htmlcode variable to a txt file and use the textutil line to convert it to html. Even doing that, the HTML tags still seem to be used as formatting. The Safari end looks nice, but a blank HTML file is all that I get... Isn't this fun... 😀

Feb 9, 2016 3:00 PM in response to Jake3231

Hi,



You can use a method from the NSString class to write to a file, like this:


set htmlcode to codeBox's |string|()
set tFile to POSIX path of ((path to application support from user domain as text) & "HTMLCode.html")
set te to current application's NSUTF8StringEncoding
set r to htmlcode's writeToFile:tFile atomically:true encoding:te |error|:(missing value)
if not r then display alert "not saved" -- some error

Feb 3, 2016 4:59 PM in response to Jake3231

I tried your script using textutil, and for me it converts back and forth from text, html, and rtf. If there is any formatting, it will be converted, for example text in an html bold tag would be bold text in an rtf file. If your text view wraps, it may look different when converted depending on where the line endings actually are.

Feb 9, 2016 12:18 PM in response to red_menace

Sorry, I'm a little late to test the code, but I wanted to ask if you know how I could allow tags to be certain colors. I'm imagining all of the tags are red, but when the tag becomes invalid, like a carrot is removed, it'll revert to the default color. Is this possible in real time? Otherwise, could I do it when the input code is compiled? I'll certainly test the code tonight.

Feb 9, 2016 2:59 PM in response to Jacques Rioux

Thanks guys! I finally got the save function to work, and now I'm on to color coding. Thanks for your help. I had to end up using the generic applescript write function to get what I wanted;

set tFile to POSIX path of ((path to application support from user domain as text) & "HTMLCode.html")

do shell script"echo > ~/Library/Application\\ Support/HTMLCode.html"

write htmlcode to tFile


This will clear the text file and write my new code in its place. Thanks!

ApplescriptObjc Full Document Based Application

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.