Call subroutine

Hi, I think you will be hearing a lot from me in the near future…

Still converting old projects to ASOC.

I have a call to a subroutine and it is not working. I seem to be missing some basic concept. Can anyone tell me what I am doing wrong.

on launched_(theObject)
    set my statusText to "Initializing"   --This works
    my CheckAllServers_()   --Here is my call
end launched_

on CheckAllServers_()
   log "Got here"   --This does not show in the log so it is not getting here.
end CheckAllServers_

Model: iMac
Browser: Firefox 3.6.4
Operating System: Mac OS X (10.6)

I am no expert, but try doing this:

    on launched_(theObject)
        set my statusText to "Initializing"   --This works
        my checkAllServers()   --Here is my call
    end launched_

    on checkAllServers()
       log "Got here"   --This does not show in the log so it is not getting here.
    end checkAllServers_

There shouldn’t be an underscore here either (It won’t keep it from working, but it’s not correct syntax). Underscores mean arguments – one underscore for each argument a method takes if you are using cocoa methods. If you are using your own methods that are in the same script, you can use them or not, but you shouldn’t use them if the method takes no arguments. You also don’t need the “my” in front of the call.

That being said, putting in the underscores shouldn’t have prevented your method from being called – it’s bad syntax, but you could put in 5 underscores if you wanted, as long as you had 5 in the method name as well, so I don’t know why it didn’t work. Are you sure that the launched_ method is being called?

Ric

Tried all of the suggestions and still didn’t work. I ended up taking my code that was in the on launched block, and I put it in the “on applicationWillFinishLaunching_(aNotification)” blobk that is created by default when you create the project. Now it works.

I have been looking through the documentation. I don’t see “launched”. Is this no longer used? I see “applicationDidFinishLaunching”. Is that what I should use where I used to use on launched?

Ric is right about the underscore. when the handler doesn’t have any parameters you shouldn’t use an underscore. But what’s at the end doesn’t really matter at all because xcode will compile be corrected and the text behind a end tell handler doesn’t really matter at all, can be anything it just indicates that you’re using xcode 4.x and not xcode 3.x (here correct it while build/compile).

I think on launched is not called by the right object. on launched theObject is called by the AppleScript interpreter and not by the NSApp’s delegate so it runs ‘outside’ the application like in the studio days.

When you’re in the application delegate you should use the NSApplicationWillFinishLaunching notification instead and then the other handler will be called as well.


on applicationWillFinishLaunching_(aNotification)
	my checkAllServers()
end applicationWillFinishLaunching_

on checkAllServers()
       log "Got here"
end CheckAllServers

You should have a look at the NSApplicationDelegate Protocol reference to see the various methods that are called by the application. You can use awakeFromNib (this one’s in NSObject), applicationWillFinishLaunching and applicationDidFinishLaunching which are called in that order. You can also launch applications by double clicking on a file that is of a type that your app opens with application:openFile: which is called between the “will” and “did” versions mentioned above. Using these methods, you can have some control over what order things happen during startup of your app.

Ric

Thanks! Still learning…