In Make an Alfred workflow to save attachments from Mail into a folder I made an Alfred workflow to save attachments from Mail. It was easy to do after doing the AppleScript to save attachments and learning how to do an AppleScript with Alfred. There was one feature missing from the AppleScript, though: I often get zipped files and need those unzipped.
Today I'm doing a simple AppleScript to unzip all files in a folder. The result will be added to the Alfred workflow.
In the "Save Attachments" script I have a folder for the attachments called newFolder. I want to develop the script on its own. Therefore, I need to select a folder first:
set newFolder to ((choose folder) as alias)
Next I need the files that are to be unzipped. My support emails contain gz files. But I also want to unzip normal zip files. As result I first get the files with a ".zip" ending and for the second loop the files with a ".gz" ending:
set FilesToUnzip to (get files in newFolder whose name ends with ".zip")
And later:
set FilesToUnGzip to (get files in newFolder whose name ends with ".gz")
AppleScript can't unzip files natively. But the shell can unzip files. That I do an AppleScript in Alfred with the shell which does a shell is a tiny little bit perverse but needs must. For the zip file I could use unzip but there is also the ditto command. For shell paths is always no fun to make the correct paths when doing a shell in code.
In the loop over the files in the selected folder I have the variable theFile for each file that is going to be unzipped. The alias is converted to a text. The text is made to a Posix path because the shell only deals with Posix paths. Finally I need the quoted form of the Posix path. The quoted form is necessary to deal with the special characters of the shell - mostly the spaces. This gives me
quoted form of POSIX path of (theFile as text)
for the file that needs to be unzipped. The location for the unzipped files is similar. Using the ditto command looks simple because it only needs the source file, a space and the location for the unzipped file.
The second no fun part of a shell command are the options. The ditto command has lots of options. Each command has a "man page". It's easier to access the man page in the browser by typing in "man ditto". Which leads to https://ss64.com/osx/ditto.html . The x option is needed to extract files. Additionally, the k option is needed because I want to unzip zip files instead of CPIO (whatever that is). This gives me "-xk" as options for ditto.
After unzipping a file it needs to be moved to the trash. Which I can do with a AppleScript command "move to trash".
Doing the unzip for the gz files becomes super easy. The gunzip command needs to be used on the source file. There is no location for the unzipped files. And the original file is deleted automatically.
This gives me the following script:
tell application "Finder"
set newFolder to ((choose folder) as alias)
--unzip zip files
set FilesToUnzip to (get files in newFolder whose name ends with ".zip")
repeat with theFile in FilesToUnzip
set ZipFile to quoted form of POSIX path of (theFile as text)
set ZipLocation to quoted form of POSIX path of (newFolder as text)
do shell script "/usr/bin/ditto -xk " & ZipFile & space & ZipLocation
move theFile to trash
end repeat
--unzip gz files
set FilesToUnGzip to (get files in newFolder whose name ends with ".gz")
repeat with theFile in FilesToUnGzip
set GzipFile to quoted form of POSIX path of (theFile as text)
do shell script "/usr/bin/gunzip " & GzipFile
end repeat
end tell
The script is now ready for the Alfred workflow. As I said above the original script for Alfred already has the folder for the attachments. I only need to take out the like with "choose folder". The rest goes into the Alfred AppleScript after the attachments have been saved:
As always you can download the workflow on the Xojo + Alfred page.
I'm really happy with the updated workflow!