Jump to content
shadylane

Workflow dealing with files in folders with spaces in the name

Recommended Posts

Hi everybody!

 

OK, I am desperate - I've spent the last two days trying to cobble together a tagging workflow and I am totally stuck...

I would be extremely thankful if some of you Alfred-specialists around here could take a look at my issue. I kind of know my way around Applescript but everything else is a total mystery to me.

 

So here it is:

  • The Workflow uses "tag" (https://github.com/jdberry/tag/ - installed via homebrew, if that matters) to assign my custom tags to files selected in Finder.
  • The workflow should work with a hotkey - be it via a "selection in macOS"-hotkey or by telling Finder to get the selection. 
  • It should work with multiple files.
  • The tags will be specified inside the script (I will duplicate the script for the handful of tags that I use daily).

 

Edit: nevermind, the bash script works now for some reason... I will never understand this stuff. I give up on the Applescript ;)

 

Cheers, Anna

 

on alfred_script(q)

set tagName to "paid"

set text item delimiters to tab
set filePaths to text items of q
set text item delimiters to ""

repeat with thisPath in filePaths
	
	set theFilePath to POSIX path of (thisPath as text)

	do shell script "/usr/local/bin/tag --add " & tagName & " " & theFilePath
	
end repeat

end alfred_script

 

query="{query}"

/usr/local/bin/tag --add paid {query}

echo -n $query

 

Edited by shadylane

Share this post


Link to post
5 hours ago, shadylane said:

do shell script "/usr/local/bin/tag --add " & tagName & " " & theFilePath

 

do shell script "/usr/local/bin/tag --add " & tagName & " " & quoted form of theFilePath

Edited by deanishe

Share this post


Link to post
3 minutes ago, deanishe said:

 

do shell script "/usr/local/bin/tag --add " & tagName & " " & quoted form of theFilePath

 

I'll be damned... THANK YOU!!!! Seriously, stuff like that is driving me insane - I do my best to find answers to problems like these myself online, but it's like pulling teeth for me.

Next time I visit Essen I'll buy you an actual beer ;)

 

Cheers from Austria and thanks again,

Anna

Share this post


Link to post

Let’s look at it in parts:

 

First, when asking for help with a Workflow, please upload it somewhere as we can’t properly help you without access to it. There are multiple places where your code or Workflow setup may be going wrong. Without looking at it we’re mostly shooting in the dark.

 

On to the bash code, since @deanishe already dealt with the AppleScript part:

query="{query}"

That line means {query} and ${query} (same as $query) will be the same. This is both unnecessary and a source of confusion. There’s no reason to have two pieces of code with the same exact value, especially when they have similar names.


Also, avoid with input as {query} (at the top of the script box), as it’s a legacy option that requires escaping depending on the language; use with input as argv instead, which is clearer and less prone to errors. With that setting, your first line would instead be query="${1}" (same as query="$1"). But again, there’s no point in that for so little code, use ${1} directly. Alternatively, use a descriptive name. query is too generic. You know what you’re dealing with, so make it clear (e.g. file_path).

 

/usr/local/bin/tag --add paid {query}

You’re not quoting {query}, which is where your issue lies. Spaces separate arguments—note how each instruction is separated by spaces—so if the value {query} expands to has spaces in it, they’ll be interpreted as different arguments. If the content of {query} were one two (note the space), unquoted {query} (what you have) would be two arguments (one and two); quoted {query} (i.e. "{query}" or '{query}') would be a single argument (one two), which is what you want.

 

echo -n $query

Same issue as above. Always quote your variables (unless you know what you’re doing and that you specifically do not want them quoted). The difference between double quotes (") and single quotes (') is that the latter takes everything literally while the former expands values. So echo -n "${query}" would expand to one two and echo -n '${query}' to ${query} (literal).


Finally, use ${} instead of $. It’s clearer, and it will save you from situations where you need text next to your variable. Bash can understand ${query}three as one twothree, but $querythree tries to be read as a single variable and would fail.


All together now (I’ve added readonly for added correctness, but it’s far from mandatory in this case):

readonly file_path="${1}"
/usr/local/bin/tag --add paid "${file_path}"
echo -n "${file_path}"

Share this post


Link to post
2 minutes ago, shadylane said:

stuff like that is driving me insane

 

AppleScript drives everyone insane, tbh.

 

3 minutes ago, shadylane said:

Next time I visit Essen I'll buy you an actual beer ;)

 

🍻

Share this post


Link to post
13 minutes ago, vitor said:

All together now (I’ve added readonly for added correctness, but it’s far from mandatory in this case):


readonly file_path="${1}"
/usr/local/bin/tag --add paid "${file_path}"
echo -n "${file_path}"

 

It's supposed to work with multiple files, so this would be better:

 

tag.jpg.8fba8cb8374d30a8d5662997c72198b9.jpg

 

Edited by deanishe

Share this post


Link to post

@vitor Thank you, seriously, I really appreciate it that you are taking the time to (try to) explain this stuff to the least logical thinker you will ever meet. I had to turn the music off in the background to use all my brain power to read your answer :D

I'll admit that bash scripts are pretty much gibberish to me, I lack the basic understanding... I had no idea what the difference between "input as {query}" and "input as argv" is - or the difference between single and double quotes - I just throw stuff at the wall and see what sticks, basically. You did clear up some things for me though and I will save your advice for future reference! 

 

@deanishe ha, it's comforting that I'm not the only one scratching my head about Applescript. The sad part though: it's the only language that makes even the tiniest bit of sense to me because it's so much like english ;)

Yeah, the workflow should work with multiple files actually - thanks for adding that!

 

Again, I very much appreciate the help guys. This forum has always been a place where you could ask stupid questions and folks were willing to help. Maybe an app like Alfred, that's made with so much dedication, draws a certain crowd :)

Share this post


Link to post
24 minutes ago, vitor said:

Ah, since the AppleScript was looping through a file list, I was unsure if the tool supported multiple paths at once.

 

I had to check.

 

24 minutes ago, vitor said:

 

Not necessary because I used zsh.

Edited by deanishe

Share this post


Link to post
12 minutes ago, shadylane said:

the difference between "input as {query}" and "input as argv" is

 

Generally, there's little practical difference, but in this particular case you definitely want to use "input as argv" because it provides proper support for multiple arguments (filepaths in this case). {query} can only ever be one argument, so Alfred joins multiple filepaths with tabs, and then you have to split them again.

 

With argv, the first filepath is $1, the second is $2 etc. $@ means "all arguments".

 

As Vítor pointed out, you need to wrap it in double quotes ("$@") in bash to avoid the same problem with spaces in filenames, but you don't in zsh, which I used in the example above.

Share this post


Link to post
1 hour ago, deanishe said:

Not necessary because I used zsh.

 

I missed that on the screenshot. Though I also use ZSH as my shell (and have been doing so for years) I fall back on those habits because I still write scripts in Bash since ShellCheck—my favourite linter in any language—doesn’t support the former. Still, one thing you learn fast with shell scripts is you can never have too much quoting!

 

Fun tangential fact, which I’m not sure I’ve shared before: back in April I opened a radar suggesting changing the default shell to ZSH. It was closed with “there are no plans to address this”. Talk about lack of communication between teams.

Share this post


Link to post
8 hours ago, vitor said:

Talk about lack of communication between teams.

 

:D Though with Apple, they wouldn't tell you even if they knew.

 

8 hours ago, vitor said:

ShellCheck—my favourite linter in any language—doesn’t support the former

 

Yeah, this is really annoying.

Share this post


Link to post
13 hours ago, deanishe said:

 

{query} can only ever be one argument, so Alfred joins multiple filepaths with tabs, and then you have to split them again.

 

With argv, the first filepath is $1, the second is $2 etc. $@ means "all arguments".

 

Hmmm, this actually does make sense. Now I look at all my cobbled-together workflows and wonder how much easier and more efficiently I could have achieved these things not always relying on Applescript... I always use the selection-hotkey and then text item delimiters to spit up the filepaths - but that does look incredibly complicated now in comparison...

 

Oh well. Lots to learn :) Thanks again!

Share this post


Link to post
21 minutes ago, shadylane said:

how much easier and more efficiently I could have achieved these things not always relying on Applescript...

 

There are a few masochists who genuinely seem to like (and be good with) AppleScript, but the most common attitude towards it in the community is to only use it when it's unavoidable.

 

The fact that it kinda looks like English makes it look relatively simple on the surface, but it's such a limited language that it very quickly gets more difficult to do stuff than with a "normal" language. Compare filepaths = query.split('\t') to the whole "text item delimiters" business.

 

21 minutes ago, shadylane said:

and then text item delimiters to spit up the filepaths

 

That's not actually necessary in AppleScript, either. The problem is that you're using "Run NSAppleScript" (which you really shouldn't without a very good reason), instead of "Run Script" with "Language = /usr/bin/osascript (AS)" (where you can also use "with input as argv"):

on run(argv)
    repeat with thePath in argv
    ...
    end repeat
end run
Edited by deanishe

Share this post


Link to post
5 hours ago, deanishe said:

That's not actually necessary in AppleScript, either. The problem is that you're using "Run NSAppleScript" (which you really shouldn't without a very good reason), instead of "Run Script" with "Language = /usr/bin/osascript (AS)" (where you can also use "with input as argv"):

 

Wow. I had no idea... this is so much cleaner and better!!!! Why did I think I needed to use NSAppleScript? Was that maybe the case back in the beginning, when workflows got introduced initially (with Alfred 2 I think)? I never even questioned that, I always assumed the correct way to deal with "stuff" coming from the first node was to use NSAppleScript...

God damn it. Now I want to immediately start to clean up all my workflows ;D 

 

One last question if I may (sorry to keep you occupied) : what would be a "very good reason" to use NSAppleScript?

Edited by shadylane

Share this post


Link to post
5 hours ago, shadylane said:

what would be a "very good reason" to use NSAppleScript?

 

If you need it to run in Alfred's process. The only reason I know would be using the "display notification" command. With Run NSAppleScript, the notification will have Alfred's icon.

 

"Very good reason" is an exaggeration, but you shouldn't use Run NSAppleScript by default, as it has some downsides. It doesn't support multiple arguments, some scripts that work in Script Editor or via a Run Script don't work in Run NSAppleScript (nobody knows why), and Alfred blocks until the script finishes.

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...