maestral
maestral copied to clipboard
Maestral CLI not working outside of terminal on MacOS
Describe the bug I created a shortcut to run as a quick action on files and folders.
The shortcut took the filepath of the file being acted on, and tried to make a sharelink for it; if a sharelink already existed, then got the existing link, and then copied the output to the clipboard for pasting into other apps.
It's stopped working, and gives 'Exit code 1' when the shell script code executes.
I also have a version of the code which ran as an applescript, and it doesn't work - the script exits with an error; the script output doesn't get returned.
Yet when I run the command '/opt/homebrew/bin/maestral sharelink create ' with the filepath in terminal directly, everything works great :-(
To Reproduce
The applescript code is:
set thepath to POSIX path of (choose folder) if thepath does not contain "Dropbox" then return "Not a dropbox file" set the clipboard to "" if character -1 of thepath is "/" then set thepath to text ((offset of "/{my dropbox name}/" in thepath) + 1) thru -2 of thepath else set thepath to text ((offset of "/{my dropbox name}/" in thepath) + 1) thru -1 of thepath end if set thetext to (do shell script ("/opt/homebrew/bin/maestral sharelink create " & quoted form of thepath)) if thetext contains "already exists" then set thetext to (do shell script ("/opt/homebrew/bin/maestral sharelink list " & quoted form of thepath)) if thetext is "" then return "Couldn't copy link :-(" else set the clipboard to thetext return "Dropbox Link copied!" end if
Expected behaviour
I'd expect a dropbox link to be generated and placed on the clip board
System:
- Maestral version: 1.7.3
- Python version: 5.15.7
- OS: [e.g. Ubuntu] MacOS 13.4.1
Additional context
First, I am assuming that in your actual script, you aren't using the placeholder "{my dropbox name}" as the name of your Dropbox root.
The problem is that you're not constructing the path properly. This line:
set thepath to text ((offset of "/Dropbox/" in thepath) + 1) thru -2 of thepath
only clips the leading "/" before "Dropbox", so you're left with a path like "Dropbox/MyFolder/", but the maestral
tool expects the path to start after the Dropbox root. offset of "/Dropbox/" in thepath
gives you the offset to where the substring starts, not ends, in thepath
.
You're also handling the "shared link already exists" error incorrectly. Here's a version of your script that works for me:
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
-- Should be something like "/Users/yourname/Dropbox/":
set dropbox_root to (get POSIX path of (path to home folder)) & "Dropbox/"
-- This might be something else, like "/opt/homebrew/bin/maestral":
set maestral_tool to "/usr/local/bin/maestral"
set thepath to POSIX path of (choose folder)
if thepath does not start with dropbox_root then return "Not a Dropbox file"
set thepath to text (length of dropbox_root) thru end of thepath
-- Maestral CLI tool allows optional leading slash, but not a trailing slash:
if character -1 of thepath is "/" then
set thepath to text 1 thru -2 of thepath
end if
try
set thetext to do shell script (maestral_tool & " sharelink create " & quoted form of thepath)
on error errMsg
if errMsg contains "The shared link already exists." then
set thetext to do shell script (maestral_tool & " sharelink list " & quoted form of thepath)
else
return errMsg
end if
end try
set the clipboard to thetext
return "Dropbox Link copied"
Indeed, thank you @gruber for the detailed explanation and posting a working version of the script!