Jump to content

Workflow to mount volumes


Recommended Posts

Hi everyone,

 

I'm newish to the many powers of Alfred and I'd appreciate some help with creating a workflow to mount volumes via Alfred.

 

When a volume is ejected by Alfred, it remains mountable (see Disk Utility; the drive containing the volume isn't ejected) and I can open DU, select the volume in the sidebar and remount it by clicking in Mount on DU's toolbar.

 

I'd love to be able to type something like “mount [VolumeName]” in Alfred and have it magically be available once again.

 

Could any of you experienced Alfred users point me in a direction to have a functioning workflow? I'm really green in scripting, so any help would be truly appreciated.

 

 

Link to comment
12 hours ago, Selma said:

When a volume is ejected by Alfred

 

Yeah. I think Alfred only unmounts volumes and doesn't actually eject them.

 

12 hours ago, Selma said:

Could any of you experienced Alfred users point me in a direction to have a functioning workflow?

 

You can use /usr/sbin/diskutil list and /usr/sbin/diskutil info to get info about volumes, and show it in a Script Filter. Then you can use /usr/sbin/diskutil mount to mount your device.

Link to comment

This is amazing, thanks a lot for the workflow! It indeed lists /dev/disk#, but somehow only one partition of one of the disks gets mounted (which is an APFS main disk clone partition). Could I work around it to make other file systems mountable as well or would this work only with APFS? Much appreciated!

Link to comment
On 12/27/2020 at 1:19 AM, Selma said:

but somehow only one partition of one of the disks gets mounted (which is an APFS main disk clone partition).

 

That’s weird, because I’m using diskutil mountDisk, which per the documentation should Mount an entire disk (all mountable volumes). You can verify that on the Workflow’s Run Script and may want to play with it to see if you can get the desired result. Running diskutil on its own on a terminal will give you the available commands.

Link to comment
  • 8 months later...
On 12/26/2020 at 3:43 PM, vitor said:

Here’s a simple Workflow to do what you want. It’s pretty bare, in that it doesn’t show the volume’s name or size (it’s a quick semi-hackish solution which parses the human-readable output of diskutil instead of the plist), but unless you have a ton of external disks connected at once, it should fulfil your immediate needs.

Thanks for this.

 

I rewrote it in bash:

  • mount (lists by Volume name)
  • unmount (lists by Volume name)
    [these are customised to ignore some of my system volumes, you may want to edit it]
  • vol (lists by Volume name)
    [both mount and unmount combined into one node, code is duplicated so any edits will also need to be done here]

 

https://www.gingerbeardman.com/alfred/Disk Utilities.alfredworkflow

 

Edited by gingerbeardman
version 2 now written in bash
Link to comment
  • 2 months later...

I updated my workflow available above.

 

I rewrote it in bash as I'm more familiar with that than ruby.

 

  • mount and unmount both list Volumes by name, rather than device number
  • mount will only show unmounted Volumes
  • unmount will only show mounted Volumes
  • vol one keyword that lists all drives available for mount/unmount- certain Volumes/Devices are skipped (hardcoded)
  • no more need for list command

 

cheers!

 

Edited by gingerbeardman
Link to comment
  • 3 months later...
On 11/22/2021 at 1:45 PM, gingerbeardman said:

I updated my workflow available above.

 

I rewrote it in bash as I'm more familiar with that than ruby.

 

  • mount and unmount both list Volumes by name, rather than device number
  • mount will only show unmounted Volumes
  • unmount will only show mounted Volumes
  • vol one keyword that lists all drives available for mount/unmount- certain Volumes/Devices are skipped (hardcoded)
  • no more need for list command

 

cheers!

 

 

Appreciate the workflow, this is just what I want.
I can not get it to work though. Every time time I try to list the volumes it just "blanks out" and shows nothing. Do you know if Apple changed something in the most resent version of Monterey?

 

Thanks

Link to comment
On 3/6/2022 at 12:11 PM, vitor said:

Welcome @davidrd85,

 

Open the debugger and try again. It should tell you what’s wrong, and that’s crucial information to help you.

Thank you so much. I didn't know about that feature.
 

This is what I got:

 

[14:20:16.098] Logging Started...
[14:20:20.841] Disk Utilities[Script Filter] Queuing argument '(null)'
[14:20:21.230] Disk Utilities[Script Filter] Script with argv '(null)' finished
[14:20:21.232] STDERR: Disk Utilities[Script Filter] /Users/david/Library/Caches/com.runningwithcrayons.Alfred/Workflow Scripts/E7FEF73A-A0F8-4D15-B92D-D36DADCD0903: line 18: /usr/local/opt/jq/bin/jq: No such file or directory
[14:20:21.233] Disk Utilities[Script Filter] {"items":[,]}
[14:20:21.234] ERROR: Disk Utilities[Script Filter] JSON error: Invalid value around line 1, column 10. in JSON:
{"items":[,]}

 

Do I need to install some other script?

Link to comment

@davidrd85 If you are on an Intel Mac, have Homebrew, and run brew install jq in a terminal, the Workflow should start working.

 

@gingerbeardman To make this work out-of-the-box for more people, you only need two changes (in all Script Filters):

  • Add export PATH="/opt/homebrew/bin:/usr/local/bin:${PATH}" at the top.
  • Change /usr/local/opt/jq/bin/jq to jq.

Also, you can remove the #!/usr/bin/env bash shebang because it’s not doing anything; you’re already telling Alfred to run them via /bin/bash.

Link to comment
16 minutes ago, vitor said:

@davidrd85 If you are on an Intel Mac, have Homebrew, and run brew install jq in a terminal, the Workflow should start working.

 

@gingerbeardman To make this work out-of-the-box for more people, you only need two changes (in all Script Filters):

  • Add export PATH="/opt/homebrew/bin:/usr/local/bin:${PATH}" at the top.
  • Change /usr/local/opt/jq/bin/jq to jq.

Also, you can remove the #!/usr/bin/env bash shebang because it’s not doing anything; you’re already telling Alfred to run them via /bin/bash.

Thank you for the detailed explanation.

I'm on a M1 Mac though. Is Homebrew not yet available for Apple silicon?

Link to comment
1 minute ago, davidrd85 said:

I'm on a M1 Mac though. Is Homebrew not yet available for Apple silicon?

 

It is, but it installs to a different location and this Workflow hardcodes the Intel path. If you brew install jq then do the steps I wrote for @gingerbeardman above, it should work for you.

Link to comment
16 minutes ago, vitor said:

 

It is, but it installs to a different location and this Workflow hardcodes the Intel path. If you brew install jq then do the steps I wrote for @gingerbeardman above, it should work for you.

 

I did install Homebrew for Apple silicon. I also tried to do what you explained in your post, but I have no real scripting knowledge.

Would you please write the corrected script?

 

#!/usr/bin/env bash

devices=($(diskutil list external physical | grep "/dev/" | cut -d' ' -f1))

for device in ${devices[@]}; do
	if [[ ${device} != *"disk2"* && ${device} != *"disk5"* ]];then
		mounted=$(mount | grep $device | wc -l)
		
		if [[ $mounted -eq 0 ]];then
			max_partition=$(($(diskutil list $device | wc -l)-3))
			
			vol_name=$(diskutil info ${device}s${max_partition} | grep "Volume Name" | xargs echo -n | cut -d' ' -f3)
			
			JSON_STRING=$(/usr/local/opt/jq/bin/jq -n \
			--arg title "$vol_name" \
			--arg subtitle "Select to mount" \
			--arg arg "$device" \
			'$ARGS.named')
			
			JSON+="$JSON_STRING,"
		fi
	fi
done

echo "{\"items\":[$JSON]}"

 

Link to comment
1 minute ago, davidrd85 said:

I have no real scripting knowledge.

 

You don’t need it. I phrased it so you can just make the text changes I pointed out. But I’ll say it another way. In all three Script Filters:

  • Replace the line #!/usr/bin/env bash with export PATH="/opt/homebrew/bin:/usr/local/bin:${PATH}".
  • Replace the line JSON_STRING=$(/usr/local/opt/jq/bin/jq -n \ with JSON_STRING=$(jq -n \.

This is untested, but considering your error above, should fix it.

Link to comment
14 minutes ago, vitor said:

 

You don’t need it. I phrased it so you can just make the text changes I pointed out. But I’ll say it another way. In all three Script Filters:

  • Replace the line #!/usr/bin/env bash with export PATH="/opt/homebrew/bin:/usr/local/bin:${PATH}".
  • Replace the line JSON_STRING=$(/usr/local/opt/jq/bin/jq -n \ with JSON_STRING=$(jq -n \.

This is untested, but considering your error above, should fix it.

 

Thank you. But that sadly did not help. I even tried a restart just to make sure.

 

Still same ERROR

[15:40:54.784] Logging Started...
[15:40:57.189] Disk Utilities[Script Filter] Queuing argument '(null)'
[15:40:57.558] Disk Utilities[Script Filter] Script with argv '(null)' finished
[15:40:57.560] STDERR: Disk Utilities[Script Filter] /Users/david/Library/Caches/com.runningwithcrayons.Alfred/Workflow Scripts/CDCC52B7-F260-4227-B60C-275901821708: line 18: jq: command not found
[15:40:57.561] Disk Utilities[Script Filter] {"items":[,]}
[15:40:57.562] ERROR: Disk Utilities[Script Filter] JSON error: Invalid value around line 1, column 10. in JSON:
{"items":[,]}

 

Link to comment
Just now, davidrd85 said:

No that is not installed.

 

Then install it. That’s part of the instructions:

 

1 hour ago, vitor said:

have Homebrew, and run brew install jq in a terminal

54 minutes ago, vitor said:

If you brew install jq then do the steps I wrote

 

You cannot invoke a tool if you don’t have it.

Link to comment
19 minutes ago, vitor said:

 

Then install it. That’s part of the instructions:

 

 

You cannot invoke a tool if you don’t have it.

 

No clearly, but I'm a noob at this 🙄

But the workflow might be broken altogether. Now when the workflow list the unmounted disks, it just lists two selections that writes: "Not"

 

[16:04:03.704] Logging Started...
[16:04:08.273] Disk Utilities[Script Filter] Queuing argument '(null)'
[16:04:09.190] Disk Utilities[Script Filter] Script with argv '(null)' finished
[16:04:09.193] Disk Utilities[Script Filter] {"items":[{
  "title": "Not",
  "subtitle": "Select to mount",
  "arg": "/dev/disk4"
},{
  "title": "Not",
  "subtitle": "Select to mount",
  "arg": "/dev/disk6"
},]}

 

The unmount command just fails

 

[16:08:02.337] Disk Utilities[Script Filter] Processing complete
[16:08:02.338] Disk Utilities[Script Filter] Passing output 'Seagate' to Run Script
[16:08:02.386] ERROR: Disk Utilities[Run Script] Unmount failed for Seagate

 

Thank you for your generous time. But this script was not really a mount/unmount solution.

Edited by davidrd85
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...