Applescript and Swift Happy Together Yet

This post is probably a week early, WWDC and all. I have a semi-commercial project that’s in AppleScript that I want to migrate to Xcode. There are a number of parameters that need to be set, probably about 15 or so. I generally do these for each customer manually, and it limits the possibilities greatly, as you can imagine. I’d like to create a window in Xcode that will prompt the user to fill in all the parameters and options themselves.

I could write a straight Mac OS app that just has one window, collects all the info and creates a plist; and launchd file for the timing functions. I’m sure the storyboard features would make this fairly easy. (I have written a working iOS app with Swift and am familiar with these.)

It would be nicer to include all the AppleScript right in the project. It would also allow me to expand it to include other Applescripts I’ve written. I did start to learn a little ObjC but then Swift came along and it seems to this observer that the only thing Apple seems to want to get rid of faster than automation in general is ObjC.

I’m looking for advice and resources. Do I start with an Xcode AppleScript project, or just do the information collection and leave the AppleScript alone? Does Xcode/AppleScript play nice with Swift yet? Are there iBooks or courses out there that anyone can recommend that deal with AppleScript in the Xcode environment with Swift vs ObjC ?

TIA

Lenny

I’d create the project in Objective-C (it won’t be going anywhere for a long time if ever; too much of Swift is dependent on it), and then include my vanilla AppleScripts in the bundle and call them with Obj-C code.

There’s a bunch of ways you can do that, but probably the most straigthforward is to use something like this, where the supplied url points to an .applescript text resource in your bundle that is compilable AS:

-(NSString *)callNSAppleScript: (NSURL *)url 
{

NSDictionary *myError;
NSAppleScript *userScript = [[NSAppleScript alloc] initWithContentsOfURL: url error: &myError];

NSAppleEventDescriptor *result = [userScript executeAndReturnError: &myError];

    return result.stringValue;
}

You can look at NSAppleEventDescriptor for other kinds of return value depending on what you want to do, but I generally find the stringValue the most useful.

You’ll need to check that your .applescript files are compilable in a script editor. The error will give you some basic info if they fail, but Xcode offers minimal-to-nothing in the way of debugging AppleScript.

search for applescript iin swift:

Swift code:
let myAppleScript = “…”
var error: NSDictionary?
if let scriptObject = NSAppleScript(source: myAppleScript) {
if let output: NSAppleEventDescriptor = scriptObject.executeAndReturnError(
&error) {
print(output.stringValue)
} else if (error != nil) {
print(“error: (error)”)
}
}

Seem interesting. (I don’t know Swift)

Don’t know precisely what you want to do beyond setting prefs.
And don’t know how tightly you can integrate Swift interfaces with as. But maybe!

Back in the time of cats , I’ve moved my rather complex applescript to Xcode to have one small window with all the controls (pop-ups, several buttons and checkboxes) and feedback (2 progress bars with updating text labels) rather than dozens of one-question dialogs.

Then I had to re-do the whole interface in Xcode when ASObj first came out-which Shane Stanley’s great book and editor enabled.Check his ASObj books out if you haven’t. I think you can do all that in ScrptDebugger now (haven’t had to try).

Hello everyone, I want to ask should I learn Swift or Objective-C now, I Read this https://www.cleveroad.com/blog/swift-vs-objective-c--what-is-the-best-language-for-ios-development-
and now I am confused!
Can you help me?

What’s your ultimate aim?

It depends what type of platform you want to use.
If you want iOS app you can learn Swift. With that you will be able to create apps for iPhone iPad and Macs.
Learn Java if you want to publish your app on Android. You can use Java for a lot of things other than phone apps.
You can learn JavaScript/html/css if you want your app to be cross platform. You can check Cordova (Apache) for that. By doing that you won’t have native code so you will have access to less functionalities on each platform.
I have an iPhone and a Mac so I’ve decided to learn Swift.
Read also this https://artjoker.net/blog/objective-c-or-swift/

Seems like using initWithContentsOfURL and executeAndReturnError in a xcode project or any other tool wants to control Script Debugger or fail if not opened and runs the file with tell ‘source application’? Seen on Catalina. Any explanation for this behavior?

Short objectivec console test:

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        NSLog(@"Hello, World!");
        NSURL* url = [NSURL fileURLWithPath:@"/Users/username/Documents/test1.scpt"];NSDictionary* errors = [NSDictionary dictionary];
        NSAppleScript* appleScript = [[NSAppleScript alloc] initWithContentsOfURL:url error:&errors];
        [appleScript executeAndReturnError:nil];
        
    }
    return 0;
}

applescript nothing more then 1 line set a to “abc”.

EDIT: only happens when the applescript file is saved with script debugger. When saved with Script Editor it does not interfere…

Did you save the script with debugging on?

AHA, good to know that the debugger info is saved in the scpt. With it disabled, no interruption anymore. Thanks.