Search code examples
applescriptdropbox

Applescript click 'share dropbox link' using contextual menu


I am trying to access the 'share dropbox link' item the contextual menu of a specific file in a specific dropbox folder using AXShowMenu.

I have found the following script. Via Applescript right click a file

tell application "System Events"
    tell process "Finder"
     set target_index to 1
     set target to image target_index of group 1 of scroll area 1
     tell target to perform action "AXShowMenu"
    end tell
end tell

Which seem to do the trick for desktop items,

but how can I use this to target specific files in folders then click the 'share dropbox link' in the contextual menu?


Solution

  • A little kludgey, but it does work. This requires you have the dropbox folder open and frontmost in Finder.

    tell application "Finder"
        activate --brings Finder to front
        select item 1 of window 1 --selects the first item in the list ; not sure how you want to select/determine the item
    end tell
    delay 0.5 --this is to insure you don't get unwanted typing in script editor (makes sure Finder activates before kludgey typing gets done)
    tell application "System Events"
        tell application process "Finder"
            set _selection to value of attribute "AXFocusedUIElement" --focused element (selected element)
            tell _selection to perform action "AXShowMenu" --contextual menu
        end tell
        keystroke "share dropbox link" --type to select contextual menu item
        keystroke return --select it by hitting return
    end tell
    

    [EDIT] Here's a trick to make sure your dropbox folder is open and frontmost in the Finder:

    tell application "Finder"
        activate --brings Finder to front
        tell me to set dbFolder to POSIX file ((do shell script "cd ~/Dropbox/;pwd") & "/")
        open dbFolder
        select item 1 of window 1 --selects the first item in the list ; not sure how you want to select/determine the item
    end tell
    
    tell application "System Events"
        tell application process "Finder"
            set _selection to value of attribute "AXFocusedUIElement" --focused element (selected element)
            tell _selection to perform action "AXShowMenu" --contextual menu
        end tell
        keystroke "share dropbox link" --type to select contextual menu item
        keystroke return --select it by hitting return
    end tell
    

    There may be another way of selecting the contextual menu, but this is what I came up with so far late at night after a glass of wine.