I simply cannot get this script to provide the the correctly formatted file path to the dropbox API, I keep getting the error that the filepath doesn't exist.
And I can't find a definitive answer on how the API needs to receive the path.
Also, what is the difference in paths between a file in the personal user area, and a file in the shared team area?
I keep getting this error:
ApiError(70782f83ba81470284d6e6905b9e828e, CreateSharedLinkWithSettingsError(path, LookupError(malformed_path, None)))
Any help apreciated!!
import dropbox
import os
from subprocess import Popen, PIPE
ACCESS_TOKEN = 'XXXXX'
def get_selected_file_in_finder():
# AppleScript to get the selected file in Finder
applescript = '''
tell application "Finder"
set theSelection to selection as alias list
set theItem to item 1 of theSelection
return POSIX path of (theItem as text)
end tell
'''
process = Popen(['osascript', '-e', applescript], stdout=PIPE)
stdout, stderr = process.communicate()
return stdout.strip().decode('utf-8')
def get_dropbox_link(file_path, dbx):
dropbox_root = "XXXXX"
personal_folder = "XXXXX"
if file_path.startswith(dropbox_root):
relative_path = file_path[len(dropbox_root):]
relative_path = relative_path if relative_path.startswith('/') else '/' + relative_path
if relative_path.startswith(personal_folder):
dropbox_path = relative_path[len(personal_folder):] # Strip the personal folder name
else:
dropbox_path = relative_path # Team space path
else:
os.system(f'osascript -e \'display dialog "The selected file is not in the Dropbox folder."\'')
return None
os.system(f'osascript -e \'display dialog "Dropbox path: {dropbox_path}"\'') # Added logging for Dropbox path
try:
shared_link_metadata = dbx.sharing_create_shared_link_with_settings(dropbox_path)
os.system(f'osascript -e \'display dialog "API response: {shared_link_metadata}"\'') # Added logging for API response
return shared_link_metadata.url.replace("www.dropbox.com", "dl.dropboxusercontent.com").replace("?dl=0", "")
except dropbox.exceptions.ApiError as err:
if isinstance(err.error, dropbox.sharing.CreateSharedLinkWithSettingsError) and \
err.error.is_shared_link_already_exists():
os.system('osascript -e \'display dialog "Shared link already exists, retrieving the existing link."\'')
shared_links = dbx.sharing_list_shared_links(path=dropbox_path, direct_only=True)
if shared_links.links:
return shared_links.links[0].url.replace("www.dropbox.com", "dl.dropboxusercontent.com").replace("?dl=0", "")
os.system(f'osascript -e \'display dialog "Error getting Dropbox link: {err}"\'')
return None
except Exception as e:
os.system(f'osascript -e \'display dialog "Unexpected error: {e}"\'')
return None
def main():
dbx = dropbox.Dropbox(ACCESS_TOKEN)
file_path = get_selected_file_in_finder()
if not file_path:
os.system('osascript -e \'display dialog "No file selected in Finder."\'')
return
os.system(f'osascript -e \'display dialog "Selected file path: {file_path}"\'') # Added logging for file path
download_link = get_dropbox_link(file_path, dbx)
if download_link:
os.system(f'osascript -e \'display dialog "Dropbox download link: {download_link}"\'')
else:
os.system('osascript -e \'display dialog "Failed to generate Dropbox download link."\'')
os.system(f'osascript -e \'display dialog "Failed to generate Dropbox download link for file: {file_path}"\'') # Added logging for failure
if __name__ == '__main__':
main()