Search code examples
javascriptechoconsole.logosascriptalfred

how can I echo something without newline with osascript in `run script` workflow in alfred?


I wrote some script in alfred workflow with osascript(js). As we known, console.log will left an undefined after echo the log to stdout, we could not use it to pass the result to the workflow outputs like clipboard. And then I found I can use eval or just run a expression to pass something to the stdout. But we known that it would add an newline to the stdout. And then, I got a result tailing with a newline, which was additional. It was not a perfect way to resolve my problem. Is there any other resolutions?

enter image description here

enter image description here

enter image description here

Function of the code could be ignored. Just focus on how to echo without a newline tailing. Thank you.


Solution

  • I can't find a way to use osascript(JS) to do that. But there's another way.

    1. Change script language from osascript(JS) to bash.
    2. Script: get result of your osascript, trim the extra newline.

      osascript -l JavaScript process.js "{query}" | tr -d '\n'
      
    3. Parse your code to process.js in workflow folder. Add code to read arguments from command line. (special thanks to mikaelbr/node-osascript)

      var app = Application.currentApplication();
      app.includeStandardAdditions = true;
      
      ObjC.import('Cocoa');
      var args = ObjC.deepUnwrap($.NSProcessInfo.processInfo.arguments).slice(4);
      
      var query = args[0].split(' ');
      var set1 = query[0].split(',');
      var set2 = query[1].split(',');
      var res = set1.filter(function (id,index){
          return set2.indexOf(id) != -1;
      })
      res.join(query[2] || ',')