Jump to content

How to pass a file name as an argument?


Recommended Posts

I have just created a few workflow with the tail command in zsh. 

 

I was looking for a workflow that would take the name of the file as an argument so I could

 

1°) type a keyword,

2°) the files of the scope I'd defined would appear,

3°) hit enter on the file I selected.

 

The result of my query (tail -1 /Name/Of/The/file.txt) would appear in a large type output.

 

I am stuck in the 2°) part.

 

 

Link to comment

@deanishe the scope is the user "Downloads" folder (/Users/Username/Downloads).

 

I tried to define it with a file filter. I don't know how to code a script for a script filter. 

 

I have looked at the Text Tools workflow of Iankybutmacho. I think it does something similar to what I want in that it filters files of a scope defined by the user.  

Link to comment

@deanishe thanks for the tip. 

 

I religiously followed your advice about the escaping options. I am not sure I really do understand what the escaping are all about. Do escaping = escape characters?

(Question of a noob I know). I am really interested in learning more about them because I planned creating more workflows with Run script actions (currently building a workflow with system_profiler). 

Link to comment

Essentially, Alfred edits your code before running it (when it replaces {query}), and the Escaping options tell Alfred how it should add user input to your code in a way that's compatible with the language. Which options you need depends on which characters have special meaning in the language you're using. (I think the next version of Alfred will come with good defaults for each language.)
 
Escaping just means putting a backslash in front of a character (which tells the language to interpret it differently). For example, if I want to print a double quote, I have to use print("\""), not print("""), which is a syntax error. Similarly, in bash echo "$HOME" will print the path to your home directory. To get a literal dollar sign in bash, you escape the dollar, e.g. echo "\$HOME = $HOME".
 
Normally, you'll want {query} assigned to a variable, so you write query = "{query}" in your script. Alfred's going to insert user input between those double quotes, so you to tell it to escape Double Quotes, so it doesn't insert any more literal double quotes between those two (which would be a syntax error, like in the above print() example). You also need to escape Backslashes because you don't want Alfred to insert a backslash just before your closing quote, causing another syntax error (or insert them anywhere, really).
 
That's enough for Python, as its strings aren't very magical.
 
With PHP and bash, you normally also escape Dollars, as those languages will try to expand variables within double quotes (like in the $HOME example above). That is to say, if a user enters "Price: $tbd" and Alfred inserts it unchanged into your script, PHP and bash will replace $tbd with either the value of the variable $tbd if it's set or an empty string if not (leaving "Price: "). This typically isn't the behaviour you want, so you select escape Dollars, your code ends up as "Price: \$tbd", and bash/PHP interpret it as a literal dollar sign and some characters, not a variable name.
 
With bash, you also usually want Backquotes selected because bash will treat strings in backticks as commands and try to execute them.
 
The other options (Spaces, Brackets, Semicolons) are only interesting if you don't enclose {query} in quotes.
 
Does that help?

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...