2 problems

Different problems, different app while I’m waiting for answers, sorry XD

  1. I get this logged:

from this script:

tell current application's class "NSBundle"
	tell its mainBundle()
		set valrecpreffile to its pathForResource_ofType_("RTE Recents", "txt")
	end tell
end tell

followed by this (the 2 lines that get logged):

log valrecpreffile
open for access valrecpreffile with write permission

as alias doesn’t help but as string as alias tells me the file doesn’t exist

  1. How do I trigger
wineditor's setDocumentEdited_(true)

when someone changes a text view’s text and how can I activate script when someone closes the window.

Help appreciated :slight_smile:

Try:

open for access file (valrecpreffile as text) with write permission

But there do seem to be problems using the “as” parameter when writing to files.

It normally happens automatically if you use the document-based template. Otherwise you need to implement one of the NSTextView delegate methods.

Implement NSWindow’s NSWindowWillCloseNotification.

3rd question) Why are you so clever? XD

  1. similar with this:
on subopenrecent_(sender)
	subgetfiles()
	log valfile
	if valfile as string is "%default%" then
		set valfile to (choose file of type {"public.plain-text"} with prompt "Because you have not saved a file with this application before you must choose a file to open.")
		subopenfile(sender)
	else
		log 0
		subopenfile(sender)
	end if
end subopenrecent_

on subgetfiles()
	tell current application's class "NSBundle"
		tell its mainBundle()
			set valrecpreffile to its pathForResource_ofType_("RTE Recents", "txt")
		end tell
	end tell
	tell class "NSString" of current application
		-- NSUTF8StringEncoding has a value of 4
		set valfile to (its stringWithContentsOfFile_encoding_error_(valrecpreffile, 4, missing value))
	end tell
end subgetfiles

on subopenfile(sender)
	winwelcome's performClose_(sender)
	log 2
	if (get eof (valfile as text)) is 0 then
		set valrev to ""
	else
		log 1
		set valrev to read (valfile as text)
	end if
	tell application "Finder"
		set valname to name of valfile as string
		end tell
	wineditor's setTitleWithRepresentedFilename_(POSIX path of valfile)
	valwintext's setString_(valrev)
	wineditor's makeKeyAndOrderFront_(sender)
	wineditor's setDocumentEdited_(true)
end subopenfile

log:

The pref file contains the text stated in the valfile log and that file contains text which would make the get eof if to do the else. But I am guessing this is still a coercion problem.

P.S. I now have

set valrecpreffile to its pathForResource_ofType_("RTE Recents", "txt") as text

and removed all the "as text"s and it works fine but with the extra “as text” doesn’t work, I think, so please coerce

set valfile to (its stringWithContentsOfFile_encoding_error_(valrecpreffile, 4, missing value))

instead please :slight_smile:

You’ve lost me. To use the read/write/eof/etc commands, you should use file (or alias) before the HFS path. So, for example:

set valrev to read file (valfile as text)

That’s the case in ASObjC and AS generally.

You will probably also have to enclose them in a “tell current application” block for ASObjC.

Fixed it, it needed as string/text as alias.

My app is working perfectly now :slight_smile:

Could you explain these a bit further, I tried looking them up and using them but I haven’t got a clue.

Actually, for a window you’re better off just setting your script as a delegate for the window (something like myWindow’s setDelegate_(me)), and then implement the appropriate NSWindowDelegate Protocol methods, like on windowWillClose_(notification).

Similarly for a text view: set your script as the delegate, and implement the delegate methods you want.

In both cases, a delegate will also receive notifications. So if you want to act on NSTextViewDidChangeTypingAttributesNotification, you include a handler on TextViewDidChangeTypingAttributes(notification).

I tried those, the script is the delegate of file owner, window, and text view. Close window works but changing the text doesn’t.

on textViewDidChangeTypingAttributes_(notification)
	log ":D"
end textViewDidChangeTypingAttributes_

Try on TextDidChange_(notif).

Yeah I know, posting slipped my mind. Worked that out 2 hours ago XD. New probs, will post below later.

  1. Inside the:
on windowWillClose_(notification)
		if (wineditor's isDocumentEdited()) is true then
			tell application "Finder"
				if valnew is true then
					set valdocname to "Untitled"
				else
					set valdocname to (name of valfile) as string
				end if
			end tell
		end if
		try
			if button returned of (display dialog "Do you want to save the changes you made in the document " & valdocname & "?

Your changes will be lost if you don't save them." buttons {"Cancel", "Don't Save", "Save..."} default button 3 with icon note) is "Save..." then --with title "Richard's Text Editor")
				log 1
				subsave_(notification)
			else
				log 2
			end if
		on error
			log 0
		end try
	end windowWillClose_

for where log 0 is (cancel) how would I stop the window closing?

You’d use windowShouldClose_ instead, and return true or false.

All working great now. But I have a small problem, how do I grey out items in menus (and disable shortcuts)? This is causing alot of problems, unchecking enabled or checking hidden do not help at all.

Which menu items?

Either you disable “Auto enable Items” in each NSMenu and en-/disable the menu items “manually”

or (the better way) is to use this delegate method

-(BOOL)validateMenuItem:(NSMenuItem*)theMenuItem

you can distinguish the menu items by checking the title or action and return YES or NO to en/disable the menu item

What variable is the menuitem?

the method is a delegate method which will be called for each menu item when a menu opens.
It can be used in this way (pseudo code):

if action of theMenuItem is "close:" return (count windows) return YES
the code enables the menu item which responds to the action close, if there is at least one open window

Oh wait I just worked out the auto enable thing, works just right, the popup was full of buttons so I had been en/disable and un/hiding things anyway. Thank you.

app: http://files.me.com/imadrichard/4wiktu
project: http://files.me.com/imadrichard/uhsibv

:smiley: