I have started a project to make some notes for Swift, Objective-C and AppleScriptObjC.
This is a early version of it. The purpose of this notes is to help a user to be able to understand the syntax of different language. To make custom framework to be called from AppleScriptObjC.
Any comments is welcome or if you find any error.
(*** Notes ***)
Objective-C to AppleScriptObjC
Objective-C blocks are not supported in AppleScriptObjC
In other words methods that use completionHandler and is of type void and begins with ^. Objective-C block that invokes after the process is finish.
(*** Syntax ***)
In Objective-C
options:(NLTaggerOmitPunctuation | NLTaggerOmitWhitespace)
In Swift
Options = [.omitPunctuation, .omitWhitespace]
In AppleScriptObjC
options:((current application's NLTaggerOmitPunctuation as integer) + (current application's NLTaggerOmitWhitespace as integer))
(*** Framework ***)
(**
* Make new project and choose Framework -> Next -> Your name for the Framework and set the language to Objective-C -> Next
***
* Build a framework in Xcode to use Objective-C. So we could call the custom class and methods from AppleScriptObjC.
* We will male custom class with instance type with 2 parameters and return its max value.
*
** Xcode setting
* Set the targets class <Build_Active_Architecture_Only> to <No> (If the machine is Apple Silicon)
* And import the framework Foundation
*)
In the file <FGMath.m>
#import <Foundation/Foundation.h>
#import <FGMath/FGMath.h>
@implementation FGMath
-(int)maxWithNumber:(int) num1 secondNumber:(int) num2 {
int result;
if (num1 > num2) {
result = num1;
}
else {
result = num2;
}
return result;
}
@end
In the file <FGMath.h>
#import <Foundation/Foundation.h>
@interface FGMath : NSObject
-(int)maxWithNumber:(int)num1 secondNumber:(int)num2;
@end
(**
* Class: FGMath
* instance method: maxWithNumber:secondNumber:
**
* use framework "FGMath"
*
* set theMath to current application's FGMath's alloc()'s init()
* set theResult to theMath's maxWithNumber:100 secondNumber:200
*)
In Objective-C
#import <Foundation/Foundation.h>
@interface MYClass:NSObject
- (int)maxWithNumber:(int)num1 secondNumber:(int)num2;
@end
@implementation MYClass
/* function returning the max between two numbers */
- (int) maxWithNumber:(int) num1 secondNumber:(int) num2 {
int result;
if (num1 > num2) {
result = num1;
} else {
result = num2;
}
return result;
}
@end
(*** Implementation ***)
int main(int argc, char *argv[]) {
@autoreleasepool {
NSString *myString = @"hello";
NSArray *myArray = [NSArray arrayWithObjects:@"hello", @"world!", nil];
NSDictionary *myDict = @{@"1": @"firstString", @"2": @"secondString", @"3": @"thirdString"};
/* local variable definition */
int a = 100;
int b = 200;
int ret;
MYClass *result = [[MYClass alloc] init];
/* calling a method to get max value */
ret = [result maxWithNumber:a secondNumber:b];
NSLog(@"myString: %@ with lenth: %lu", myString, myString.length);
NSLog(@"myArray: %@ with count: %lu", myArray, myArray.count);
NSLog(@"myDict: %@ with count: %lu", [myDict description], myDict.count);
NSLog(@"my first string of myDict: %@", [myDict objectForKey:@"1"]);
NSLog(@"my first string of myDict: %@", [myDict objectForKey:@"2"]);
NSLog(@"my first string of myDict: %@", [myDict objectForKey:@"3"]);
}
}
In Swift
import Foundation
// functions
func max (firstNumber: Int, secondNumber: Int) -> Int {
var ret = 0
if (firstNumber > secondNumber) {
ret = firstNumber
}
else {
ret = secondNumber
}
return ret
}
var myString = "hello" // 5 character
var myArray = ["hello","world!"] // 2 object
var myDict:[Int:String] = [1:"firstString", 2:"secondString", 3:"thirdString"]
var res = max(100, 200)
print("myString: \(myString) with count: \(myString.count)")
print("myArray: \(myArray) with count: \(myArray.count)")
print("my first string of myDict: \(String(describing:myDict[1]))")
print("my second string of myDict: \(String(describing:myDict[2]))")
print("my third string of myDict: \(String(describing:myDict[3]))")
print("The max between two numbers is: \(res)")
NSLog("myString: %@ with count: %lu", myString, myString.count)
NSLog("myArray: %@ with count: %lu", myArray, myArray.count)
NSLog("myDict: %@ with count: %lu", myDict, myDict.count)
In AppleScriptObjC
use framework "Foundation"
use scripting additions
set myString to current application's NSString's stringWithString:"hello"
set myArray to current application's NSArray's arrayWithArray:{"hello", "world!"}
set myDict to current application's NSDictionary's dictionaryWithDictionary:{a1:"firstString", a2:"secondString", a3:"thirdString"}
log {myString as string, myString's |length|()}
log {myArray as list, myArray's |count|()}
log {myDict as record, myDict's |count|()}
log {(myDict's objectForKey:"a1") as string}
log {(myDict's objectForKey:"a2") as string}
log {(myDict's objectForKey:"a3") as string}
current application's NSLog("myString: %@ with length: %lu", myString, myString's |length|())
current application's NSLog("myArray: %@ with length: %lu", myArray, myArray's |count|())
current application's NSLog("%@ %lu", myDict, myDict's |count|())
current application's NSLog("%@", myDict's objectForKey:"a1")
current application's NSLog("%@", myDict's objectForKey:"a2")
current application's NSLog("%@", myDict's objectForKey:"a3")