2
  • AppleScript / JXA doesn't return object result on doJavascript

  • BugsDesktopDone

Steps to reproduce:

Open Script Editor from Apple
Create a new Script choose Javascript
Write

      const system = Application('System Events');
      const process = system.processes.whose({ name: 'Orion' })[0];
      const appName = process.displayedName();
      const app = Application(appName);
      const window = app?.windows?.[0];
      const tab = window.currentTab();
     console.log(app.doJavaScript('(() => { return {}})()', { in: tab }))

Expected behavior:
To return {} like in Safary

Orion, OS version; hardware type:

Version 0.99.124.4.1-beta (WebKit 616.1.14.11.1)

  • dino replied to this.

    castroCrea It's behaving same as Safari.

    The reason behind not returning JS output, is webpage security from external attacks.

      It's behaving same as Safari.
      I'm sorry but you are wrong, example below. Please try by yourself. I don't send errors without checking.

      If you need interpretation, Safari returns an object when Orion returns an empty string.

      The reason behind not returning JS output, is webpage security from external attacks.
      This is your choice, but it doesn't behave the same as Safari, and we can still use JSON.stringify first and then JSON.parse. JXA or AppleScript returns an object without any function if this is your point. There is not more security issue, to return an object than a string.

      Code for you to try:

      const system = Application('System Events');
            var process = system.processes.whose({ name: 'Safari' })[0];
            var appName = process.displayedName();
            var app = Application(appName);
            var window = app?.windows?.[0];
            var tab = window.currentTab();
           console.log('safari', app.doJavaScript('(() => { return {}})()', { in: tab }))
      	 
      	 
            var process = system.processes.whose({ name: 'Orion' })[0];
            var appName = process.displayedName();
            var app = Application(appName);
            var window = app?.windows?.[0];
            var tab = window.currentTab();
           console.log('Orion', app.doJavaScript('(() => { return {}})()', { in: tab }))

        It's behaving same as Safari.

        I'm sorry but you are wrong, example below. Please try by yourself. I don't send errors without checking.

        If you need interpretation, Safari returns an object when Orion returns an empty string.

        The reason behind not returning JS output, is webpage security from external attacks.

        This is your choice, but it doesn't behave the same as Safari, and we can still use JSON.stringify first and then JSON.parse. JXA or AppleScript returns an object without any function if this is your point. There is not more security issue, to return an object than a string.

        Code for you to try:

        const system = Application('System Events');
              var process = system.processes.whose({ name: 'Safari' })[0];
              var appName = process.displayedName();
              var app = Application(appName);
              var window = app?.windows?.[0];
              var tab = window.currentTab();
             console.log('safari', app.doJavaScript('(() => { return {}})()', { in: tab }))
        	 
        	 
              var process = system.processes.whose({ name: 'Orion' })[0];
              var appName = process.displayedName();
              var app = Application(appName);
              var window = app?.windows?.[0];
              var tab = window.currentTab();
             console.log('Orion', app.doJavaScript('(() => { return {}})()', { in: tab }))

          castroCrea Thanks for more context about the issue.

          Like the initial description had errors, where it shown return as {} where expected return was [object Object], and we accidentally ended-up just comparing last result which is undefined

          But as you shared new post with additional information, we were able to reproduce the issue, and got a fix 🙂

            a month later
            No one is typing