Jump to content

argv arguments in external script


Recommended Posts

I am trying to create a workflow that work with two arguments. I am calling an external script (using the "run script" action; see screenshot) with the code below. It works fine from the command line but appears to pass no arguments to the script in Alfred..the workflow completes but nothing is added to the file. What am I doing wrong?

 

#!/usr/local/bin/bash

SCALE=$1
DESCRIPTION=$2
UPDATE=""

CURRENTDATE=`date +"%Y_%m_%d"`
CURRENTTIME=`date +"%H:%M"`

UPDATE+="$CURRENTTIME [[🧠]] [["

for (( i = 1; i <= SCALE; ++i )); do
        UPDATE+="🔷"
done

UPDATE+="]] $DESCRIPTION"


printf "\n- $UPDATE" >> "/Users/chris/Library/Mobile Documents/iCloud~md~obsidian/Documents/bujo/journals/${CURRENTDATE}.md"

 

CleanShot 2021-06-29 at 13.46.16@2x.png

Link to comment

External Script doesn’t take arguments, which is why with input as argv is greyed out. Use /bin/zsh instead and call your script from the box: ./test.sh "${1}". However you’re going into that Run Script, you’re probably passing it one argument, not two—Alfred isn’t a shell, it won’t split what you give it on spaces—you need to take that into account.

 

In the future, when asking for help with a Workflow please upload it somewhere as it’s hard to help you without access to it. Debugging can already be hard with access to the code, and you’re asking us to guess yours from a description. There are multiple places where your code or Workflow setup may be going wrong.


See the Reporting Problems with Workflows topic for a nice overview on how to build an effective report.

Link to comment

My apologies. The code I posted IS the workflow, but for the Keyword action that leads into it with argument required. I assumed the greyed out arguments area in my screenshot was because it didn't have an option to choose between argument types, not that it takes no arguments at all.

 

Based on information here, I'd already tried your approach (using bash, not zsh), and have now tried using zsh as you suggest with the same (lack of) result. I've shared the workflow, using zsh here: https://www.dropbox.com/s/mbcpkzfp2vwqq7z/mood.alfredworkflow?dl=0

 

I'm running Alfred 4.3.4 on OSX 11.4 with no system changes. As noted, test.sh works fine from the command line.

 

Link to comment
10 hours ago, vitor said:

External Script doesn’t take arguments, which is why with input as argv is greyed out.

 

This isn't actually true, when you select External Script, arguments are passed as argv (it says that in description under the script file).

 

8 hours ago, fncll said:

I assumed the greyed out arguments area in my screenshot was because it didn't have an option to choose between argument types, not that it takes no arguments at all.

 

That is the correct assumption, for example, this script would echo back the arg:

 

Screenshot 2021-06-30 at 08.40.49.png

 

The External Script option is my preferred option as it allows you to use any scripting language, and your external development environment.

 

Cheers,

Andrew

 

Link to comment
On 6/30/2021 at 8:42 AM, Andrew said:

This isn't actually true, when you select External Script, arguments are passed as argv (it says that in description under the script file).

 

I stand corrected! Has that always been the behaviour? Unsure why I thought it wasn’t.

 

@fncll Here’s your Workflow fixed. Note how there are almost no changes. The link I posted earlier should explain it, but ask if you have a question. I purposefully made no changes to your script other than the arguments, but you should consider changing /Users/chris to ${HOME} so it’s not tied to your specific username/machine.

Edited by vitor
Link to comment

@fncll: It looks like you want to use a Run Script with bash or zsh (depending on which you prefer) with input via {query} and all escaping options turned off.

 

That will insert your input into the script as if you'd typed it and the weird shell parsing semantics you want will be applied.

Link to comment

Thanks, @vitor, but your workflow exhibits the same issue. If I run the test.sh script embedded from the workflow from the command line like this:

 

./test.sh 5 "foo bar"

 

Then the result is written to the file as intended, like this:

 

19:52 [[🧠]] [[🔷🔷🔷🔷🔷]] foo bar

 

The issue is that when I use the same input in Alfred:

 

m 5 "foo bar"

 

using your revised workflow (or my original), this is written:

 

19:53 [[🧠]] [[]]

 

Sorry if I'm being obtuse. I'm (obviously) not a programmer!

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