==UPDATED==: 2019-06-20 20:08 GMT-5
- Cleanup thread
- Upload revised Script, Ver 2.5.3
- Add file path verification
- Revise to with with PF8
- Now works with both Finder & PathFinder (PF8).
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Chris, unfortunately that is no longer true, ==as of 2017-09-01, Dropbox has eliminated use of the Public Folder==. And now, the "Copy Dropbox Link" does not seem to be able to be automated without UI scripting, because the root URL changes for every file. OTOH, if you know a better way -- I'm all for it.
So, based on your UI script, ==I've written a new one that uses the Dropbox button/menu item in the Finder.== Things seem to have changed since your script two years ago, and the Dropbox group is no longer in the same location. My script does not depend on group location.
AppleScript To Get New Dropbox Public Link (post 2017-09-01)
(this is NOT the same as the old Dropbox Public Folder link).
• Since 2017-09-01 public links have been disabled for all DropBox users
• So the prior methods of getting a Public Link not longer work.
• This is based on the DropBox UI in the Finder Toolbar.
property ptyScriptName : "Get Dropbox Public Link of Selected Finder Item"
property ptyScriptVer : "2.5.3" -- CHG revealInFinder Handler for PF8
property ptyScriptDate : "2019-06-20"
property ptyScriptAuthor : "JMichaelTX" -- based on script by @ccstone
(*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PURPOSE:
• Get the Public Link for any DropBox item currently selected in the Finder
• Since 2017-09-01 public links have been disabled for all DropBox users
• So the prior methods of getting a Public Link not longer work.
• This is based on the DropBox UI in the Finder Toolbar.
RETURNS:
• URL of Public Dropbox item to the Clipboard
• URL is set with "dl=1" to auto-download
UNLESS the file kind contains a keyword in the property viewKWList;
THEN it is set to "dl=0"
• Script returns Markdown link of same
REQUIRED:
1. macOS 10.11.6+
2. Mac Applications
• Dropbox
REF: The following were used in some way in the writing of this script.
1. https://www.dropbox.com/help/files-folders/public-folder
2. Based on script by Chris Stone (@ccstone)
https://forum.keyboardmaestro.com/t/how-do-i-execute-dropbox-copy-public-link/1772/7
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*)
use framework "Foundation"
use scripting additions
property LF : linefeed
try
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
set testPF to false
set frontApp to path to frontmost application as text -- use for dialogs
if ((frontApp contains "Path Finder") or testPF) then
--- Reveal Item Selected in PF in the Finder ---
-- (DropBox Menu works only in Finder)
set fileAlias to revealPFItemInFinder()
end if
set maxWait to 2.0 -- sec
set delayInt to 1.0E-3
set elapsedTime to 0.0
set cbTest to "WAITING"
set the clipboard to cbTest
tell application "Finder" to set finderSelectionList to selection as alias list
if length of finderSelectionList = 0 then error "No files were selected in the Finder!"
set fileAlias to item 1 of finderSelectionList
set filePath to POSIX path of fileAlias
if (filePath does not contain "/Dropbox/") then
error "fItem MUST in the the Dropbox folders. Invalid path:" & linefeed & filePath
end if
set oFile to info for fileAlias
tell oFile
set fileName to name
set fileSize to size
set fileKind to kind
end tell
set fileSize to (current application's NSByteCountFormatter's stringFromByteCount:fileSize countStyle:(current application's NSByteCountFormatterCountStyleDecimal)) as text
tell application "System Events"
tell application process "Finder"
set frontWin to (first window whose role is "AXWindow")
tell frontWin
set dbGrp to first group of first toolbar whose help contains "Dropbox"
# Alternate Method #
(*
IF you prefer speed over reliability, then use next statement instead of prior.
Position of the Dropbox Group/Menu may vary by macOS and how you have configured the Finder.
(uses < 0.01 sec less time in my testing, but YMMV)
set dbGrp to group 5 of toolbar 1
*)
tell dbGrp
tell menu button 1
perform action "AXPress"
tell menu 1
--- Delay Until Menu Item Shows ---
repeat until (exists menu item "Copy Dropbox Link")
delay delayInt -- short delay needed to show menu
set elapsedTime to elapsedTime + delayInt
if (elapsedTime > maxWait) then error ("Max Time Exceeded Waiting for Dropbox Menu to show: " & elapsedTime)
end repeat
click menu item "Copy Dropbox Link" -- to the Clipboard
end tell
end tell -- menu button 1
end tell -- dbGrp
end tell -- frontWin
end tell
end tell
--- Delay Until Link is on Clipboard ---
(*
NOTE: You wouldn't think this is necessary, but in my testing
I found it could take almost 1 sec for the Dropbox Mac
service to put the link on the clipboard.
*)
set elapsedTime to 0.0
set dbLink to "TBD"
repeat until (dbLink starts with "http")
delay delayInt -- short delay needed for DropBox Service
try
set dbLink to the clipboard as text
on error
set dbLink to "TBD"
end try
set elapsedTime to elapsedTime + delayInt
if (elapsedTime > maxWait) then error ("Max Time Exceeded Waiting for Dropbox Link to be put on Clipboard: " & elapsedTime)
end repeat
if (my isFileTypeAVI(fileAlias) is false) then
--- Change the DropBox Link to Auto-Download ---
set dbLink to «event SATIRPLl» "dl=0" given «class by »:"dl=1", «class $in »:dbLink
set mdTextPrefix to "Download: "
else --- File Type is either Audio, Video, or Image ---
set mdTextPrefix to "View/Download: "
end if
set the clipboard to dbLink as text
--- Build & Return Markdown Link ---
set mdLink to "[" & mdTextPrefix & fileName & " (" & fileSize & ")](" & dbLink & ")"
set scriptResults to mdLink
--~~~~~~~~~~~~~ END TRY ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
on error errMsg number errNum
if errNum = -128 then ## User Canceled
set errMsg to "[USER_CANCELED]"
end if
set scriptResults to "[ERROR]" & return & errMsg & return & return ¬
& "SCRIPT: " & ptyScriptName & " Ver: " & ptyScriptVer & return ¬
& "Error Number: " & errNum
end try
--~~~~~~~~~~~~~~~~END ON ERROR ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
return scriptResults
--~~~~~~~~~~~~~~~~~~ END OF MAIN SCRIPT ~~~~~~~~~~~~~~~~~
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
on isFileTypeAVI(pFileAlias)
--–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
(* VER: 1.0 2017-12-25
PURPOSE: Determine if file is any of these top level types:
Audio, Video (Movie), Image, PDF
PARAMETERS:
• pFileAlias | alias | File to be evaluated for file type
RETURNS: true/false
AUTHOR: JMichaelTX (refactor of script by Shane Stanley)
## REQUIRES: use framework "Foundation"
REF: @ShaneStanley, Late Night Software Forum, script
http://forum.latenightsw.com/t/handler-to-determine-if-text-contains-item-in-list/893/2
--–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
*)
tell current application's NSWorkspace's sharedWorkspace()
set theUTI to its typeOfFile:(POSIX path of pFileAlias) |error|:(missing value)
return (its |type|:theUTI conformsToType:"public.movie") ¬
or (its |type|:theUTI conformsToType:"public.audio") ¬
or (its |type|:theUTI conformsToType:"public.image") ¬
or (its |type|:theUTI conformsToType:"com.adobe.pdf")
end tell
end isFileTypeAVI
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
on doesTextContainAnyListItem(pText, pList)
--–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
(* VER: 1.0 2017-12-23
PURPOSE: Determine if string (text) contains any item in the List
PARAMETERS:
• pText | text | Text to search
• pList | list | List of text items to search for
RETURNS: true IF any item in the list was found in the text
false IF NOT
AUTHOR: JMichaelTX
--–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
*)
set foundItemBool to false
repeat with aItem in pList
if pText contains (contents of aItem) then
set foundItemBool to true
exit repeat
end if
end repeat
return foundItemBool
end doesTextContainAnyListItem
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
on revealPFItemInFinder()
--–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
(* VER: 2.1 2018-03-19
PURPOSE: Reveal Item in Finder that is Selected in Path Finder
RETURNS: alias of item selected in both Finder and Path Finder
AUTHOR: JMichaelTX
--–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
*)
local finWinName, pfWinName, fileList, itemPath, oItem
--- GET THE ITEM SELECTED IN PATH FINDER ---
tell application "Path Finder"
set fileList to (get selection)
if ((fileList is missing value) or ((count of fileList) ≠ 1)) then error ("You must select only ONE file in Path Finder.")
set oItem to item 1 of fileList
set itemPath to POSIX path of oItem
set pfWinName to name of container of oItem
end tell
set itemAlias to alias POSIX file itemPath
--- REVEAL SAME ITEM IN FINDER ---
tell application "Finder"
activate -- to make sure reveal will be in frontmost window
reveal itemAlias
--- Now Wait for New Finder Window with Same Name as Path Finder ---
tell application "System Events" to set currentAppName to name of first application process whose frontmost is true
set maxWaitTime to 3.0
set delayTime to 0.1
set waitTime to 0
repeat while currentAppName ≠ "Finder"
delay delayTime
tell application "System Events" to set currentAppName to name of first application process whose frontmost is true
set waitTime to waitTime + delayTime
if (waitTime > maxWaitTime) then error "Max wait time of " & maxWaitTime & " exceeded waiting for Finder Window of " & pfWinName
end repeat
end tell
return itemAlias
end revealPFItemInFinder