Jump to content
Caleb Grove

Using JavaScript for Automation

Recommended Posts

I've been playing around with the new JavaScript for Automation in Yosemite, and I must say that I've been smitten. It so much more predictable than AppleScript. However, I'm a noob when it comes to scripting in general.

So, on that note, does anyone know how I should be implementing JavaScript into my Alfred workflows? It won't run in a NSAppleScript action, nor will it run if I put in in a Run Script action with the language set to osascript. I was able to get it to work if I placed the script inside the workflow folder and ran it with a bash command, but then how do I grab the arguments in the script?

Thanks!

Edited by Caleb Grove

Share this post


Link to post

I've been playing around with the new JavaScript for Automation in Yosemite, and I must say that I've been smitten. It so much more predictable than AppleScript. However, I'm a noob when it comes to scripting in general.

So, on that note, does anyone know how I should be implementing JavaScript into my Alfred workflows? It won't run in a NSAppleScript action, nor will it run if I put in in a Run Script action with the language set to osascript. I was able to get it to work if I placed the script inside the workflow folder and ran it with a bash command, but then how do I grab the arguments in the script?

Thanks!

 

Either wait for a new release that supports it or run it using the shell 'osascript' command.

Share this post


Link to post

Either wait for a new release that supports it or run it using the shell 'osascript' command.

Hi, I use JavaScript for Automation and run it using the shell 'osascript' command, but I don't know how to get the arguments in the script.

Share this post


Link to post

Hi, I use JavaScript for Automation and run it using the shell 'osascript' command, but I don't know how to get the arguments in the script.

OK, I have solved the problem, it looks like this:

function run(arg){
    console.log(arg);
}

I never wrote applescript and I know nothing about it. I read the document from this url, and I found that the run function. It solved my problem.

 

My English is very bad, I hope you can get it!

Share this post


Link to post

Xiabai,

Close, but not quite close enough. The problem with the <code>run()</code> function is that it requires that the script to be saved as an application rather than a script. That actually makes is rather difficult to run with arguments, unless you like a new terminal window popping up every time you run the workflow.

But yeah, that function is basically what I'm looking for, though in a way that can be used in a regular .scpt file.

Share this post


Link to post

Theoretically, if you can't wait for the Alfred update, you could create a "runner script" in another language that would accept {query} from Alfred, write it into your JavaScript, then run it with osascript.

Share this post


Link to post

I think if you want to use the JS eval() function, you kinda have to do it from JavaScript.

 

What I mean would be to read the script into a variable and then do 

set the_js to (whatever the code is to read a file into a variable in applescript)
"eval(" + the_js ");" 

from inside an AppleScript. Granted, I haven't played around with js inside AppleScript yet, so this might be a bust.

Share this post


Link to post

That should work. That's what I was getting at in my previous post.

 

Okay, I though that you were mentioning that my previous reply wouldn't work (the one right above yours that says almost the exact same thing). Hence, my clarification.

Share this post


Link to post

I was referring to the post before the post that you're referring to. The post above my post that says almost the same thing is not the post I was referring to, but rather the post before the post above my post that says almost the same thing as the post I made that isn't the post I'm referring to.

 

It would be silly to refer to a post that said the same as the post that preceded the post I would, in this case, be referring to.

 

Which is why I'm referring to the post I made before that post. The one that didn't say the same as the post that you think says the same as the post I'm not referring to. Which is not the post I meant.

Share this post


Link to post

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
×