Jump to content

How to get into workflow folder using NSApplescript?


Recommended Posts

Hello.

 

When I use Run Script in my workflows, working directory for script is the workflow directory. It is very convenient.

But with NSApplescript working directory is / (root directory). It makes troubles for me.

 

Is there a reliable way to get (into) workflow directory in NSApplescript?

Yes, I can hard-code it, but (1) it's awfully long and (2) I'm not sure how Alfred generates its name (can it change in future? will it the same on the other installation?).

 

Thanks for you replies.

Link to post
  • 3 weeks later...

But my problem is: I want run NSAppleScript in workflow directory.

Why? In what way is a normal Run Script Action with Language set to /usr/bin/osascript (AS) not acceptable? Especially considering that it does run in the workflow directory by default and is, by all appearances, exactly what you need.

If you give me a reason why only an NSAppleScript will do, I'll tell you how to work out the workflow directory :)

(I'm not going to explain it without a good reason because it's a very messy solution to a problem that is easily solved using a normal Run Script Action, and NSAppleScript is usually a bad choice, anyway.)

Edited by deanishe
Link to post

1. When I do display notification in NSAppleScript I see a notification with nice and meaningful Alfred icon but when I do the same in Run Script Action I see a notification with miserable and irrelevant AppleScript icon.

2. NSAppleScript cache compiled AS. It is not a notable optimization but it's better to do it than not to do.

 

Here is my two reasons, but I do not think the reasons are sufficient to use "a very messy solution". :) There are another ways.

Link to post

Ah, notifications. Yeah, that's a good reason to use NSAppleScript.

The caching/compilation can be achieved by saving your script in a .scpt file, too.

The method I was thinking of to get a workflow's directory from a script (Alfred's environmental variables) actually doesn't work from NSAppleScript, 'cos there are no environmental vars :(

Link to post

Yes, I was investigating the Alfred's environmental variables just now; unfortunately this method doesn't work.

But the environmental variables seems very useful in Run Script Action. Thank you, deanishe, I've overlooked it.

 

I know that *.applescript can be compiled by osacompile to *.scpt. 

But it is unnecessary headache to do it.

Link to post

FWIW, when Alfred's own notifications aren't good enough, most folks use terminal-notifier.

You can set the icon you want, and you don't have the other limitations of NSAppleScript, most notably having to paste your code in the Script box (which is a pain, and messes with version control) and the fact that NSAppleScript runs on Alfred's main thread, blocking Alfred till it finishes.

Edited by deanishe
Link to post

No, I don't like the idea of terminal-notifier, either, but with the notifications API being so crappy, it's probably the best solution if you can't or don't want to use Alfred's Post Notification output.

I do something similarly hacky in my own Python library to allow notifications.

Link to post

If you use JavaScript for Automation (JXA), you can use this to get environment variables:

 

ObjC.import('stdlib');
var alfver = $.getenv('alfred_version').split(".")[0];

var alfapp = "Alfred " + alfver;
var appAlfred = Application(alfapp);
appAlfred.includeStandardAdditions = true;
appAlfred.runTrigger("OpenDir", {
   inWorkflow: "com.customct.AlfredBrowser",
   withArgument: "{query}"
});

 

It is run by oscascript with the "-l JavaScript" flag. This example get the Alfred version for knowing which Alfred to call and external trigger.

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
×
×
  • Create New...