Search code examples
shellautomator

Referencing both original and converted file in Automator


I'm trying to create an Automator app that would work like this:

  1. You would drag any type of image file onto the app.
  2. The app would make a duplicate of the file, convert it to JPEG, scale it down to 500px, remove the " copy" part at the end of the filename.
  3. Upload the original file to a specific folder on my Dropbox account.
  4. When step 3 is done, upload the converted JPEG to another specific folder on my Dropbox account.
  5. Delete the converted JPEG locally when the process is finished.

Steps 1 and 2 are working. I've also found a shell script that can be used to upload files to Dropbox with a provided access token. So I can make it upload the converted JPEG, as that seems to be what the $1 variable is holding in the script.

Here's the shell script:

DROPBOX_TOKEN="YOUR DROPBOX ACCESS TOKEN GOES HERE"
FILE=$1
FILENAME=$(basename "$FILE")

UPLOAD=`curl -sX POST https://content.dropboxapi.com/2/files/upload \
--header "Authorization: Bearer $DROPBOX_TOKEN" \
--header "Dropbox-API-Arg: {\"path\": \"/$FILENAME\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}" \
--header "Content-Type: application/octet-stream" \
--data-binary @"$FILE"`

I'm also attaching this screenshot of how the current "Automator flow" looks like. In the current state of this "flow", the file that gets uploaded through the shell script is the converted file.

Automator flow

The part I'm stuck at is how to first upload the original image file with one shell script and then upload the converted file with another shell script. I guess that I have to store a reference for both in some way?

I'm also not quite sure how to specifically delete the JPEG after everything is done.


Solution

  • I think with the Set Value of Variable and Get Value of Variable actions you can achieve what you're trying to do here.

    This workflow provides an example of using these. You can set multiple different named variables, and then read and reuse them later. To get this workflow to work properly I had to select Ignore this action's input for the Get step, otherwise it used both the value of example (which was the filename of the file dropped onto the app) and also the output of Ask for confirmation (which was the filename again, so it tried to move the same file to the Trash twice).

    When creating a variable you just choose New Variable... from the drop-down menu. You then give it a name, and leave the Value box empty (and it gets set from the Set... action's input).

    new variable

    Once you have a Variable set up you can also drag it into some places (from the Variable section at the bottom of the window), as I've done here to make the file path show up in the Ask for Confirmation action. You can also remove any unused variables by right-clicking on the variable in the Variable section, and choosing Delete exampleVariable

    example workflow

    Full steps which I think would complete your full workflow could be:

    1. Set Value of Variable first, to store your 'original filename' in variable original (or something), then the parts of the workflow you already have:
    2. Duplicate file
    3. Convert to to JPEG
    4. Scale
    5. Remove "copy"
    6. Set Value of Variable converted to input
    7. Your already-working upload shell script step
    8. Get Value of Variable to read converted back in (this should have Ignore this action's input ticked)
    9. Move Finder Items to Trash to remove the JPEG file (could be a shell script to delete the file immediately)
    10. Get Value of Variable to read original back in (this should also have Ignore this action's input ticked)
    11. Your upload shell script step again (to upload this time the original file)
    12. Display happy alert message