applescript-objc

Call a method or function from Objective-c in AppleScript


I'm trying to use LestMove to be more precise the second implementation method where it says:

Option 2:

Copy the following files into your project:

PFMoveApplication.h PFMoveApplication.m If your project has ARC enabled, you'll want to disable ARC on the above files. You can do so by adding -fno-objc-arc compiler flag to your PFMoveApplication.m source file. See How can I disable ARC for a single file in a project?

If your application is localized, also copy the 'MoveApplication.string' files into your project.

Link your application against Security.framework.

In your app delegate's "-[applicationWillFinishLaunching:]" method, call the PFMoveToApplicationsFolderIfNecessary function at the very top.

but I'm not able to call the method / Class, could someone help me with this issue? Thanks in advance!


Solution

  • In general, there are a couple of ways to set up an Objective-C class in your AppleScriptObjC project:


    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):

    1. Add PFMoveApplication.h and PFMoveApplication.m files to the project (the class name is LetsMove)
    2. Add Security.framework to Link Binary With Libraries in Build Phases
    3. 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:

    1. Move the @interface declaration to the .h file and include the redefined method signatures below in it:
    2. The PFMoveToApplicationsFolderIfNecessary and PFMoveIsInProgress methods are redefined as instance methods:

      - (void)PFMoveToApplicationsFolderIfNecessary;
      - (BOOL)PFMoveIsInProgress;
      
    3. 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)
    4. 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
    5. 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()