One App, multiple .scpt's

Hello there, this is my very first message here, so HELLO EVERYONE!

I have this app, that I’ve build using the tons of tips from this site and StackExchange. The script is starting to get unusually big and confusing (to me), I wanted to split into sections, each group of functions in it’s own scpt file and IF possible keep only the properties and initialisation and termination on the main script file.

I’ve tried some tricks, but this is my very first AppleScriptObjC project, and I am quite new to all this. Needless to say I failed.

Is that possible?

Hi machuser
Welcome to the macscripter forums.

In answer to your question.
Yes it is possable to split your ASOC projects code across several scripts, and in fact I would highly recommend you do so for large projects, as once you get beyond a few hundred lines of code, debugging becomes a headache.

I’ve worked on two very large projects over the past couple of years, one of which had 8 scripts, and the other had 9 scripts, both projects have about 3000 to 3500 lines of code, which as you can imagine would be impossable to work with in one script.

I personally try to use the MVC design pattern for my ASOC projects, although it’s not always very intuitive with ASOC projects, as it is with ObjC or Swift projects.
Also Xcode’s support for AppleScriptObjC code and projects is poor in comparison to the other aforementioned languages.

So I would advise splitting the code across scripts in a logical fashion, so use the AppDelegate script for things relating to the main applications function, for example, when the app opens or closes, or is activated or deactivated, or when files need to be opened when dropped, and handling the main window and main menu functions.
And then use other scripts for other specific functionality within the App, for example data handling or manipulation, displaying the data in the user interface, for checking system hardware or software resources and availability, setting and saving preferences, and so on in an exhaustive list.

As others on this forum will tell you, there are several ways to work with multiple scripts in projects, loading the scripts with the load script command, or initialising a reference to the script with alloc()'s init(), or calling the scripts functions directly with current applications otherScript’s method(), all of which have pro’s and con’s, and does depend on what your trying to achieve.
You can also hold a reference to other project scripts in global properties, and also have a reference to them in interface builder for event handling and UI updating.

So when you have a clearer idea of how you want make your project more modular, there are plenty of knowledgable ASOC people that hang out here, and I’m sure you could be pointed in the right direction, once you have a more specific question.

Regards Mark

Thanks Mark FX. I’ll check into it.

But because I’m a [s]uber noob at this, the lack of an example made it pretty cryptic to me, right now I’m trying “load script”, with no success I may add.I’ll keep trying.

Right now I am trying:

     set fname to POSIX path of (path to resource "Hosts.scpt") as text
    return load script fname as alias

The error is:

But when I:

The file is there.

Well this is a complication. The script is working ok all hardcoded. My wishes were to add a preferences window to make every var editable, so I could put this code out. It is an app to control Web services (apache sql dnsmasq etc). Pretty handy for those that don’t want to use Mamps or other bundle solutions.

But this is an aggravation. Thank anyway for the help. I guess I’ll just leave as it is.

Hi machuser

Sorry for the late reply.

It seems that I might have steered you wrong with the “load script” command when used in ASOC projects in Xcode.
I also couldn’t use it when I tried, but another user might be able to shed more light on that one.

I generaly use either a script class reference, where the script has an init() method.
Or alternatively call the script directly.

A script class example.


--
--  MyScript.applescript
-- 
script MyScript

    use scripting additions
    property parent : class "NSObject"
    
    on init()
        continue init()
        return me
    end init
   
    on method()
        log "MyScript's method() called"
    end method
 
end script

A plain script example.


--
--  OtherScript.applescript
-- 
script OtherScript

    use scripting additions
    property parent : class "NSObject"
    
    on method()
        log "OtherScript's method() called"
        return "Message from OtherScript" as text
    end method
 
end script

AppDelegate calling example.


--
--  AppDelegate.applescript
--

script AppDelegate

    use scripting additions
    property parent : class "NSObject"
	
    -- IBOutlets
    property theWindow : missing value
    
    property myScript : missing value
	
    on applicationWillFinishLaunching:aNotification
        set my myScript to current application's MyScript's alloc()'s init()
        my myScript's method()
    end applicationWillFinishLaunching:
    
    on applicationDidFinishLaunching:aNotification
        set theMessage to (current application's OtherScript's method()) as text
        log theMessage
    end applicationDidFinishLaunching:
    
    on applicationShouldTerminate:sender
	return current application's NSTerminateNow
    end applicationShouldTerminate:
	
end script

I hope that is less cryptic for you.

I also noticed in your posted error message example, that you are using a retun value from the applicationWillFinishLaunching:aNotification AppDelegate method, this is not allowed, as that particular AppDelegate method has a void return value, meaning that it returns nothing.

Regards Mark