Jump to content

Create and open new file


Recommended Posts

Hi there, long time Alfred user, first time poster. 

 

I've been trying  to create a workflow in Alfred that doesn't rely completely on a script. The steps I want to do are:

 

  • Keyword + args
  • New markdown file named with args+date
  • open the new file with iA Writer

 

I've got the first two steps working, using a Ruby script for the second step. I was trying to use the "open file" action for the third step by passing in the newly created file into this action. However I've been unable to work out if args can be passed out of the script to the next step. 

Is passing args out of a script and into another step possible? Or is there a better approach to creating and opening a MD file? 

 

CleanShot 2022-07-21 at 14.25.59@2x.png

Link to comment

I ended up just resorting back to a bash script, which is how I've done it in the past

 

query="{query}"
timestamp=$(date +%Y%m%d%H%M)
dir=/Users/me/some\ dir

# Create new file
touch "$dir"/"$query $timestamp".md

# Open file
open $l -a /Applications/iA\ Writer.app "$dir"/"$query $timestamp".md

 

I was trying to also incorporate snippets so that I could open and auto populate some text, but I've got this as two seperate steps for now. 

Link to comment

@amoodie I've created a workflow that might help you simplify your process.

 

You can download the workflow here.

 

Screenshot 2022-07-21 at 12.06.03.png

 

In the keyword object, you set your argument (e.g. "write myfile").

 

In the arg/var second object, the path is set and the date is added as a dynamic placeholder. It's currently set to ~/Documents/{query} - {date:yyyy-MM-dd}.md but you can change the location and date format to your preference.

 

The Write Text file object creates the file - you have the option to add some pre-defined content to your file here if it's helpful.

 

And finally, the Open File object opens the file in the default app for .md, but in your case, you can specify using iA Writer here.

 

Hope this helps :)

 

Cheers,
Vero

Link to comment

Thank you @Vero!!

 

This is exactly what I was after. Great to be able to see how you put this together. Thanks for sharing. 

 

One last thing, in the snippets there's an option for the {{cursor}} placement. Do you know if there is any way to do this in the Write Text File output? 

 

Thanks,

Aaron

Link to comment

@Vero Actually, I figured it out! If you add the Copy To Clipboard output right at the end, and select the "Automatically paste into frontmost app", the content can be added here as the second last step opens the app, and then there is a {{cursor}} placement option. 

Link to comment
9 hours ago, amoodie said:

Is passing args out of a script and into another step possible?

 

For future reference, it is indeed possible. It’s whatever you send to standard output. In other words, if you echo (Bash and Zsh) or puts (Ruby) something in the Run Script, it will be read as input by the next object. Those commands do send a newline at the end, which you can avoid with echo -n / printf (Bash and Zsh) or print(Ruby).

 

Alternatively, you can output a JSON string with arg filled out, and that will be used. That method is useful if you want to send more things along (such as variables).

 

7 hours ago, amoodie said:
query="{query}"

 

Avoid with input as query in favour of with input as argv so you don’t have to worry about escaping (there’s a reason the latter is the default). You’d then use $1${1} (Bash and Zsh) or ARGV[0] (Ruby).

 

7 hours ago, amoodie said:
touch "$dir"/"$query $timestamp".md

 

You don’t need to start and stop the quotes. You can outright do touch "$dir/$query $timestamp.md".

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