Different problems, different app while I’m waiting for answers, sorry XD
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
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.
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))
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).
Yeah I know, posting slipped my mind. Worked that out 2 hours ago XD. New probs, will post below later.
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?
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.
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.