NSTextView only erroring on some computers?

Hey MacScripters,
I am making a rather large data management program, and decided to use an NSTextView as a progress box that would display what is happening and any errors that occur. I’ve set up a function for this so that I call the function (named “ProgReport”), and give it the new progress as string, and a number to tell it what color the text will be (1 being red, and 0 being black). On my personal computer it works seamlessly, but when I run it on a different computer, I get this error:

I run this code in applicationWillFinishLaunching_:

[code]set someFontName to “Calibri”
set textSize to 14
tell current application’s NSColorList to set AppleColors to colorListNamed_(“Apple”)

		set someColor to AppleColors's colorWithKey_("Red")
		tell current application's NSFont to set myFont to fontWithName_size_(someFontName, textSize)
		set errorFontattrsDict to current application's NSDictionary's dictionaryWithObjects_forKeys_({myFont, someColor}, {current application's NSFontAttributeName, current application's NSForegroundColorAttributeName})
		
		set someColor to AppleColors's colorWithKey_("Black")
		tell current application's NSFont to set myFont to fontWithName_size_(someFontName, textSize)
		set regFontattrsDict to current application's NSDictionary's dictionaryWithObjects_forKeys_({myFont, someColor}, {current application's NSFontAttributeName, current application's NSForegroundColorAttributeName})[/code]

And here is the progReport function(the NSTextView is linked to the variable “prog”):

on ProgReport_(NewProgress, theModifier) set NewProgress to " " & NewProgress set textStorage to prog's textStorage() if theModifier = 1 then tell current application's NSAttributedString to set newText to alloc()'s initWithString_attributes_(NewProgress, errorFontattrsDict) else if theModifier = 0 then tell current application's NSAttributedString to set newText to alloc()'s initWithString_attributes_(NewProgress, regFontattrsDict) else set someFontName to "Calibri" set textSize to 14 tell current application's NSColorList to set AppleColors to colorListNamed_("Apple") set someColor to AppleColors's colorWithKey_(theModifier) tell current application's NSFont to set myFont to fontWithName_size_(someFontName, textSize) set attrsDict to current application's NSDictionary's dictionaryWithObjects_forKeys_({myFont, someColor}, {current application's NSFontAttributeName, current application's NSForegroundColorAttributeName}) tell current application's NSAttributedString to set newText to alloc()'s initWithString_attributes_(NewProgress, regFontattrsDict) end if tell textStorage to beginEditing() tell textStorage to appendAttributedString_(newText) tell textStorage to endEditing() set newTextLength to textStorage's |length|() as integer tell prog to scrollRangeToVisible_({|location|:newTextLength, |length|:0}) end ProgReport_
using a bunch of tries and on errors, I’ve found that the line “tell textStorage to appendAttributedString_(newText)” is the one that is erroring out. Any help would be appreciated. Thank you!

~Josh Fletcher

I’m not sure why you’re defining attrsDict when you don’t use it, but my first thought would be to try with a different font.

Stanley, I tried changing the font a few times, but it is still erroring out on the machine (but still working on mine!). Not sure if this helps, but it will error out the first time I try to run it, but then the second time it will run normally except that nothing will show in the Text View.

Also, I don’t actually know all too much about Objective C, I am more of an applescript guy. All of the objective C that I used in the code I posted was from your Exploring ApplescriptObjC book. In other words, if you have any suggestions or improvements for the Objective C portion of the code, I’m afraid you’re going to have to spell it out for me or else I won’t be able to understand. Sorry!

Still sounds like a font issue to me. Could he be using a font manager that loads the font?

In your “else” section, you have this:

set someFontName to "Calibri"
set textSize to 14
tell current application's NSColorList to set AppleColors to colorListNamed_("Apple")
set someColor to AppleColors's colorWithKey_(theModifier)
tell current application's NSFont to set myFont to fontWithName_size_(someFontName, textSize)
set attrsDict to current application's NSDictionary's dictionaryWithObjects_forKeys_({myFont, someColor}, {current application's NSFontAttributeName, current application's NSForegroundColorAttributeName})

but it never gets used.

Ahhh, that’s probably the issue. The man I am making the app for is a graphic designer and I can almost guarantee you he uses a font manager. In that case, how will I work around it? Could I embed the font in the program? If so, how would I call on it for NSTextView?

That snippet you posted is for the few special instances where I needed to use a color other than red or black. Rather than a number 1 or 0, If I put the color into the progReport function as a string, it will run it through that code and color the text that color.

Thank you for your help and quick responses so far, I hope we can find a way to fix this!

The first thing you can do is check to see if the font you want is in availableFonts. You could then fall back to a system font with systemFontOfSize_ if it’s not.

It looks like the APIs for loading your own files are C-based, so I doubt be able to do that in AppleScriptObjC.

How would i check availableFonts? Would it be:
tell current application’s NSFont to log availableFonts()

Thank you so much!

No, you have to ask the font manager, something like (untested):

set theFonts to current application's NSFontManager's sharedFontManager()'s availableFontFamilies()

Hey Shane,
Sorry it’s taken me so long to get back to you, my client has been busy. I ended up “embedding” the font by adding an “Application fonts resource path” to the info.plist file and then putting the .ttf font file in there. Now when I call on the font, the application recognizes it and everything is working perfectly! I only have one other question for you, I am using a progress bar, and I switch from indeterminate to incremental. The switching works fine, but when I make it indeterminate, it refuses to animate! I am doing the whole thing by binding variables in interface builder. Not sure if this helps, but the indeterminate bar WILL animate after the function is finished running (I include the doEventFetch() function you included in your book, so i don’t see why this is happening). Thank you so much for all of your help!

hm, what’s wrong with using an installed system font for something unemotional like an event log? :wink:

Are you calling startAnimation:?

Stefan:
it’s not so much that I need to change the font, it’s more that I want to know that I can, so for future projects, if for some reason it actually is a necessity to change the font, and the computer I will be running the application on is using a font manager, I will know exactly what I need to do to make my client happy and get the application on its feet.

Shane:
Like i said, I have bound Animate to the variable “animateMe”, so in the code i just write “set my animateMe to true” (and then after that i will call your doEventFetch function). Is there any reason why this shouldn’t be working? What is the syntax for calling on the Objective-C function?