Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Using CoreData with AppleScript Studio



I'll show some sample scripts to access Core Data using "call method".
This is just an experiment. I don't think that this is a good idea...

Download "EventManager.zip" from this URL.
http://homepage.mac.com/ssfactory/

The original is Apple's Core Data example.
(/Developer/Examples/CoreData/EventManager/)
I changed it into ASS application.
Open "CranberryFestival.events" in "Sample Event Files" folder with
EventManager.app, then try following AppleScripts.

1. sample script for getting all values of all objects.

tell application "EventManager"
    set managedObjectContext to call method "managedObjectContext" of document 1
    set registeredObjects to call method "registeredObjects" of managedObjectContext
    set allObjects to call method "allObjects" of registeredObjects
    set allRecords to {}
    repeat with theObject in allObjects
        set theEntity to call method "entity" of theObject
        set allKeys to call method "allKeysOfEntity:" of document 1 with parameter theEntity
        set allValues to {}
        repeat with theKey in allKeys
            set theValue to call method "primitiveValueForKey:" of theObject with parameter theKey
            try
                theValue
            on error -- set missing value when failed to get primitive value
                set theValue to missing value
            end try
            set end of allValues to theValue
        end repeat
        set end of allRecords to call method "dictionaryWithObjects:forKeys:" of class "NSDictionary" with parameters {allValues, allKeys}
    end repeat
    return allRecords
end tell

2. sample script for getting values of 'Event' object whose 'name' contains 're'.

tell application "EventManager"
    set managedObjectContext to call method "managedObjectContext" of document 1
    set entityDescription to call method "entityForName:inManagedObjectContext:" of class "NSEntityDescription" with parameters {"Event", managedObjectContext}
    set predicate to call method "predicateWithFormat:" of class "NSPredicate" with parameter "name contains 're'"
    set foundObjects to call method "executeQueryWithEntity:predicate:" of document 1 with parameters {entityDescription, predicate}
    set allKeys to call method "allKeysOfEntity:" of document 1 with parameter entityDescription
    set allRecords to {}
    repeat with theObject in foundObjects
        set allValues to {}
        repeat with theKey in allKeys
            set theValue to call method "primitiveValueForKey:" of theObject with parameter theKey
            try
                theValue
            on error -- set missing value when failed to get primitive value
                set theValue to missing value
            end try
            set end of allValues to theValue
        end repeat
        set end of allRecords to call method "dictionaryWithObjects:forKeys:" of class "NSDictionary" with parameters {allValues, allKeys}
    end repeat
    return allRecords
end tell

Hope this helps.

SSFactory
<email@hidden>
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-studio mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/applescript-studio/email@hidden

This email sent to email@hidden



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.