How to Create a Dropbox Link With AppleScript & Automator

Published:
Last Updated: Oct 06, 2020

Written by

Creating a Dropbox link for files and folders already stored in Dropbox is easy. But in this article, I’ll show you how to create a Dropbox link for any file in Finder, regardless of where the file is stored using Automator and the Dropbox API.

Note: The technique I describe below does not rely on the deprecated Public folder that Dropbox disabled for accounts created on October 4th, 2012 or later. Instead, I use the Dropbox API to upload files and generate links.

Why create a Dropbox link for files outside of Dropbox?

I store most of my files in either Dropbox or iCloud Drive. The latter doesn’t support sharing individual files via public link yet. So I figured it would be convenient if I could quickly share links to files via Dropbox, regardless of where the file is stored on my Mac.

Also, when I upgraded to the public beta of macOS 10.12 Sierra, I noticed that Annotate, my favorite screen capturing application wasn’t working. Using Annotate, I could grab screenshots and automatically upload them to Dropbox. Annotate would also generate a public Dropbox link that I could paste into a message or an email.

How to create a Dropbox link for any file

My goal was to be able to right-click on any file in Finder and create a Dropbox link to it. I realized that I had to create a system-wide service to make that happen.

The service would do the following:

  • Take the selected file in Finder and upload it to Dropbox
  • Once uploaded, generate a public Dropbox link
  • Copy the Dropbox link to the clipboard

Create a Dropbox App

To accomplish the above, you need to create a Dropbox application. That sounds more complicated than it is. But as a prerequisite, you need to have a Dropbox account. If you don’t have an account, you can sign up here and install the Dropbox app for macOS/OS X. Next, you need to create a Dropbox app. To do that, follow these steps:

  • Log in to developer.dropbox.com
  • Click on My Apps and then on Create app
  • Choose Dropbox API
  • Select App folder as access type
  • Name your app – I called mine MKPublicShare
  • Choose your Dropbox account – if you have multiple
  • Generate your access token
How to create a Dropbox link for any file in Finder using Automator
Create a new app in Dropbox
How to create a Dropbox link for any file in Finder using Automator
Generate token in Dropbox app
How to create a Dropbox link for any file in Finder using Automator
New Dropbox app folder in Finder

The access token is a secret and is to be treated as such. So don’t lose it or share it with anybody! Once you have generated your access token, you will see a new folder in Dropbox/Apps/NameOfYourApp – for instance: ~/Dropbox/Apps/MKPublicShare. That’s the folder your newly created application will have access to.

Automator Service

Next fire up Automator using Spotlight or Launchpad and select Service as the document type. Make the following adjustment at the top of your service:

  • Service receives selected: files or folders in Finder

On the left-hand side, search for the following actions and drag them over, one by one, to the right side in the order as shown in the screenshot below:

  • Run Shell Script
  • Copy to Clipboard
How to create a Dropbox link for any file in Finder using Automator
Configure Service in Automator
How to create a Dropbox link for any file in Finder using Automator
Create a new Service in Automator
How to create a Dropbox link for any file in Finder using Automator
Launch Automator using Spotlight

Click into the text box under the Run Shell Script action and paste the following script. Make sure you change Pass Input to “as arguments.”

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"`

JSON=`curl -s -X POST https://api.dropboxapi.com/2/sharing/create_shared_link_with_settings \
--header "Authorization: Bearer $DROPBOX_TOKEN" \
--header "Content-Type: application/json" \
--data "{\"path\": \"/$FILENAME\",\"settings\": {\"requested_visibility\": \"public\"}}"`
grep -Eo 'https://www.dropbox.com(/[[:graph:]]*)?' <<< $JSON | cut -d\" -f1

Replace “YOUR DROPBOX ACCESS TOKEN GOES HERE” with your actual Dropbox access token. Then Save the service and give it a try.

  • Open Finder and select a file that’s smaller than 150mb (the limit of the API call I use)
  • Right-click the file, select Services and click on the name of your service
  • Depending on the size of the chosen file, you should have a public Dropbox link in your clipboard a few seconds later

If you would like to get notified once the link was generated, add a “Display Notification” action to the end of your service. You can also create a keyboard shortcut for your service via System Preferences –> Keyboard –> Shortcuts –> Services. I use Control + Option + Command + D.

Frequently Asked Questions

Q: Why not leverage the Public Dropbox folder instead of the API?

A: Dropbox deprecated the Public folder for accounts created on October 4th, 2012 and later. So not everyone has that folder. If you do, there are plenty of how-tos out there, which show you how you can use it to generate public Dropbox links. The issue with the Public folder is that it reveals your Dropbox ID and the links can be guessed if you know the name of the shared file. Using the API, each link has a random component to it and cannot easily be guessed.

Q: Can you change the link sharing settings, such as expiration date and visibility?

A: You absolutely can by changing the curl statement from:

JSON=curl -s -X POST https://api.dropboxapi.com/2/sharing/create_shared_link_with_settings \
--header "Authorization: Bearer $DROPBOX_TOKEN" \
--header "Content-Type: application/json" \
--data "{\"path\": \"/$FILENAME\",\"settings\": {\"requested_visibility\": \"public\"}}"

To something like:
EXPIRATION_DATE=`date -v +3w +%Y-%m-%dT%H:%M:%SZ` # 3 weeks in the future

JSON=`curl -s -X POST https://api.dropboxapi.com/2/sharing/create_shared_link_with_settings \
--header "Authorization: Bearer $DROPBOX_TOKEN" \
--header "Content-Type: application/json" \
--data "{\"path\": \"/$FILENAME\",\"settings\": {\"requested_visibility\":{\".tag\":\"password\"},\"link_password\":\"Secret\",\"expires\":\"$EXPIRATION_DATE\"}}"`

That adds an expiration date of 3 weeks into the future and makes the link password protected using “Secret” as the password. Note that only Pro and Business accounts can use those features, Basic accounts cannot.

Q: How can I store uploaded files in a different folder inside of Dropbox?

Full Dropbox - Access
Full Dropbox – Access

A: To store files, uploaded via the script above, outside the “Apps” folder, you have to give your Dropbox app full access to Dropbox. So when you create your app, choose “Full Dropbox – Access” and then modify my script to specify the folder you want to store uploaded files in. For example, if you want to store uploaded files in Dropbox\Public, change the script as follows:

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

JSON=`curl -s -X POST https://api.dropboxapi.com/2/sharing/create_shared_link_with_settings \
--header "Authorization: Bearer $DROPBOX_TOKEN" \
--header "Content-Type: application/json" \
--data "{\"path\": \"/Public/$FILENAME\",\"settings\": {\"requested_visibility\": \"public\"}}"`
grep -Eo 'https://www.dropbox.com(/[[:graph:]]*)?' <<< $JSON | cut -d\" -f1

How to create a Dropbox link for any file in Finder using Automator

Using Automator and a few shell commands, it’s relatively simple to add a system-wide service that enables you to create a Dropbox link for any file in Finder. I find that capability very useful, especially when I need to share files that I have stored on iCloud Drive. Maybe in the future, Apple will add that capability to iCloud. Until then, I will have to rely on Automator and my service script.

36 thoughts on “How to Create a Dropbox Link With AppleScript & Automator”

  1. Hi Michael,

    Thanks a lot for this post. I’m trying to use it as a basis for a “copy multiple dropbox links to the clipboard at once” tool, but I’m not having any luck thus far :-(

    So, I don’t need to upload anything to Dropbox, just get links for files that are already in Dropbox. I’ve generated my Dropbox access token, and tried to use your example usage of the create_shared_link_with_settings method, but I get nothing in my clipboard after running the automator service.

    Please could you explain what the line
    FILENAME=$(basename “$FILE”)
    is for in your script? What is “basename” and where does it come from?

    I’ve even tried replacing $FILENAME in the –data “{\”path\”: \”/$FILENAME\”, etc. line, with an absolute path to a file in Dropbox, but still get no link, and no error messages.

    Any assistance most gratefully received.

    Reply
    • I have figured it out! I now have a service where I can select multiple files in my Dropbox, right click, select my “Copy Multiple Dropbox Links” service, and get a list of all the links copied to the clipboard.

      My investigations started by running the curl operation directly in terminal, and it appears that if you run create_shared_link_with_settings on a file that has already had a link created, that results in an error – I wasn’t seeing the error, because the grep operation in your script is just looking for a link, and if there isn’t one, you get no output.

      So, to handle the situation where selected files might already have a link associated with them, you need to use the list_shared_links method first. If that doesn’t find a link, then use create_shared_link_with_settings.

      Here’s my configuration:

      Create a service (now called “Quick Action”) in Automator, with “workflow receives current” set to “files or folders” in “Finder”, and feed that into a “Run Applescript” block. The AppleScript is as follows:

      on run {fileList}
      set tid to text item delimiters
      set linkList to “”
      repeat with aFile in fileList
      set filePath to aFile as text
      set text item delimiters to “:”
      set the pathList to every text item of filePath
      set text item delimiters to “/”
      set filePath to items 5 thru -1 of pathList as string
      set dropboxLink to do shell script “/Applications/Utilities/get_db_link.sh ” & quoted form of filePath
      if dropboxLink is “” then
      set dropboxLink to do shell script “/Applications/Utilities/create_db_link.sh ” & quoted form of filePath
      end if
      set linkList to linkList & dropboxLink & return
      end repeat
      set text item delimiters to tid
      if length of linkList > 0 then
      set linkList to text 1 thru -2 of linkList
      end if
      set the clipboard to linkList
      end run

      Others will need to configure the line “set filePath to items 5 thru -1 of pathList as string”, which removes the first four parts of the file path (my Dropbox folder is at macOS:Users::Dropbox, where “macOS” is the name of my hard drive, so I need to remove that beginning part of the path from the file’s full paths.

      This script calls two shell scripts, “get_db_link.sh” and “create_db_link.sh”. The contents of get_db_link.sh is:

      JSON=`curl -s -X POST https://api.dropboxapi.com/2/sharing/list_shared_links \
      –header “Authorization: Bearer ” \
      –header “Content-Type: application/json” \
      –data “{\”path\”: \”/$1\”, \”direct_only\”: true}”` grep -Eo ‘https://www.dropbox.com(/[[:graph:]]*)?’ <<< $JSON | cut -d\" -f1

      where is the Dropbox key (inserted directly, no extra quotes, or begin and end carets (“”) required).

      And the contents of create_db_link.sh is:
      JSON=`curl -s -X POST https://api.dropboxapi.com/2/sharing/create_shared_link_with_settings \
      –header “Authorization: Bearer ” \
      –header “Content-Type: application/json” \
      –data “{\”path\”: \”/$1\”,\”settings\”: {\”requested_visibility\”: \”public\”}}”` grep -Eo ‘https://www.dropbox.com(/[[:graph:]]*)?’ <<< $JSON | cut -d\" -f1

      One thing to note is that executing this script can be VERY slow. I've set the automator workflow to end in a notification, and also you can keep an eye on the system menubar – on the right hand side a spinning cog appears when the service is executing, so as long as you see that spinning away you know the service is doing something and you just have to be patient. But fair warning, if you use this on hundreds or even thousands of files at once, you need to be prepared to wait for several minutes.

      Reply
      • Some further notes, as I see the comments system has modified my post:

        Firstly, in the AppleScript and shell scripts, be sure that all double quotes are “straight” quotes, rather than the curled “smart” quotes you see here.

        Secondly, the shell scripts should of course contain your dropbox access token, after the word “Bearer”: I had put “insert your Dropbox access token here”, but enclosed in carats, and the system just disappeared that completely.

        Reply
  2. Hi Michael,
    Clearly you are quote the guru when it comes to understanding dropbox API.
    I am trying to create a dropbox link to multiple pdf files in my dropbox folders and paste that link into a new row in an excel template.
    The volume is around 200 pdf files.
    I’ve tried VBA and automator with a little bit of windows command script while at the office.
    Would you have any idea how to best go about this?

    Many thanks

    Giles

    Reply
    • Hi Giles,

      You might be overestimating my abilities :)

      If you have multiple source files, you can either upload them one-by-one or zip them first. If you do it one-by-one, you’d end up with multiple links, which it sounds like, is not what you want. If you need a single link, you have to upload a single item (folder, zip file…).

      If you are OK with multiple links, then you’d just have to loop through all the PDFs in your target folder and run my script. How to get those links into Excel, I’m not sure because I don’t speak VBA. From within Apple Script, I could imagine writing those links to a CSV (text) file and picking that up from within Excel via VBA. I don’t know if there is a more elegant solution for instance by calling Apple Script from VBA and using the link as a return code.

      Cheers
      Michael

      Reply
  3. This script works well for me, thank you!

    How would you modify the script to generate a shared link for a file that is already uploaded to Dropbox?

    I know I can just right click in Finder to Copy Public Link, but if I can make a Quick Action for it then I can assign a keyboard shortcut, which is my ultimate goal.

    Reply
    • Hi Enrico,

      to do that you have to give your app FULL ACCESS to your DB, not just the app folder. Then you can skip the upload, and call right the create_shared_link_with_settings method by providing the path to the file in question.

      Cheers
      Michael

      Reply
  4. Hi Michael,
    Works like a charm but now, I don’t have the right click options on the other folders in my dropbox. Any idea why ?

    Reply
    • I have reinstalled dropbox and everything is back to normal, except that the script does not work anymore. I tried again from scratch but no luck. Anyway, that’s it for today. Thanks anyway Michael.

      Reply
      • Hi Max!

        have you tried running the script from within Automator, so you can see if and where it gets stuck! You just need to add a “Ask for Finder Items” action before, so you can pick a file to test.

        Also, of your DB is re-synching after installing it, the problem might just be temporary and until everything has synched up.

        Cheers
        Michael

        Reply
  5. I faced an issue where all uploaded files end with a curled “.

    Replacing the curled quotation marks in the part FILENAME=$(basename “$FILE”) fixed it.

    Reply
  6. Hi Michael! Great article, I did not know this existed!
    I do have a problem though, and that is that the file uploaden in the Dropboxfolder, contains an extra ” after the extension. So if the fike was test.png, it becomes test.png”
    This gives an error in Dropbox obviously. The link does get copied correctly, including the unwanted ”
    Any idea what I’m doing wrong? Thanks in advance!

    Best, Martine

    Reply
    • Hmm…I can only imagine you have an ” too much in your script…can you post the relevant sections of your script please, so I can take a look at it?

      Reply
  7. A couple of modifications would make the script work when the folder and/or file names have spaces in them:
    use
    FILENAME=$(basename “$FILE”) instead of FILENAME=`basename “$1″`
    and
    –data-binary @”$FILE”` instead of –data-binary @$FILE`

    Thanks.

    Reply
  8. In OSX El Capital (10.11.6) on my older iMac, I simply Control-Click on the file name in the Dropbox drive in Finder on my iMac, and choose “Copy Dropbox link.”

    In MacOS High Sierra on my newer iMac, this doesn’t work. Surely there’s a simpler way to get a Dropbox file URL than all the stuff you described.

    Reply
    • Hi Mike,

      Of course, but the article talks about how to create a link to any file and by that I mean specifically files that are not stored in Dropbox. So instead of manually copying them over and then manually creating a link, you can use a simple keyboard shortcut using the method I proposed.

      Hope that clarifies it!

      Cheers
      Michael

      Reply
  9. Hi Michael
    I am also getting an “empty file” upload when I run the shell script within a more complex automator folder action. Its almost as it its uploading the file name instead of the file. [is that even possible]. The script works fine in isolation though.
    I have quite a detailed requirement for the script.
    I export a .mov video from Pro Tools
    convert it to mp4
    upload it to dropbox and get shared link
    great email and send info to a blog post
    post opens in chrome and video is ready to send to client.
    The only flaw in the process is the empty file in dropbox.
    Any ideas?

    Reply
  10. Hi Michael — I am trying to problem-solve now that the Dropbox Public folder is no longer public…

    I regularly upload a .csv of product info to my online store at Shopify, including a column of links to photos in my Public Dropbox folder. Now, instead of being able to automate a list of filenames to text, then replacing the root folder path with the public Dropbox link, each individual file has a unique sharing link.

    I’m trying to use your Automator Service here, and it works well! Thanks! But, I would love to add one more step that would allow me to copy all of the dropbox links from a folder and throw them in a .txt file. It could be all at once, or one at a time, added on; the key thing is that I need to automate it so I don’t have to manually run a service or right click/copy link on every one of hundreds of files.

    Any ideas of what I could add to Automator or to the script to solve this? Thanks!!

    Reply
  11. Yeah, the Dropbox API doesn’t currently offer a call for uploading multiple files at once, but it is possible to wrap upload in a loop and then merge the answer-links into one text and puts it in the clipboard.

    This approach was implemented in that script by the link above.

    Reply
    • I see what you mean! If I have some extra time, I may try to implement it and update the blog post. Thanks again for visiting my blog!

      Reply
  12. It’s a pity that the script can’t load several selected files at once.

    As in this, already not working script: http://www.dropboxwiki.com/dropbox-addons/dropbox-service

    Reply
    • Hi Serj,

      It’s not a limitation of my script but of Dropbox. You can share a folder or you can share a single file but not multiple files at once. You would have to zip the selected files and then share the zip file via Dropbox.

      Reply
  13. DROPBOX_TOKEN=”MYTOKEN”
    FILE=$1
    FILENAME=`basename “$1″`

    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`

    JSON=`curl -s -X POST https://api.dropboxapi.com/2/sharing/create_shared_link_with_settings \
    –header “Authorization: Bearer $DROPBOX_TOKEN” \
    –header “Content-Type: application/json” \
    –data “{\”path\”: \”/$FILENAME\”,\”settings\”: {\”requested_visibility\”: \”public\”}}”`
    grep -Eo ‘https://www.dropbox.com(/[[:graph:]]*)?’ <<< $JSON | cut -d\" -f1

    https://www.dropbox.com/s/q9e41g5ciaoitch/automator_script.jpg?dl=0

    Reply
    • Looks pretty identical to mine, with one minor exception. Not sure if that causes the issue but it’s worth noting: see https://db.tt/9doKYIKfdw

      Reply
  14. 10.12.3 also. I’m not sure what the reason could be. I do the exact same thing as above and if the name contains spaces (like default scren shoot) then I get an empty file. I thought maybe icloud desktop could be the cause, but the problem remains even if I create a dropbox link from a file already in Dropbox’s App folder. I thought maybe Sharpshooter was interferring somehow, but remains if disabled. Very strange.

    Reply
    • Very odd indeed…Dropbox shouldn’t have any issues with spaces in file names. Can you paste your full script?

      Reply
  15. Hi Michael,
    But the issues appears to be with the script part (which I know nothing about). The dropbox link to “file with spaces” works, just the file is empty. That is: when your script copies the selected file and moves to the Dropbox App it’s empty when it contains spaces in file name.
    Any ideas?
    -Brian

    Reply
    • Hi Brian!

      I just tested the script on my MacBook running macOS 10.12.3 and had no issues whatsoever. I took a screenshot with the built-in macOS function (CMD + SHIFT + 4), which was then stored on my Desktop. Then I right-clicked on the file (Screen Shot 2016-12-30 at 12.56.17.png) and selected Services –> Create Dropbox Link. The resulting file in Dropbox was not empty.

      Cheers
      Michael

      Reply
  16. Hi Michael,
    Thanks for posting your solution. I have the “empty” file problem also, appears to happen with any file that includes spaces in filename. Would love a solution.
    Thanks
    Brian

    Reply
    • Hi Brian,

      if you suspect spaces in the filename to be the issue, you can use the TRIM function of JMichaelTX from http://macscripter.net/viewtopic.php?id=18519

      Cheers
      Michael

      Reply
  17. This seems to create empty files for screen shots (I don ‘t want to pay for Hazel – sorry) when using the first option.

    Reply
    • Hi Rob,

      are you saying that when you use the service with a text document (or PDF), the file uploaded to Dropbox looks fine but when you use it with an image file (i.e. screenshot) it results in an empty file?

      Cheers

      Michael

      Reply

Leave a Comment

[Fit In 40 Seconds]
[Fit In 40 Seconds]