Jump to content
advena

Problem with Write Text File using multiple variables

Recommended Posts

Hey there. First off I'm not a programmer so I apologize for the abomination you're about to witness in this workflow.

 

So here's my issue: I'm trying to write a workflow that I can use to select text in Safari, hit a hotkey and have a .md file created in a specific folder. That's fine and I've got that working. The problem I'm having is when I start to try to use multiple variables. I'm sure it's something I'm doing, but I simply can't get more than one variable to be written in the file. I've tested each variable by either having it copy to the clipboard or having it post to large type and they all work, but for some reason I can't get all the variables working together. I strongly suspect I'm missing something about how to use variables even though I've been googling about this till my fingers bleed lol.

 

Alfred v3.6.2

macOS 10.13.6

 

Link to workflow

 

Thanks for any help!

-Scott

 

The variables are:

 

{uid}    (date:yyyyMMddHHmm)

{url}    (current tab's url)

{title}    (current tab's title)

{selection}    (selected text in Safari)

 

And here's what I'm trying to get:

 

Filename: {var:uid} {var:title}.md

 

Contents of file:

 

UID: {var:uid}
Source: {var:url}
Tags: 

 

# MyNoteTitleHere

 

MyNotesHere

 

---

 

## {var:title}

 

{var:selection}

Share this post


Link to post

There are a couple of issues with your workflow which which is why it's not working as you intend:

 

1. Having 3 separate streams coming out of a hotkey means these three streams are running independently of each other, the top one first, then the second, then third one down. I.e. in your example workflow, the file will be written 3 times. To run as a single flow, you need to chain the items in series.

 

2. Setting a variable in osascript is local to the script itself... so for example, "theTitle" will never be passed back into the Alfred workflow. For data to be passed back into Alfred, it has to be output from the script - you can then set the variable using the Arg / Var as you have done. (More advanced scripting allows you to pass JSON out of a script to directly set variables.)

 

Add a Utilities > Debug workflow object connected to outputs of objects then use the little bug icon top right of the workflow editor to see what data is being passed around your workflow. This will help you work out where things are going wrong! :)

 

Cheers,

Andrew

Share this post


Link to post
7 hours ago, Andrew said:

these three streams are running independently of each other, the top one first, then the second, then third one down. I.e. in your example workflow, the file will be written 3 times

 

By that, you mean Alfred starts them in that order, right? They are in fact run in threads and can finish in any order, can't they?

Share this post


Link to post
7 hours ago, Andrew said:

For data to be passed back into Alfred, it has to be output from the script

 

@advena Concretely, this means that your AppleScript shouldn't be set theTitle to name of front document but return the name of front document.

Share this post


Link to post
56 minutes ago, deanishe said:

By that, you mean Alfred starts them in that order, right? They are in fact run in threads and can finish in any order, can't they?

 

That's correct, I was going to say they essentially launch in order but run in parallel but that's with caveats and can be misleading! Thanks for correcting me.

Share this post


Link to post

@Andrew Thank you so much for taking the time to actually teach me what was going on rather than just supplying an answer! I've now got the workflow running great and have expanded it 😅

 

@deanishe thanks for the explanation… I updated my applescripts and they're working great!

Share this post


Link to post
10 minutes ago, Andrew said:

Thanks for correcting me.

 

I'm just verifying my mental model of how Alfred works. I'd always been under the impression that the Append to File action would only get run once when the first incoming connection fires, not three times, one for each incoming connection.

 

Today I learned…

Share this post


Link to post

Hey @Bhishan--

 

I decided to post this to the forum in case it helps someone else out.

 

There were two main things I was doing wrong. First was running each variable on its own thread. For what I was doing, the proper thing to do was to have everything on one thread (and therefore running one after the other).

 

The second issue I was having was with filters. You'll need to google to understand exactly why it happens (I don't understand it well enough to explain it), but I was having unwanted newlines & whitespace in the output. This meant filters weren't working as I expected (since the input to the filter had newlines etc I didn't know about). That was fixed by using some `Transform` and `Replace` utilities.

 

I'll attach both of the workflows based around the original workflow.

 

Hope these workflows help you figure it out :)

-Scott

 

Create a new note with or without selected text with Zettelkasten metadata

 

Create a new note (with or without selected text) & PDF from Safari tab with Zettelkasten metadata


 

Screenshots:

 

New note:

 

1664636025_ScreenShot2018-09-17at3_38_30pm.thumb.png.28b6077655118ceebc977a75226103ff.png

 

New note & PDF:

 

1403111702_ScreenShot2018-09-17at3_39_38pm.thumb.png.69022d63cf2ad7bd7f31d040270bb279.png

Edited by advena
formatting

Share this post


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
×