Call SubRoutine in a script from an other one in Xcode

HI
So i made 2 applescript files in Xcode. I want to call a subroutine from script “InformationImage” an two have the value return. This function “nomFichier” is attache to a button in the interface

script InformationImage
        on nomFichier_(sender)
        set nbDocs to (current application's Routine's initializeNameFile())
        display dialog nbDocs
    end nomFichier_
    end script

Here is the subRoutine in an other script


script Cartouche
    on initializeNameFile()
        tell application "Adobe Illustrator"
            set listDesDocs to name of every document as list
        end tell
        log listDesDocs
        return listDesDocs
    end initializeNameFile:
end script

When i run the app, i have this message
“2016-12-16 17:37:45.074 AppelEntreScript[5108:1673740] *** -[InformationImage nomFichier:]: Routine doesn’t understand the “initializeNameFile” message. (error -1708)”

What is wrong ? Thanks for your help
Regards

The problem is that your calling a method in a Script named “Routine”


set nbDocs to (current application's Routine's initializeNameFile())

But you should be calling the method in the Scipt called “Cartouche”


set nbDocs to (current application's Cartouche's initializeNameFile())

There are also a couple of minor syntax error’s, but not serious ones, her’s a simple example of what you should be doing.


script ScriptA
    
    property parent : class "NSObject"
    
    on buttonClick:sender
        try
            set theResult to (current application's ScriptB's specialFunction()) as list
            display dialog (theResult as text)
        on error
            display dialog "Error occured"
        end try
    end buttonClick:
    
end script


script ScriptB
    
    property parent : class "NSObject"
    
    on specialFunction()
        set theList to {"One", " ", "Two", " ", "Three", " ", "Four"} as list
        return theList
    end specialFunction
    
end script

Alternatively you could have an init method in the ScriptB to retain it in a property for other uses, like this.


script ScriptA
    
    property parent : class "NSObject"
    
    property scriptB : missing value
    
    on buttonClick:sender
        set my scriptB to current application's ScriptB's alloc()'s init()
        try
            set theResult to (my scriptB's specialFunction()) as list
            display dialog (theResult as text)
        on error
            display dialog "Error occured"
        end try
    end buttonClick:
    
end script


script ScriptB
    
    property parent : class "NSObject"
    
    on init()
        continue init()
        return me
    end init
    
    on specialFunction()
        set theList to {"One", " ", "Two", " ", "Three", " ", "Four"} as list
        return theList
    end specialFunction
    
end script

Hope this helps

Regards Mark

Hi Mark
Thanks a lot, it works fine.
I was not so far.:slight_smile:
Regards
Christophe