magobaol Posted August 1, 2021 Share Posted August 1, 2021 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 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: and then I modify the Script Filter Object like this: I get this error: 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
deanishe Posted August 1, 2021 Share Posted August 1, 2021 (edited) 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 August 1, 2021 by deanishe Link to comment
magobaol Posted August 1, 2021 Author Share Posted August 1, 2021 Got it! Thanks a lot @deanishe ! Link to comment
AlexMaiburg Posted February 19, 2023 Share Posted February 19, 2023 (edited) 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 February 19, 2023 by AlexMaiburg Link to comment
vitor Posted February 19, 2023 Share Posted February 19, 2023 (edited) 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 February 19, 2023 by vitor Link to comment
AlexMaiburg Posted February 19, 2023 Share Posted February 19, 2023 (edited) Hi @vitor, thanks for your help. Yes, you're right. I mistyped $ for %. My bad. But I still don't get it with the $HOME variable. How can I use it in the configuration? When I use the "evil" eval then it works. Edited February 19, 2023 by AlexMaiburg Link to comment
vitor Posted February 19, 2023 Share Posted February 19, 2023 (edited) 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 February 19, 2023 by vitor Link to comment
AlexMaiburg Posted February 19, 2023 Share Posted February 19, 2023 Yes, file picker, of course! Sometimes it can be so easy 🙂 Thanks! Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now