Jump to content

Writing Scripts in JS? Or is it JXA?


Recommended Posts

I’m new and most familiar with JavaScript, and would like to create my own workflows.

 

If I create a basic index.js file in the Workflow folder, am I able to write normal JS? I wasn’t able to get a basic DOM manipulation function or console.log to work in a basic workflow (page was loaded from the Open URL object, but the single function produced no results or errors when trying to replace the header text, for example. 

 

Am I thinking about this wrong? Any guidance or reference material is truly appreciated.  

Edited by Anthony B
Link to comment

Hi, @Anthony B, welcome to the forum.

 

3 hours ago, Anthony B said:

Am I thinking about this wrong?

 

Yup, sounds like it.

 

Things like window and document are part of the API provided by browsers, not part of JS. There is no browser or event loop, so there is no DOM or setTimeout or XHR.

 

console.log does work, but I suspect you're either looking in the wrong place for the output or are confused about how Alfred runs scripts. console.log writes to STDERR, which is displayed in Alfred's debugger, but it's possible your script isn't actually being run. It sounds like you've just named your file index.js and are expecting Alfred to run it. That may be how CommonJS/RequireJS work, but you're not using them here. You're writing a UNIX command-line script here.

 

You need to run it from an Alfred Run Script action, either by pasting your script in the Script box and setting language to /usr/bin/osascript (JS) or setting language to /bin/bash and entering /usr/bin/osascript -l JavaScript index.js in the Script box (assuming your script is still called index.js). You can also use Language = External Script, but then you need to give your script a shebang and make it executable in classic UNIX style.

 

Finally, the Open URL action opens the URL passed to it in your browser. It does not retrieve a URL and pass the HTML (or whatever) to your script. There is no output from this action.

 

I hope that helps.

 

If you have any further questions, it would be helpful if you could upload your workflow somewhere and post a link. That way, we can see exactly what's wrong rather than trying to guess based on a description that might not include all the relevant information.

Edited by deanishe
Link to comment

@deanishe thank you very much for the quick reply! You’re absolutely right - I was treating Alfred as the browser, so it makes sense now why I was having issues.

 

That direction helps a lot. I’m sure I’ll get stuck plenty, but now I’ve got a better understanding of where to start. 

Link to comment
10 hours ago, deanishe said:

[...] or are confused about how Alfred runs scripts. [...]

 

If you have any further questions, it would be helpful if you could upload your workflow somewhere and post a link. That way, we can see exactly what's wrong rather than trying to guess based on a description that might not include all the relevant information.

 

@deanishe You're definitely right, here.

 

I know this is more a support forum than a "welcome programmer newbies" forum, but would you have any recommended resources for understanding how Alfred runs scripts (you mentioned 'Classic UNIX-style' which piqued my curiosity)? I saw another post where you mentioned less-than-ideal maintainability/distribution from workflow-helpers like Alfy, so I'd like to ensure I'm approaching my efforts in the best way possible.

 

Thanks again for your help.

 

 

Link to comment
53 minutes ago, Anthony B said:

but would you have any recommended resources for understanding how Alfred runs scripts (you mentioned 'Classic UNIX-style' which piqued my curiosity)?

 

Not specifically, no. Essentially, Alfred runs scripts in exactly the same way you run them in a shell (e.g. in Terminal). Input (i.e. the user query, selected files) is passed via ARGV, workflow variables are provided as environment variables, JSON feedback is sent to STDOUT and log message to STDERR.

 

Translated to a fairly simple set of instructions for JXA, that means the following:

 

Save your JavaScript file in the workflow folder (we'll assume it's called script.js) and run it using a Run Script action with Language = /bin/bash and /usr/bin/osascript -l JavaScript script.js in the Script box. The basic template (taken from Alfred) is:

function run(argv) {
    // first command-line argument is the user's query
    var query = argv[0]
    // log query to STDERR (i.e. Alfred's debugger)
    console.log(`query=${query}`)
  
    // do your thing here
 
    // Output Alfred feedback JSON to STDOUT
    return JSON.stringify({items: [
      {title: "Example item"}
    ]})
}

To retrieve workflow/environment variables, see this tutorial for a general overview and JXA-specific instructions.

 

1 hour ago, Anthony B said:

less-than-ideal maintainability/distribution from workflow-helpers like Alfy

 

That's true, but it only applies to Alfy-based workflows (and Node-based workflows in general to a lesser extent), not to workflow helpers in general, nor to JXA-based workflows like you're trying to write.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...