-
Notifications
You must be signed in to change notification settings - Fork 5
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
Rewrite with ScriptingBridge (was Rewrite with PyXA) #16
Comments
Actually, might be able to implement this with ScriptingBridge without the overhead of PyXA (which looks awesome but still immature and has some weird dependencies: python==3.11, numpy, all of pyobjc) python
^[[APython 3.11.0 (main, Dec 25 2022, 14:21:02) [Clang 12.0.0 (clang-1200.0.32.29)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import ScriptingBridge
>>> from ScriptingBridge import SBElementArray
>>> app = ScriptingBridge.SBApplication.alloc().initWithBundleIdentifier_("com.apple.Notes")
>>> notes = app.accounts()[1].notes()
>>> len(notes)
394
>>> import AppKit
>>> predicate = AppKit.NSPredicate.predicateWithFormat_("name contains[cd] %@", "cocktail")
>>> predicate
name CONTAINS[cd] "cocktail"
>>> results = notes.filteredArrayUsingPredicate_(predicate)
>>> len(results)
4
>>> names = [results.arrayByApplyingSelector_("name")]
>>> names
[(
"You\U2019ve Never Heard of One of the Bahamas\U2019 Most Popular Cocktails",
"Watermelon Mint Mojito Cocktails - Creative Culinary",
"Gin Basil Smash cocktail",
"Tequila Sunrise Cocktail"
)]
>>> ids = [results.arrayByApplyingSelector_("id")]
>>> ids
[(
"x-coredata://BA47E88C-3599-48BE-88DD-5CBC118E9CE6/ICNote/p1038",
"x-coredata://BA47E88C-3599-48BE-88DD-5CBC118E9CE6/ICNote/p1030",
"x-coredata://BA47E88C-3599-48BE-88DD-5CBC118E9CE6/ICNote/p659",
"x-coredata://BA47E88C-3599-48BE-88DD-5CBC118E9CE6/ICNote/p661"
)]
>>> note = notes.filteredArrayUsingPredicate_(AppKit.NSPredicate.predicateWithFormat_("id == %@", ids[0][0]))
>>> len(note)
1
>>> note[0].name()
'You’ve Never Heard of One of the Bahamas’ Most Popular Cocktails'
>>> |
Making progress on this but looks like I can't get rid of the AppleScript completely. There are some conditions (e.g. using selection on Catalina or using a predicate) where the ScriptingBridge sets the object ID to 0. I haven't been able to figure out why but in this case, the id can be determined by examining the string representation of the object which looks like this: Using this, I can get the Note ID then use the existing AppleScript calls to get the note details. This isn't elegant but works in all cases I've been able to test. |
I just looked into this for PyXA (thanks for the feedback btw!). It seems to be an issue with how PyObjC's lazy evaluation of SBObjects works when using predicates -- it ends up evaluating the object at its index within the result of the filtered array. E.g., if |
@SKaplanOfficial Thanks. I saw that you did call |
This looks like a much better way of interacting with Notes: PyXA
As an example:
Prints titles of all notes instantly
The text was updated successfully, but these errors were encountered: