Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create axvalue_to_pytuple to convert AXValueRef to tuples #163

Merged
merged 2 commits into from Feb 2, 2019

Conversation

daveenguyen
Copy link
Contributor

I was looking into issue #162 and discovered some things.

pyobjc documentation says "AXValueGetValue is not yet supported, it requires a manual wrapper."
This is a problem because AXValueGetValue was how the original c-extension of atomac was getting the value.

This PR implements a workaround by parsing the string of AXValueRef object to get the values and return a tuple like it was before.

snippet used in c-extension:

/*
*-----------------------------------------------------------------------------
*
* CGValueToPyTuple --
*
* Convert a CG value (CGSize, CGPoint, or CFRange) to Python tuple
*
* Results:
* Returns a Python Tuple or NULL and a Python exception on
* failure.
*
* Side effects:
* Ownership of the Python Tuple object is transferred to the caller.
*
*-----------------------------------------------------------------------------
*/
PyObject *
CGValueToPyTuple(AXValueRef value) //IN: AXValueRef to convert
{
PyObject *tuple = PyTuple_New(2);
if (kAXValueCGSizeType == AXValueGetType(value)) {
CGSize size;
double float1 = 0.0;
double float2 = 0.0;
if (AXValueGetValue(value,kAXValueCGSizeType,&size) == 0){
return NULL;
}
float1 = (double)size.width;
float2 = (double)size.height;
PyTuple_SetItem(tuple,0,Py_BuildValue("d",float1));
PyTuple_SetItem(tuple,1,Py_BuildValue("d",float2));
return tuple;
}
if (kAXValueCGPointType == AXValueGetType(value)){
CGPoint point;
double float1 = 0.0;
double float2 = 0.0;
if (AXValueGetValue(value,kAXValueCGPointType,&point) == 0){
return NULL;
}
float1 = (double)point.x;
float2 = (double)point.y;
PyTuple_SetItem(tuple,0,Py_BuildValue("d",float1));
PyTuple_SetItem(tuple,1,Py_BuildValue("d",float2));
return tuple;
}
if (kAXValueCFRangeType == AXValueGetType(value)){
CFRange range;
long index1 = 0;
long index2 = 0;
if (AXValueGetValue(value,kAXValueCFRangeType,&range) == 0){
return NULL;
}
index1 = range.location;
index2 = range.length;
PyTuple_SetItem(tuple,0,Py_BuildValue("l",index1));
PyTuple_SetItem(tuple,1,Py_BuildValue("l",index2));
return tuple;
}
// @@@TODO: Need to set a python exception here if not already set
return NULL;
}

@daveenguyen
Copy link
Contributor Author

Using description() (objective-c's equivalance to __repr__) in conjunction with NSSizeFromString, NSPointFromString, NSRangeFromString from Foundation framework to get the value.
Then casting to tuple

@nagappan nagappan merged commit a170717 into pyatom:master Feb 2, 2019
@daveenguyen daveenguyen deleted the bugfix/axvaluepytuple branch March 14, 2019 03:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants