Jump to content
vitor

RecentDownloads — List and act on the contents of a directory

Recommended Posts

List the contents of a directory and act on them. Call the workflow with rdn to sort from the most recently modified, rdo for the reverse; rda to sort from the most recently added, rdz for the reverse.


2kGDKPa.png


To act on the selections, use the standard Alfred shortcuts. You can activate file actions, dive into directories, preview files, add them to the file buffer — whatever you want to do that is supported by Alfred.

 

⌥↵ opens the selection in Finder, while ⇧↵ clears the cache for rda and rdz.


By default the Workflow looks in ~/Downloads. Change the value in the downloads_dir Workflow Environment Variable to use a different path, relative to your home directory.

 

Download | Source

Edited by vitor

Share this post


Link to post

I find this to be a very useful workflow, thanks!

 

A minor issue for me is that after I run rdn or rdo some applications with names matching the command (e.g. "Remote Desktop Connection") show first in the list, above the files in the Downloads directory. Is this possible to fix?

Edited by cands

Share this post


Link to post

Yes, you can simply go to the workflow (via Alfred’s preferences), and change the keywords (you just have to double-click each Script Filter node and change the top line, to something that will not interfere with your other apps.

Share this post


Link to post

Thanks, using other keywords solved the problem (I realize that it may not be possible to only show the results output from the workflow after invoking the commands)!

Share this post


Link to post
2 hours ago, Jasondm007 said:

@vitor - I love this workflow! Thanks for sharing!!

 

Thank you. Glad you find it useful.

 

2 hours ago, Jasondm007 said:

Out of curiosity, is there any way to sort by the date added field?

 

There is, but I do not intend to add it to the Workflow. This Ask Different answer and this blog post should put you on the right track.

Share this post


Link to post

Thanks for the suggested materials concerning how I might implement having the workflow sort the download folder's contents by the date it was added.

 

After looking through everything, I'm afraid they're pretty far past my limited skillset. 

 

Thanks for your help though!!

Share this post


Link to post
4 hours ago, politicus said:

Is is possible to hide not completed Downloads from the search results?

 

Would have to look into it. But I’m not going to add it to the Workflow because seeing incomplete Downloads is useful (e.g. if you cancel a download and want to delete the incomplete file).

Share this post


Link to post
On 08/10/2017 at 1:57 AM, Jasondm007 said:

how I might implement having the workflow sort the download folder's contents by the date it was added.

 

The date added is stored in the Spotlight metadata, so you’d have to use something like mdls to retrieve it.

Share this post


Link to post
On 13/10/2017 at 6:36 PM, vitor said:

 

That explains why it's way slower than the other variants. How about turning on "Alfred filters results" for that one?

 

Also, I'm seeing the following error for all commands (although they all still seem to work):

Code 2: /bin/bash: -c: line 13: syntax error near unexpected token `fi'
/bin/bash: -c: line 13: `fi'

Share this post


Link to post
1 hour ago, deanishe said:

Also, I'm seeing the following error for all commands (although they all still seem to work):

 

Fixed that. Thank you.

 

1 hour ago, deanishe said:

That explains why it's way slower than the other variants. How about turning on "Alfred filters results" for that one?

 

The solution itself is pretty hacky. Wasn’t thinking of officially adding it to the Workflow. Conceptually, Downloads should be a directory where other apps put files and then we only take them out.

 

I also wouldn’t like the inconsistency: the current solution starts the search from anywhere on the string, including the middle of words. I find that somewhat important for downloaded stuff (where much/most of the time you don’t control the filename as it comes). Since that’s not something Alfred can currently do by itself, the two solutions would be inconsistent with each other.

 

This Workflow should be pretty safe to edit, though. It’s unlikely it’ll need further updates apart from the just released fix (it even still uses the XML Script Filter and is largely unchanged from its origins).

Edited by vitor

Share this post


Link to post
1 hour ago, vitor said:

Conceptually, Downloads should be a directory where other apps put files and then we only take them out.

 

I tend to leave quite a lot of stuff in ~/Downloads that doesn't have an obvious other place to be, so I always want most-recently added at the top, not most-recently accessed.

 

1 hour ago, vitor said:

the current solution starts the search from anywhere on the string, including the middle of words

 

Good point. That is preferable behaviour.

 

1 hour ago, vitor said:

This Workflow should be pretty safe to edit, though

 

Given the above, I think I'll whip up my own that caches the times. I'm not a fan of bash and/or perl.

Share this post


Link to post

@vitor A few weeks ago, in this thread, you were nice enough to update this great workflow, adding the options to sort by the date added. More specifically, you added two options: one placing the newest item first, and another placing the oldest item first. To jog your memory I've added the script from the filter below (for the second option - file with the oldest date added is listed first).

IFS=$'\n'

files=$(mdls -name kMDItemFSName -name kMDItemDateAdded -raw "${HOME}/Downloads/"* | xargs -0 -I {} echo {} | sed 'N;s/\n/ /' | sort | cut -d ' ' -f 4- | grep --ignore-case ".*{query}.*" | perl -MHTML::Entities -CS -pe'$_ = encode_entities($_, q{&<>"'\''})')

echo "<?xml version='1.0'?><items>"
for file in ${files}; do
  echo "<item uuid='file' arg='${file}' type='file'>"
  echo "<title>$(basename ${file})</title>"
  echo "<subtitle>${file}</subtitle>"
  echo "<icon type='fileicon'>${file}</icon>"
  echo "</item>"
done
echo "</items>"
fi

 

While the query works great - as it pulls the items up in their proper order - unfortunately, they're not clickable. When I press return on the file that I'd like to open, the files never actually open. In addition, all of the icons/thumbnails in the query just look like white (see image below).

 

Any ideas for how I might amend your code above to operate like usual?

 

As always, thanks a ton for all of your help!

 

5a03e45829c3b_ScreenShot2017-11-08at9_05_07PM.jpg.94c407210fbf403c41525537d8a29987.jpg

 

 

 

 

Share this post


Link to post

Fixed.


For reference:

IFS=$'\n'

files=$(mdls -name kMDItemFSName -name kMDItemDateAdded -raw "${HOME}/Downloads/"* | xargs -0 -I {} echo {} | sed 'N;s/\n/ /' | sort | cut -d ' ' -f 4- | grep --ignore-case ".*{query}.*" | perl -MHTML::Entities -CS -pe'$_ = encode_entities($_, q{&<>"'\''})')

echo "<?xml version='1.0'?><items>"
for file in ${files}; do
  file="${HOME}/Downloads/${file}"
  echo "<item uuid='file' arg='${file}' type='file'>"
  echo "<title>$(basename ${file})</title>"
  echo "<subtitle>${file}</subtitle>"
  echo "<icon type='fileicon'>${file}</icon>"
  echo "</item>"
done
echo "</items>"
fi

 

Share this post


Link to post

Last question, @vitor , I promise!

 

For those of us without coding backgrounds, do you have any favorite resources that you'd recommend to others interested in tweaking the RecentDownloads workflow? More specifically, I'd like to learn how to use some of the other spotlight metadata to restrict the output further, but - as a newbie - I'm not exactly sure where to start with some of these things.

 

For example, if wanted to restrict the output to certain file types, it looks like using the kMDItemKind field might be a good way of doing that. However, I don't have enough understanding of how your script is using these elements to provide the output (i.e., and this is a "ME" problem and nothing with the script). I've tried adding these elements, but it's clearly not working ... and I don't know enough to efficiently problem shoot.

 

 In any event, I'd just like to learn how to some of these metadata elements without bothering you and other equally nice people in the Alfred community. Thanks for your help!

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