0

I'm doing an applescript cocoa app and I'd like to run a function as a notification with the name "Finished Downloading" is displayed. The function changes a value displayed in the main window of the cocoa app and the notification is created with a terminal command line.

I've tried to use observers but couldn't really understand how they work so, even if it's probably the right thing to use, I couldn't get it to work.

property NSNotificationCenter : class "NSNotificationCenter"
script AppDelegate
    property parent : class "NSObject"

    on myFunction()
        log "hey"
    end myFunction

    on applicationWillFinishLaunching_(aNotification)
        -- Insert code here to initialize your application before any files are opened
        set ws to workspaceClass's sharedWorkspace()
        set nc to ws's notificationCenter()
        tell nc to addObserver_selector_name_object_(me, "myFunction()", "Finished Downloading", missing value) --tried this
    end applicationWillFinishLaunching_
end script

As the notification named "Finished Downloading" is displayed in the top right corner of the monitor this script should run myFunction

Hope for help, thanks. Alex

EDIT: thanks to the answer of Willeke I now verified that it's not possible to accomplish what I was trying to do. See Observe for new System Notifications OSX

AlexPera
  • 366
  • 5
  • 12
  • Can you show us how the notification is generated, and from where it's getting its name ? – CJK Dec 30 '18 at 23:32
  • It's generated by the terminal with the command: curl -O [link]; osascript -e 'display notification "Finished Downloading"' – AlexPera Dec 31 '18 at 11:36
  • So, the text content of the notification display is "Finished Loading". This does not mean that the name of the notification is "Finished Loading". Moreover, I think the term "notification" is being conflated with `NSNotification`. [This reference page](https://developer.apple.com/documentation/foundation/nsnotificationcenter/1415360-addobserver?language=objc) states that the name of the notification must be of type `NSNotificationName`, a valid list of which can be found [here](https://developer.apple.com/documentation/foundation/nsnotificationname?language=objc). – CJK Dec 31 '18 at 13:41
  • Also, according to the same reference page as before, the selector is required to take precisely one argument, which will be of type `NSNotification`. Therefore, `myFunction()` needs to be redeclared as something more like `on myFunction:notification`, and passed to the `addObserver` method as `"myFunction:"` – CJK Dec 31 '18 at 13:44
  • See [Observe for new System Notifications OSX](https://stackoverflow.com/questions/45593529/observe-for-new-system-notifications-osx) – Willeke Jan 01 '19 at 22:31

0 Answers0