DropBox and a copy link

hello all
I am trying to figure out how to get a link generated by DropBox when you click on “copy link” into a variable. I am trying to build a script that will take the DropBox link and create an email with the link in the body of the email.

I can do the email part, but can’t figure out how to get the link

thanks for any thoughts or help

Kevin

Browser: Safari 602.3.12
Operating System: Mac OS X (10.10)

This is probably too much of a PITA, but one option is to use “do shell script” with CURL and use Dropbox’s HTTP API.

https://www.dropbox.com/developers/documentation/http/documentation#sharing-create_shared_link_with_settings

However, it requires authentication:

https://www.dropbox.com/developers/documentation/http/documentation#auth-token-from_oauth1

And the first time they request, Dropbox will ask for them to allow access to your app.

Seems like there must be a better way. Maybe through UI scripting if there’s no direct path.

Dropbox has no Applescript dictionary, the command-line control for Dropbox doesn’t work on OSX (it’s only for Linux.) I’m not aware of a way to get Finder contextual menu items though terminal…

So other than a possible UI scripting option or just going ahead and using the API, I’m short on ideas.

I took a quick look at UI scripting it, but I had no luck.

tell application "Finder"
	activate
	delay 1
end tell

tell application "System Events"
	tell application process "Finder"
		set theItem to text field 1 of UI element 1 of row 14 of outline 1 of scroll area 1 of splitter group 1 of splitter group 1 of window "Dropbox"
		tell theItem to perform action "AXShowMenu"
	end tell
end tell

This script does not return any error, but it also doesn’t open the contextual menu, and I don’t know why it doesn’t work. I thought I could open the menu that way, then script selecting the item to copy the Dropbox link to the clipboard.

Even if this did work, you’d need scripting to ensure the window is open, in a consistent view, and then determine the “row” of your item in the Finder view.

I also took a quick look at the formatting of the Dropbox links, wondering if you could just construct them yourself from information you could get, but that didn’t appear to be the case.

To me, it looks like the API is the best bet, although I’m afraid it’s probably more programming than you were hoping for.

thank you so much for trying…

It does seem like too much programming. Maybe I will contact Dropbox and see if they are willing to help

I searched here and could find an answer. You would think this would have come up before

Kevin

Browser: Safari 602.3.12
Operating System: Mac OS X (10.10)

i wanted to do this exact same thing.

Here is my solution. I’ve been testing it for a couple days, and it works quite reliably. I am open to suggestions and improvements.

Let me know what you think.

Totally Easy Steps to get access token… (the google website changes sometimes, these are the steps right now today)

  1. Log in to developer.dropbox.com (you don’t need a developer account, just login to dropbox like regular and go to this developer page)
  2. Click on Create Apps
  3. Choose an API: Dropbox
  4. Choose the type of access (i use “Full Dropbox” but you do you)
  5. Name your app (you will not be using the name in the AppleScript, so just name it something you will remember)
  6. Generate Access Token. (Copy that, paste it into the script, and do not share it with anyone.)

I made a droplet…

those ridiculous \\ are required to escape the escape " that is required for the curl… that said, there’s a LOT of double and single quotes and escaped characters in those commands–paste carefully and triple check the syntax.
replace [PUT ACCESS TOKEN HERE], [USERNAME], and of course change the /test/ directory to your desired path

on open some_items
	
	set theDropBoxBearerToken to "[PUT ACCESS TOKEN HERE]"
	
	repeat with this_item in some_items
		
		set TheSourceFile to (quoted form of POSIX path of this_item)
		
		tell application "Finder" to set theFilename to (the name of (this_item as alias) as string)
		
		set WhereTheFileGoes to "test/" & theFilename
		
		set WhereTheFileIsLocally to "/Users/[USERNAME]/Dropbox/test/" & theFilename --assuming a standard Dropbox install
		
		--REMOVE PRIOR (i often update-and-repost-files; if you don't, you can remove this.) (if there's no existing file, it just does nothing)
		do shell script "curl -X POST https://api.dropboxapi.com/2/files/delete_v2 --header \"Authorization: Bearer " & theDropBoxBearerToken & "\" --header \"Content-Type: application/json\" --data \"{\\\"path\\\": \\\"/" & WhereTheFileGoes & "\\\"}\""
		
		--UPLOAD CURRENT
		do shell script "curl -sX POST https://content.dropboxapi.com/2/files/upload --header \"Authorization: Bearer " & theDropBoxBearerToken & "\" --header \"Dropbox-API-Arg: {\\\"path\\\": \\\"/" & WhereTheFileGoes & "\\\",\\\"mode\\\": \\\"overwrite\\\",\\\"autorename\\\": false,\\\"mute\\\": false}\" --header \"Content-Type: application/octet-stream\" --data-binary @" & TheSourceFile
		
		--GET INFO
		set TheRawDropBoxInfo to {do shell script "curl -s -X POST https://api.dropboxapi.com/2/sharing/create_shared_link_with_settings --header \"Authorization: Bearer " & theDropBoxBearerToken & "\" --header \"Content-Type: application/json\" --data \"{\\\"path\\\": \\\"/" & WhereTheFileGoes & "\\\",\\\"settings\\\": {\\\"requested_visibility\\\": \\\"public\\\"}}\""}
		
		--PARSE LINK FROM INFO
		set theDropBoxLink to {do shell script "grep -Eo 'https://www.dropbox.com(/[[:graph:]]*)?' <<< '" & TheRawDropBoxInfo & "' | cut -d\\\" -f1"}
		
		display dialog theDropBoxLink
		--do stuff here. my goal is: make email with attachment:WhereTheFileIsLocally and link:theDropBoxLink
		
	end repeat
	
end open

Thanks, it’s great to see some Applescript examples and instructions for interfacing with the Dropbox API.


set theDropboxFolder to (path to home folder as text) & "Dropbox:"
-- REPLACE HERE TESTING FILE WITH YOUR OWN REAL EXISTING FILE
set theFile to (theDropboxFolder & "USB_VLC_Player.workflow.zip") as alias

-- NOTE: Here I PROCESS FOR SIMPLITY ONLY 1 FILE
-- YOU CAN LIST ALL FILES AND FOLDERS OF "DROPBOX" folder 
-- and process them USING REPEAT LOOP 

tell application "Finder"
	-- BRING TO FRONT "DROPBOX" folder 
	if front Finder window exists then
		set target of front Finder window to theDropboxFolder
	else
		open theDropboxFolder
	end if
	-- set property SELECTION of FINDER to processed file (or folder)
	set the selection to theFile
end tell

do shell script "printf %s \"\" | pbcopy" -- CLEAR THE CLIPBOARD

tell application "System Events"
	tell application process "Finder"
		-- RIGHT_CLICKING TO OPEN NEEDED MENU
		set _selection to value of attribute "AXFocusedUIElement"
		tell _selection to perform action "AXShowMenu"
		-- WAIT UNTIL NEEDED WINDOW BECOMES ACTIVE
		repeat until window "Dropbox" exists
			delay 0.1
		end repeat
		-- COPY LINK TO THE CLIPBOARD
		click menu item "Copy Dropbox Link" of menu 1 of outline 1 of scroll area 1 of splitter group 1 of splitter group 1 of window "Dropbox"
	end tell
end tell

-- WAIT UNTIL THE CLIPBOARD GETS the URL 
set theURL to ""
repeat while (theURL = "")
	try
		delay 0.1
		set theURL to (the clipboard as text)
	end try
end repeat

KniazidisR

Brilliant, thanks. I’d tried, and I know others had too, and we couldn’t get that UI scripting to work.

I think your script will be a good example for other UI scripting issues I’ve had too.

hi thanks for all the info.I finally got around to testing this

I made the following changes

set theDropboxFolder to (path to home folder as text) & “Dropbox:AllynDocuments:Client Audio files:”
– REPLACE HERE TESTING FILE WITH YOUR OWN REAL EXISTING FILE use toBurnSound cell from Filemaker cell
set theFile to (theDropboxFolder & “BeckyMiller6304.m4a”) as alias

repeat until window “Client Audio files” exists
delay 0.1
end repeat
– COPY LINK TO THE CLIPBOARD
click menu item “Copy Dropbox Link” of menu 1 of outline 1 of scroll area 1 of splitter group 1 of splitter group 1 of window “Client Audio files”

I made these changes as the file I need is nested in Dropbox:AllynDocuments:Client Audio files:

the code fails at click menu item line with the following error

error “System Events got an error: Can’t get menu item "Copy Dropbox Link" of menu 1 of outline 1 of scroll area 1 of splitter group 1 of splitter group 1 of window "Client Audio files" of application process "Finder".” number -1728 from menu item “Copy Dropbox Link” of menu 1 of outline 1 of scroll area 1 of splitter group 1 of splitter group 1 of window “Client Audio files” of application process “Finder”

any thoughts on what I did wrong

thank you

I used my UI Browser to see if the reference changed, but it was the same reference. I don’t understand why it won’t click the “copy Dropbox link”.

Hi.

Here’s a modification of KniazidisR’s script which works on my Mojave machine. Dropbox’s own items in the contextual menu now seem to be hidden from System Events. But they do still respond to keystrokes. This version doesn’t need to be edited if the file or folder isn’t in the top level of the Dropbox folder.

-- A delay to allow time to release the Command key if using Command-R to run the script.
delay 1

set theDropboxFolder to (path to home folder as text) & "Dropbox:"
-- REPLACE HERE TESTING FILE WITH YOUR OWN REAL EXISTING FILE
set theFile to (theDropboxFolder & "Getting Started.pdf") as alias

-- NOTE: Here I PROCESS FOR SIMPLITY ONLY 1 FILE
-- YOU CAN LIST ALL FILES AND FOLDERS OF "DROPBOX" folder 
-- and process them USING REPEAT LOOP 

-- Reveal and select the specified item.
tell application "Finder" to reveal theFile

do shell script "printf %s \"\" | pbcopy" -- CLEAR THE CLIPBOARD

tell application "System Events"
	tell application process "Finder"
		set frontmost to true
		-- RIGHT_CLICKING TO OPEN NEEDED CONTEXTUAL MENU
		set _selection to value of attribute "AXFocusedUIElement"
		tell _selection to perform action "AXShowMenu"
		-- WAIT UNTIL THE MENU BECOMES ACTIVE
		tell menu 1 of _selection
			repeat until (it exists)
				delay 0.1
			end repeat
		end tell
	end tell
	
	-- Select the first menu item beginning with "c" (ie. "Copy Dropbox Link").
	-- THIS WON'T WORK IF ANY MODIFIER KEYS ARE BEING HELD DOWN AT THIS INSTANT.
	keystroke "c"
	keystroke return
end tell

-- WAIT UNTIL THE CLIPBOARD GETS the URL 
set theURL to ""
repeat while (theURL = "")
	try
		delay 0.1
		set theURL to (the clipboard as text)
	end try
end repeat