Mailing Lists: Apple Mailing Lists

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

SOLVED: Changing AppleScript Records with Variable Identifiers



A little dip into Obj-C Cocoa solves AppleScript's limitation on accessing
the contents of records via the property reference form only (e.g. for a
record {label1:"foo", label2:"bar"} you can only use 'label1 of thatRecord'
and not something like 'property of thatRecord whose label is "label1"' to
talk to it).

Add these Obj-C methods to your Utility class (Which I usually call
OBCUtility, so I'll use that name in this example) and call them as
illustrated in the AppleScript below.  Note that I use AppleScript
terminology instead of Cocoa terms in the method names to be clear as to
what we are doing, since this is going to be called to make changes to
AppleScript records (which bridge to NSDictionary).

----------

- (id *)getValueForLabel:(NSString *) theLabel ofRecord:(NSDictionary
*)theRecord;
{
     return [theRecord objectForKey:theLabel];
}

- (NSDictionary *)setRecord:(NSDictionary *)theRecord label:(NSString *)
theLabel toValue:(id *) theValue;
{
     NSMutableDictionary mutableRecord = [NSMutableDictionary
dictionaryFromDictionary:theRecord];
     [mutableRecord setObject:theValue forKey:theKey];
     return [autorelease mutableRecord]; //It is autoreleased so you don't
have to monkey with it from AppleScript
}

----------

And in AppleScript (you've instantiated OBCUtility in your nib and assigned
it to a property called myUtilities):

set myRecord to {firstLabel:"foo", secondLabel:"bar"}

set theLabel to "firstLabel"
log (call method "getValueForLabel:ofRecord:" of myUtilities with parameters
{theLabel, myRecord})
--result: "foo"

set theLabel to "secondLabel"
log (call method "getValueForLabel:ofRecord:" of myUtilities with parameters
{theLabel, myRecord})
--result: "bar"

set myRecord to (call method "setRecord:label:toValue:" of myUtilities with
parameters {myRecord,theLabel,"boo"})
--contents of myRecord now: {firstLabel:"foo", secondLabel:"boo"}

-----------

Some will notice that -[OBCUtility getValueForLabel:ofRecord:] just makes
one call and you can do without it by just making that call yourself.  While
that is true, I just wrote the method as a getter mirror for the setter and
to keep the terminology the same between the two (since we are calling
NSDictionary "record"). If want to skip it, replace calls like (call method
"getValueForLabel:ofRecord:" of myUtilities with parameters {theLabel,
myRecord}) with (call method "objectForKey:" of myRecord with parameter
theLabel) instead.

I hope this of use to someone else.

Topher

-----Original Message-----
From: applescript-studio-bounces+tophu=email@hidden
[mailto:applescript-studio-bounces+tophu=email@hidden] On Behalf
Of Adam Raney
Sent: Saturday, June 25, 2005 10:44 PM
To: AppleScriptStudio MailingList
Subject: Re: Changing settings in Plists

In honor of Graham Jones, I am going to re-phrase this a bit.
Essentially, I need to know how to change the value of a record by
calling it's key as a string.

I.E., instead of:

set myRecord's fileName to "file.doc"

I would have something like:

set theKey to "fileName"
set myRecord's theKey to "file.doc"

Of course, this gets an error. How would I take the theKey variable
(containing a string), and use it to call the key with that name?

On 6/24/05, Adam Raney <email@hidden> wrote:
> First of all, lots of thanks to everyone here for showing me how to
> access plists in ApSS.
> 
> I can load a plist into an AppleScript record without a problem, and
> I can change static objects in the record easily, but I need to be
> able to change objects whose keys are determined dynamically.
> 
> My record is given the name yearData, and it contains school years,
> named "03-04", "04-05", etc, each of which is a dictionary,
> containing another dictionary called "months", which contains string
> keys for every month.
> 
> In my program, the school year and month and both determined
> dynamically. I need to be able to alter the record of these dynamic
> months, but I can't seem to pull it off. If i have a static object to
> change, I can use:
> 
> set yearData's |03-04|'s |months|'s |august| to "aug04.month"
> 
> But I need to be able to dynamically fill in the school year and
> month. I tried:
> 
> run script ("set yearData's |" & schoolYear & "|'s |months|'s |" &
> _month & "| to \"" & fileName & "\"")
> 
> But I get an error that yearData is not defined.
> 
> I have also tried the "setObject:forKey:" call method, but this gets
> no response. I even made sure to load the plist with call method
> "dictionaryWithContentsOfFile:" of class "NSMutableDictionary" ...,
> but still no response.
> 
> Please show me what I'm missing. I am sure it should be a simple
> process.
>
 _______________________________________________
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

 _______________________________________________
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.