Jul-25-2020, 03:38 PM
Hi all
Sorry in advance if this is a basic question - new to python over the recent lockdown and have never ever used objective C...
To start with - this is not homework but a personal project i am trying to do to develop an understanding of python and the bridge to running things on a Mac
I am looking to return a list of data from an objective-C object generated in python:
Briefly the aim of the project is to listen for a track change and then do some processing with the data depending on the track genre etc.
I have managed to get code to:
run an applescript whilst listening for track changes form the music app
print the information to the console - thanks to this link: https://gist.github.com/codiez/260617
However when I have tried to access the information outside the class it says it is not defined:
Sorry in advance if this is a basic question - new to python over the recent lockdown and have never ever used objective C...
To start with - this is not homework but a personal project i am trying to do to develop an understanding of python and the bridge to running things on a Mac
I am looking to return a list of data from an objective-C object generated in python:
Briefly the aim of the project is to listen for a track change and then do some processing with the data depending on the track genre etc.
I have managed to get code to:
run an applescript whilst listening for track changes form the music app
print the information to the console - thanks to this link: https://gist.github.com/codiez/260617
However when I have tried to access the information outside the class it says it is not defined:
Error:AttributeError: 'getSongInfo' object has no attribute 'this_song_info'
Below are the methods I have tried to use to define the information as a class attribute and return it every time the notification observer reacts:class getSongInfo(NSObject): #attempt 3 - make it a global variable so it is returned whenever global this_song_info this_song_info = [None]*5 def init(self): self = super ( getSongInfo, self ).init() nc = Foundation.NSDistributedNotificationCenter.defaultCenter () nc.addObserver_selector_name_object_ ( self, 'getMyData:', 'com.apple.Music.playerInfo', None ) return self # attempt 1 - run a method after calling the observer - fails # def run_script(self): # my_script = applescript.AppleScript (path_to_script) # self.this_song_info = my_script.run() # return self # attempt 2 - make it a property by running the above code this_song_info=property(run_script) def getMyData_(self, data): my_script = applescript.AppleScript ( path_to_script ) #attempt 4 - assign to self and return every time this pyobjc method is called self.this_song_info = my_script.run() return self return this_song_info GetData = getSongInfo.new () print(GetData.this_song_info) AppHelper.runConsoleEventLoop()I also use the following imports:
from PyObjCTools import AppHelper import objc from Foundation import * import applescript