My googling hasn’t managed to help me understand the differences or use cases for either Foundation or AppKit frameworks.
Can anyone point me towards a resource that explains what each is and if or why I should include one or the other or even both in a AS script “header” ??
furbies. To answer your question, it’s important to distinguish between an AppleScript and an AppleScript that employs AppleScriptObjC (aka ASObjC and ASOC), which is a bridge between AppleScript and Objective-C. When writing an AppleScript that does not employ AppleScriptObjC, no headers are required. You would include a use framework "Foundation" header when your script uses the Foundation class to do something (e.g. look here), and the same is true for the use framework "AppKit" header. If you include any use framework headers in a script, you would normally need to include a use scripting additions header, which allows basic AppleScript to run.
You will often see a use AppleScript version "2.4" header in an AppleScript. This header is optional and will report an error if the script is run under an earlier version of AppleScript.
The bottom line is that you do not need to include a header when writing a basic AppleScript, and the vast majority of AppleScripts do not use AppleScriptObjC. Additional information on this topic will be found here.
I asked because I’m attempting to hack together some applescript so I can have a menubar for my little project, and the menu examples I’ve found here and elsewhere either have
use framework “Foundation”
or
use framework “AppKit”
or both.
I’ve tried disabling both (one at a time) and discovered, that at least for the menu generating code I’ve scraped together, I do need the “AppKit” one.
But as I said, my googling didn’t result in an Dummy’s Guide level explanation of what each is and does…
furbies. There are exceptions, but pretty-much any time you use ASObjC, you need to include a use framework "Foundation" header. I believe the script you are referring to uses the NSMenu class (documentation here), which is part of the AppKit framework. For one of several reasons, the script may run correctly without a use framework "AppKit" statement, but it won’t run reliably over time.
peavine, yeah the code I’m bodging together does use NSxxxxxx code (the old Next Step stuff/days) and it also has “menubar’s setSubmenu:asstMenu forItem:asstMenuItem” as well. I assume the second sort of code is using the AppKit as well ?
In addition to the extended info provided by @peavine:
Just include both always, and you won’t go wrong.
Even if including AppKit isn’t always required for the actual functionality, it ensures that you get code completion for AppKit methods in Script Debugger, which won’t be available otherwise.
The current application is what loads the Cocoa terminology (which is also why you need to use that term so much), so it depends a bit on what is running the script. The Foundation framework defines basic data types and functions and is always required, but when using Script Debugger (or Script Editor) and scripts saved as applications, AppKit is usually already loaded. Another “framework” to consider would be Cocoa, which just groups together the Foundation, AppKit, and CoreData frameworks.
If you have Xcode installed, the various framework headers (if you are into that kind of stuff) can be viewed in /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks/. You can also use the Apple Developer Documentation to look up what the various classes are.
Foundation contains the basic vocabulary, types like NSString, NSURL, NSDate, the numeric types, the collection types NSArray/NSDictionary/NSSet but also classes like NSURLSession.
On the other hand AppKit contains all types related to an application (NSApplication) and the user interface like NSMenu, NSTableView/NSCollectionView, cells, responders, view/window controllers, windows etc. NSImage belongs also to AppKit although one could assume it’s part of Foundation.
Foundation is an universal framework and can be used in macOS, iOS, watchOS, tvOS and visionOS without modifications. AppKit belongs only to macOS. There are other application/UI frameworks for the other platforms.