Hungry Mind
Hungry Mind

Reputation: 113

osascript is very slower than Script Editor

First of all, I admit that I'm starting with a new project with JXA (Javascript automation for mac os) without having much understanding about AppleScript.

Currently, I'm trying to run following command using JXA:

Application("System Events").processes.windows.name()

First, I used Script Editor to run it. It worked fine and I got the output quickly enough.

However, according to my use case, since I want to get the output of this code frequently from one of my bash script, I tried to execute it using osascript as follows

osascript -l JavaScript -e 'Application("System Events").processes.windows.name()' 

But this time, it took few seconds to print the result in the console.

Now my question is why it takes too much time to execute the same script in osascript compare to Script Editor? Is there any way to optimize the performance of it?

Upvotes: 1

Views: 959

Answers (2)

JMichaelTX
JMichaelTX

Reputation: 1807

Here's the JXA solution:

var winList = Application("System Events").processes.whose({backgroundOnly: {'=': false} }).windows.name();

var winList2 = winList.reduce(
  function(accumulator, currentValue) {
    return accumulator.concat(currentValue);
  },
  []
);
winList2 = winList2.filter(e => (e !== ""));
winList2.join(',')

There may be a better JavaScript from those JavaScript masters.

Upvotes: 1

JMichaelTX
JMichaelTX

Reputation: 1807

This is not exactly the answer to your question, but one issue your JXA script has is that it is pulling in ALL processes (which can be a huge number), when all you probably need is those processes which are visible apps. So, let's start with that.

Here's the AppleScript to get a list of all non-empty window names, in a CSV list on one line, of all visible apps:

tell application "System Events"
  set appList to (every application process whose background only is false)
  set winList2 to {}

  repeat with oApp in appList
    set winList to (name of every window in oApp whose name is not "")
    set winList2 to winList2 & (items of winList)
  end repeat

end tell

set AppleScript's text item delimiters to ","
set winListText to winList2 as text
return winListText

-->All Notes,Keyboard Maestro Editor,macos - osascript is very slower than Script Editor - Stack Overflow - Google Chrome - JMichael,Untitled 2.scpt,Untitled 2

This should not be that hard to convert to JXA, but if you are going to just run it as is in a shell script using osascript, I see no advantage to converting to JXA.

I don't know the nature of your workflow, but if it were me I'd run this as a compiled script file (.scpt), and execute your bash script using the AppleScript do script (or JXA doScript()) command.

It would also be faster if you used a .scpt file with the osascript command.

I will continue to work on this script and convert it to JXA, for my own benefit if not yours.

I hope you find this useful. If not, maybe someone else will.

Questions?

Upvotes: 1

Related Questions