In general, there are a couple of ways to set up an Objective-C class in your AppleScriptObjC project:
- Add the file(s) to the project - the Objective-C class name will be
the one used in the
@interface/@implementation declarations
Add an outlet property in the AppleScript class/script you are using, e.g. property someProperty : missing value
Instantiate the class programmatically:
set someProperty to current application's ClassName's alloc's init()
or
- Connect stuff up with the Interface Builder:
- Add an NSObject (blue cube) from the library to your project
- Set the class of the object/cube to the class name of the Objective-C file(s) in the Identity Inspector
- Connect the AppDelegate IB Outlet to the object/cube in the Connections Inspector
After setting up the outlet property, the Objective-C methods can be used like any other script/class:
someProperty's handler()
That LetsMove project wasn't really set up for AppleScriptObjC, but I was able to tweak it a bit to get it running. I'm not that great at writing Objective-C, but the following worked for me using a new default AppleScript project with Xcode 10 in Mojave (the original file is over 500 lines long, so I'm just highlighting the changes):
- Add
PFMoveApplication.h and PFMoveApplication.m files to the project (the class name is LetsMove)
- Add
Security.framework to Link Binary With Libraries in Build Phases
- As described in the original project README, add the compiler flag
-fno-objc-arc to the Objective-C file in Compile Sources of the Build Phases
-- Now to alter the Objective-C files a bit:
- Move the
@interface declaration to the .h file and include the redefined method signatures below in it:
The PFMoveToApplicationsFolderIfNecessary and PFMoveIsInProgress methods are redefined as instance methods:
- (void)PFMoveToApplicationsFolderIfNecessary;
- (BOOL)PFMoveIsInProgress;
- Redefine the above method signatures in the
.m file, and include those methods in the @implementation section - to do this, move the @end to just before the helper methods (after the PFMoveIsInProgress method)
- Remove the
isMainThread statement at the beginning of the PFMoveToApplicationsFolderIfNecessary method - this is not not needed (AppleScript normally runs on the main thread), and fixes another issue
- There is still a little stuff in there from the original app such as NSUserDefaults, so for your own project, give it a look to see if anything else needs changing (dialog text, etc)
And finally, in the AppDelegate.applescipt file, the following was added to applicationWillFinishLaunching:
current application's LetsMove's alloc's init()'s PFMoveToApplicationsFolderIfNecessary()