I am trying to move a button by offsetting its frameOrigin from its old value, but I can’t figure out how to modify the individual values of frameOrigin. If I do the following:
set oldOrigin to sender’s frameOrigin
log oldOrigin
I get this in the console: NSPoint: {192,266}
How do I access these values so I can modify them? In ObjectiveC you seem to be able to use senderFrame.origin.x to access the x value of the origin, but how would you do this in ASOC?
set {oldFrame} to sender's frameOrigin as list
set xItem to x of oldFrame
set yItem to y of oldFrame
set newFrame to {(x of oldFrame) + 10, y of oldFrame}
log newFrame
Craig,
Thanks for your reply - that worked great. I wonder if you could expound a little on the syntax you suggested. I don’t ever remember seeing anything like:
set {oldFrame} to …
Why does the variable name have curly braces around it? Is that regular Applescript syntax (if so that’s something I missed somehow)? Also, since you are referring to the x and the y of oldFrame, that makes it seem like oldFrame is a record rather than a list. If I do a log on oldFrame I get:
{
x=192;
y=266;
}
I don’t know what that is - looks kind of like a C struct rather than a record.
You can treat an NSPoint as a list, but it seems to be a C struct that coerces to a record. A record seems safer than a list to me, and once you get to NSRects, which seem to translate to a list of records, you don’t have to worry about remembering order if you use records.
Try something like this:
set {x:x1, y:y1} to sender's frameOrigin() as record
set x1 to x1 + 10
sender's setFrameOrigin_({x:x1, y:y1})
Shane,
I tried your code, but I’m having a problem with typing the x. The editor keeps capitalizing it (and putting pipes around it doesn’t help). The button moves all the way to the left, which I presume means that the x value is either 0 or maybe undefined somehow. If I change the second line to set y1 to y1 - 20, the button does move the correct amount in the y direction (but still all the way to the left in the x direction). Interestingly, in Craig’s code the code piece “x of oldFrame” works fine even though the editor is capitalizing that x as well. Any thoughts on how to get the editor to stop doing this? I don’t have any other x’s in my program, and I’ve tried quitting Xcode and restarting but that didn’t work.
Pipes should fix it. The only other suggestion is to close the project, open the .applescript file in a text editor, make the case change, and try again.