Friday, December 15, 2017

#1 2009-10-26 09:52:39 pm

Richard
Member
From:: Herefordshire, UK
Registered: 2009-09-09
Posts: 529
Website

2 problems

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

1) I get this logged:

2009-10-27 02:30:39.334 Richard's Text Editor[1926:a0f] /Users/Richard/Xcode/Richard's Text Editor/build/Debug/Richard's Text Editor.app/Contents/Resources/RTE Recents.txt
2009-10-27 02:30:39.361 Richard's Text Editor[1926:a0f] *** -[Richard_s_Text_EditorAppDelegate subsave:]: Can’t make «class ocid» id «data kptr00000000604A620002000000» into type «class fsrf». (error -1700)


from this script:

Applescript:

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):

Applescript:

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

2009-10-27 02:36:16.734 Richard's Text Editor[1976:a0f] *** -[Richard_s_Text_EditorAppDelegate subsave:]: File /Users/Richard/Xcode/Richard's Text Editor/build/Debug/Richard's Text Editor.app/Contents/Resources/RTE Recents.txt wasn’t found. (error -43)


2) How do I trigger

Applescript:

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 smile

Offline

 

#2 2009-10-27 05:13:09 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5199

Re: 2 problems

Richard wrote:

open for access valrecpreffile with write permission


Try:

Applescript:

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.

2) How do I trigger

Applescript:

wineditor's setDocumentEdited_(true)

when someone changes a text view's text


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

and how can I activate script when someone closes the window.


Implement NSWindow's NSWindowWillCloseNotification.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#3 2009-10-27 08:22:26 pm

Richard
Member
From:: Herefordshire, UK
Registered: 2009-09-09
Posts: 529
Website

Re: 2 problems

3rd question) Why are you so clever? XD

4) similar with this:

Applescript:

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_

...

Applescript:

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

...

Applescript:

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:

2009-10-28 01:22:16.209 Richard's Text Editor[1684:a0f] Macintosh HD:Users:Richard:Documents:Other Text Files:titled.txt
2009-10-28 01:22:16.211 Richard's Text Editor[1684:a0f] 0
2009-10-28 01:22:16.255 Richard's Text Editor[1684:a0f] 2
2009-10-28 01:22:16.323 Richard's Text Editor[1684:a0f] *** -[Richard_s_Text_EditorAppDelegate subopenrecent:]: Can’t make "Macintosh HD:Users:Richard:Documents:Other Text Files:titled.txt" into type file. (error -1700)


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

Applescript:

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

Applescript:

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

instead please smile

Last edited by Richard (2009-10-27 08:38:25 pm)


Filed under: Finder, NSString

Offline

 

#4 2009-10-28 04:59:54 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5199

Re: 2 problems

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:

Applescript:

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.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#5 2009-10-28 08:32:41 am

Richard
Member
From:: Herefordshire, UK
Registered: 2009-09-09
Posts: 529
Website

Re: 2 problems

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

Last edited by Richard (2009-10-28 10:26:52 am)

Offline

 

#6 2009-10-28 06:51:54 pm

Richard
Member
From:: Herefordshire, UK
Registered: 2009-09-09
Posts: 529
Website

Re: 2 problems

My app is working perfectly now smile

Shane Stanley wrote:

2) How do I trigger

Applescript:

wineditor's setDocumentEdited_(true)

when someone changes a text view's text


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

and how can I activate script when someone closes the window.


Implement NSWindow's NSWindowWillCloseNotification.


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

Offline

 

#7 2009-10-28 08:54:15 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5199

Re: 2 problems

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).


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#8 2009-10-29 10:42:11 am

Richard
Member
From:: Herefordshire, UK
Registered: 2009-09-09
Posts: 529
Website

Re: 2 problems

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

Applescript:

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

Offline

 

#9 2009-10-29 04:06:40 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5199

Re: 2 problems

Try on TextDidChange_(notif).


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#10 2009-10-29 05:22:49 pm

Richard
Member
From:: Herefordshire, UK
Registered: 2009-09-09
Posts: 529
Website

Re: 2 problems

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

1) Inside the:

Applescript:

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?

Last edited by Richard (2009-10-29 05:27:29 pm)


Filed under: Finder

Offline

 

#11 2009-10-29 07:15:08 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5199

Re: 2 problems

Richard wrote:

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


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


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#12 2009-10-30 09:34:30 pm

Richard
Member
From:: Herefordshire, UK
Registered: 2009-09-09
Posts: 529
Website

Re: 2 problems

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.

Offline

 

#13 2009-10-31 05:46:54 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5199

Re: 2 problems

Which menu items?


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#14 2009-10-31 06:48:17 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11482
Website

Re: 2 problems

Richard wrote:

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.


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
[code]-(BOOL)validateMenuItem:(NSMenuItem*)theMenuItem[/code]
you can distinguish the menu items by checking the title or action and return YES or NO to en/disable the menu item


regards

Stefan

Offline

 

#15 2009-10-31 08:02:08 am

Richard
Member
From:: Herefordshire, UK
Registered: 2009-09-09
Posts: 529
Website

Re: 2 problems

What variable is the menuitem?

Offline

 

#16 2009-10-31 09:14:25 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11482
Website

Re: 2 problems

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):

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

Last edited by StefanK (2009-10-31 09:15:23 am)


regards

Stefan

Offline

 

#17 2009-10-31 11:16:59 am

Richard
Member
From:: Herefordshire, UK
Registered: 2009-09-09
Posts: 529
Website

Re: 2 problems

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

big_smile

Last edited by Richard (2009-10-31 12:28:28 pm)

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)