アプリで AppleScript の Make New コマンドをサポートできますが、返されたコア データ管理オブジェクトの「指定されたオブジェクト」(NSUniqueIDSpecifier) は役に立ちません。次の AppleScript は、エラー メッセージを返します。
エラー「SpellAnalysis でエラーが発生しました: 無効なキー形式です。」レベル ID "x-coredata:///Levels/tC5A49E01-1CE1-4ED6-8F6B-BC0AE90E279A2" からの数値 -10002
tell application "SpellAnalysis"
set thisLevel to make new «class Slev» with properties {«class Saln»:3}
get properties of thisLevel
end tell
そのため、新しく作成された Levels オブジェクトは AppleScript で処理できません。これに対する解決策を求めて Web をくまなく調べたところ、最も近いものは Bill Cheeseman のサンプル アプリ、「WareroomDemo」であり、特に Core Data アプリの Cocoa Scriptability を扱っています (Sketch の例では Core Data を使用していません)。残念ながら、これは古い例であり、64 ビットより前の XCode でのみ実行され、実際に実行することはできません。コードを見ることしかできません。彼のアプリの Make Command には、私が知る限り、同じ制限がある可能性があります。
返された 'objectSpecifier' は、作成されたオブジェクトを参照できません。これは、コア データの編成スキームが破損しないようにするためか、または返されたオブジェクトがキャッシュされていない '障害' である可能性があります。(マネージド オブジェクトのプロパティ値を取得することで) フォールトを強制的にキャッシュできるため、後者の可能性は低いと思いますが、AppleScript で同じエラー メッセージが表示されます。
私のクラスを作成するメソッドは次のとおりです。
- (id)newScriptingObjectOfClass:(Class)class forValueForKey:(NSString *)key withContentsValue:(id)contentsValue properties:(NSDictionary *)properties { // Creates a new Lesson object in response to the AppleScript 'make' command.
// Documentation for 'newScriptingObject…' states that to create a new class object when using Core Data, you intercede using the following method (or you can subclass the NSCreateCommand's 'performDefaultImplementation' method and put your NSManagedObject init code there):
if (class == [Levels class]) {
//NSLog(@"class: %@",class);
NSEntityDescription *levelsEntity = [NSEntityDescription
entityForName:@"Levels"
inManagedObjectContext:levelsDBase];
NSManagedObject *levelObject = [[NSManagedObject alloc] initWithEntity:levelsEntity insertIntoManagedObjectContext:levelsDBase];
SLOG(@"lessonObject: %@", lessonObject);
NSString *levelNumberString = [[properties objectForKey:@"levelNumber"] stringValue];
SLOG(@"levelNumberString: %@", levelNumberString);
[levelObject setValue:levelNumberString forKey:@"levelNumber"];
return levelObject; // When using Core Data, it seems that you return the newly created object directly
}
return [super newScriptingObjectOfClass:(Class)class forValueForKey:(NSString *)key withContentsValue:(id)contentsValue properties:(NSDictionary *)properties];
}
これが私のオブジェクト指定子メソッドです:
- (NSScriptObjectSpecifier *)objectSpecifier {
// This NSScriptObjectSpecifiers informal protocol returns a unique ID specifier specifying the absolute string of the URI representation of this managed object. // AppleScript return value: 'level id <id>'.
// The primary container is the application.
NSScriptObjectSpecifier *containerRef = nil; // I understand that if the application is the container, this is value you use for the container reference
NSString *uniqueID = [[[self objectID] URIRepresentation] absoluteString];
return [[[NSUniqueIDSpecifier alloc] initWithContainerClassDescription:[NSScriptClassDescription classDescriptionForClass:[NSApp class]] containerSpecifier:containerRef key:@"levelsArray" uniqueID:uniqueID] autorelease];
}