Jump to content

Workflow to run acextract on selected file(s) in Finder


Recommended Posts

I would like to create a Workflow that takes a file selection from Finder (matching com.apple.xcode.compiled-asset-catalog files) and runs each file through the `acextract` executable.

 

I'm completely unfamiliar with bash scripting syntax, so I understand conceptually what needs to happen, but can't figure out how to get Alfred to do what I want. 

 

The command I want to execute is:

 

acextract -i $file -o ./$dir

 

...where $dir would be the .car filename converted to a new directory, but without an extension, i.e. from ./assets.car to ./assets/

 

Thanks!

Link to post
5 hours ago, chris said:

I would like to create a Workflow that takes a file selection from Finder (matching com.apple.xcode.compiled-asset-catalog files) and runs each file through the `acextract` executable.

 

If you only want it to be available for certain filetypes, you'll need to implement it as a File Action. A Hotkey can pass your script the selected items in Finder, but you'd have to filter them yourself, then.

 

5 hours ago, chris said:

...where $dir would be the .car filename converted to a new directory, but without an extension, i.e. from ./assets.car to ./assets/

 

So the “.car filename” is what the variable $file contains?

 

Link to post

 

10 hours ago, deanishe said:

If you only want it to be available for certain filetypes, you'll need to implement it as a File Action. A Hotkey can pass your script the selected items in Finder, but you'd have to filter them yourself, then.

 

Yes, that's how I started:

 

image.png.92918b7672e89adf837933c17033db1f.png

 

But when I got to writing the script, I got a bit lost. 

 

10 hours ago, deanishe said:

So the “.car filename” is what the variable $file contains?

 

 

Typically when I run the script on the command line, it looks like this:

 

acextract -i "/Downloads/Assets.car" -o "/Downloads/Assets/"

 

"/Downloads/Assets/" is a directory that doesn't yet exist, but is created by acextract.

 

Link to post
52 minutes ago, deanishe said:

# filepaths from ARGV
for carfile in $@; do
	# strip file extension
	dirpath="${carfile:r}"
	# create directory
	mkdir -p "$dirpath"
	/usr/local/bin/acextract -i "$carfile" -o "$dirpath"
done

 

This should work in zsh (not bash).

 

Hooray! 

 

Ok, one more thing since the basics are working (I swear, I'm trying to learn as we go along)... how would I pass the number of files extracted (i.e. the count of the files in the $dirpath) to a notification?

 

I'm completely baffled. I did some research and found a bash script to do the counting, but for the life of me, I can't figure out how to get the count to the notification...

 

# filepaths from ARGV
for carfile in $@; do
	# strip file extension
	dirpath="${carfile:r}"
	# create directory
	mkdir -p "$dirpath"
	/usr/local/bin/acextract -i "$carfile" -o "$dirpath"
	
	# switch to directory	
	cd $dirpath
	find . -maxdepth 1 -exec echo \; | wc -l
done

 

I'm lost when it comes to setting the variables in the Arg and Vars object.

 

Here's what I'd like to put in the notification:

 

image.png.d390daa5a18e4640aac9ebf2280e8b5a.png

 

image.png.fc082da98fd9687db083af967e812f53.png

Link to post
12 hours ago, chris said:

I'm lost when it comes to setting the variables in the Arg and Vars object.

 

You don’t need to: just use {query}, which is the output of your script. In any case, you can't just use variables you declared in your previous script. Those variables only exist in that script. Alfred doesn't know anything about them.

 

find . -maxdepth 1 -exec echo \; | wc -l

 

This won't work properly. That does print the number of files in the directory, but you don't want that in the loop. If you ran it with multiple input files, you'd end up with three lines of output, not a cumulative total. You need to capture the output, add it to a running total, then print the total at the end.

 

total=0
# filepaths from ARGV
for carfile in $@; do
	# strip file extension
	dirpath="${carfile:r}"
	# create directory
	mkdir -p "$dirpath"
	/usr/local/bin/acextract -i "$carfile" -o "$dirpath"

	# no. of files in new directory
	n=$( ls "$dirpath" | wc -l )
	(( total += n ))
done

# pass total to next workflow element
echo -n $total

 

Edited by deanishe
Link to post
10 hours ago, deanishe said:

# pass total to next workflow element echo -n $total

 

Hmm. Seems like I'm still missing something, or that {query} has the output from the `acextract` process. From the console:

 

Passing output 'Extracting: Icon-Production20x20~ipad.png [1mOK[0m
Extracting: Icon-Production29x29~ipad.png [1mOK[0m
Extracting: Icon-Production40x40~ipad.png [1mOK[0m
Extracting: Icon-Production76x76~ipad.png [1mOK[0m
[..]
Extracting: uploaderTemporaryLines763x8@2x.png [1mOK[0m
Extracting: uploaderTemporaryLines763x8@2x.png [1mOK[0m
Extracting: uploaderTemporaryLines763x8@2x.png [1mOK[0m
38' to Post Notification

 

This is what the notification shows:

1876536185_CleanShot2020-10-22at15_48_29.png.d744c39e3555cc36aa714bf6a3a5ae77.png

How do I just pass the $total count to the notification?

Link to post
33 minutes ago, deanishe said:

Tell acextract to be quiet or redirect its output with /usr/local/bin/acextract -i "$carfile" -o "$dirpath" > /dev/null

 

Ah ha! I looked for a way to silence it's output, but it seems to only support (and no quiet mode):

 

input is required
Usage: ./acextract [options]
  -i, --input:  
      Path to input CAR file.
  -o, --output:
      Path to output directory.
  -l, --list:
      Print content of the CAR file without extracting.
  -h, --help:
      Show help message.
  -v, --verbose:
      Print more detail. You can use -vv or -vvv for more info.

 

That worked! 🥳

 

527245498_CleanShot2020-10-23at10_57_29.png.6cac1904e824b9db1f15fe6108d3aff2.png

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
×
×
  • Create New...