Jump to content

Script can't open a file that exists if I use the tilde (~) to refer to my home directory in a workflow variable


Recommended Posts

Hello everyone,

I'm new to Alfred and I love it!

I have a small php script that is in my home folder `~/dev/tag-buddy` and I want to call it from an Alfred workflow.

 

Simple Script Filter configuration that works fine

I've setup a script filter in bash language with this instruction `/usr/local/bin/php ~/dev/tag-buddy/src/search-tags.php "{query}"` and it works fine.

Below you can see the configuration

 

1128035268_Screenshot2021-08-01at18_15_40.png.3d86adf4f6c286db442fe05052feebea.png

 

 

Trying to use a variable to handle the path to php script

 

If I setup a worfklow variable to handle the php script path, like this:

474556114_Screenshot2021-08-01at18_15_52.png.d280a1b8bb07229a989db6ed70665097.png

 

 

 

and then I modify the Script Filter Object like this:

1323366065_Screenshot2021-08-01at18_15_06.png.ee9ffa75de08ab8a47b40ea92fac27a6.png

 

 

I get this error:

alfred-tb-4.thumb.png.9355d1997090162a8fd0da3b3b4b93ce.png

 

So the variable substition has worked fine, since Alfred tries to open the file with the right path, but it doesn't work.

 

However, if in the workflow variable I write the complete path, replacing the ~ with my actual home directory path, the workflow is fine.

 

So it doesn't like when using the ~ in a workflow variable.

Am I doing something wrong?

 

Thanks,

Francesco

 

 

 

Link to comment

Hi @magobaol, welcome to the forum.

 

39 minutes ago, magobaol said:

Am I doing something wrong?

 

Yes. Expanding ~ to your home directory is something that bash (and zsh) does only when it’s directly specified as an argument. If you have a variable with ~ in it, you just have a tilde character.

 

Putting tagbuddypath=~/dev/tag-buddy in your script will work because bash will expand the tilde before setting the variable, but Alfred doesn’t expand tildes in your workflow variables.

 

If you want to be able to specify paths with ~/ in Alfred’s settings, you’ll have to manually expand the tildes in your script. Same goes for other variables (i.e. you can’t use $HOME/Documents in Alfred’s workflow variables table, either).

 

Edited by deanishe
Link to comment
  • 1 year later...

In case someone is interested in a solution for a shell script:

 

Supposed you have the variable file set in your workflow configuration to $HOME/Documents/notes.md then expand it in your script like so:

 

file=(eval "echo $file")

 

Now your script will find the path to file.

Edited by AlexMaiburg
Link to comment

Welcome @AlexMaiburg,

 

33 minutes ago, AlexMaiburg said:

Supposed you have the variable file set in your workflow configuration to %HOME/Documents/notes.md then expand it in your script like so:

 

file=(eval echo $file)

 

The syntax and code are incorrect. Unix uses $HOME (not %HOME) and the given file code won’t execute, it will just create an array of the words. Furthermore, you should avoid eval at all costs when unpredictable input is involved (e.g. a path given by a user) because it can be a security risk.

 

But if you already have ${HOME}, you don’t need any modification. That is the point of it and the correct approach. The issue concerns the tilde character, which in shells is only expanded in certain situations.

 

Edited by vitor
Link to comment
18 minutes ago, AlexMaiburg said:

When I use the "evil" eval then it works.

 

Take another look at your code. I bet you have file=$(eval echo $file) instead of file=(eval echo $file). The $ makes all the difference between executing a command or adding words to an array.


Here’s a simple example why eval is dangerous: set your file variable to ; say hello and then run your code (don’t forget the semicolon). You’ll hear your Mac greeting you. Now imagine that code was instead to delete your files. It’s OK to use eval in select situations, but it should be a last resort. Sanitise your inputs.

 

26 minutes ago, AlexMaiburg said:

But I still don't get it with the $HOME variable.

 

In a Workflow Environment Variable it won’t work, because again it’s the shell which does the expansion. One way to do it is to have it with the tilde in the Workflow Environment Variable then in the bash code do "${variable_name/#\~/${HOME}}", as it will replace a leading tilde with ${HOME}. That’s still not 100% but the cases where it fails are really minor.


However, there’s an even better way. Which is to use Workflow Configuration, specifically the File Picker:

  • It allows ~ but will always expand it to the correct path when reading in the script.
  • Can be set manually, in a much clearer place for users.
  • Can be documented in place (Label, Description).
  • Can be configured with a GUI, by clicking the folder icon.

It has the best of all worlds and the pitfalls of none.

 

Edited by vitor
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...