Retrieve Downland link from icloud

Hi
I am looking for an AppleScript method to upload a file to iCloud and then receive a link that can be emailed in order to share the file.

I can script the uploading of the file and the email part

What I don’t know is how to automate the retrieval of a download link

I appreciate that you’d prefer to see current attempts at scripting but I haven’t got a clue where to start on this one

Is anyone able to point me in the right direction

I don’t even know how to retrieve or get a download link in any form!!

There are “share” options but nothing like Dropbox or google drive links:.

With the latest Catalina update, this is easy. Just make one folder an iCloud folder. Or use Desktop-iCloud. Next comes the GUI scripting.

Now I’m busy, but as I get free, I’ll try to write an sample script.

Hi, all.

I made Desktop-iCloud folder which is easy with new Catalina. That is, what files I copy to this folder is uploaded to iCloud automatically. Here I wrote sample script to retrieve Downland link from iCloud to the clipboard. The principle is momentary sharing the chosen file with me, to be able then to retrieve the link. I will enhance my script. But it works already, as is:


set desktop_iCloude to alias "HARD_DISK:Users:123:Desktop:" -- my iCloud folder
set aFile to alias "HARD_DISK:Users:123:Desktop:Desktop – MacBook Pro του/της 123:Alexandra Downloads:02 - Color.pdf" -- chosen file in this folder

tell application "Finder"
	activate
	reveal aFile
	set fileName to name of aFile
end tell

tell application "System Events" to tell process "Finder"
	repeat until window 1 exists
		delay 0.02
	end repeat
	tell window 1
		set theRows to rows of outline 1 of scroll area 1 of splitter group 1 of splitter group 1
		repeat with aRow in theRows
			try
				if value of text field 1 of UI element 1 of aRow is fileName then exit repeat
			end try
		end repeat
		tell text field 1 of UI element 1 of aRow
			perform action "AXShowMenu"
		end tell
		repeat until menu 1 of outline 1 of scroll area 1 of splitter group 1 of splitter group 1 exists
			delay 0.02
		end repeat
		click menu item "Share" of menu 1 of outline 1 of scroll area 1 of splitter group 1 of splitter group 1
		click menu item "Add People" of menu 1 of menu item "Share" of menu 1 of outline 1 of scroll area 1 of splitter group 1 of splitter group 1
	end tell
	repeat until window "Add People" exists
		delay 0.02
	end repeat
	tell window "Add People"
		click UI element "Share Options"
		click pop up button 1
		click menu item "Anyone with the link" of menu 1 of pop up button 1
		click UI element "Share" -- share with me, because only I have this file currently
	end tell
end tell

-- I don't need to send this file to me, so I will close Mail window here
tell application "System Events" to tell process "Finder"
	repeat until window "Mail" exists
		delay 0.02
	end repeat
	click button "Cancel" of window "Mail"
	repeat while window "Mail" exists
		delay 0.02
	end repeat
end tell

-- now this file is already shared on the iCloud, so we can get the link
tell application "System Events" to tell process "Finder"
	repeat until window 1 exists
		delay 0.02
	end repeat
	tell window 1
		set theRows to rows of outline 1 of scroll area 1 of splitter group 1 of splitter group 1
		repeat with aRow in theRows
			try
				if value of text field 1 of UI element 1 of aRow is fileName then exit repeat
			end try
		end repeat
		tell text field 1 of UI element 1 of aRow
			perform action "AXShowMenu"
		end tell
		repeat until menu 1 of outline 1 of scroll area 1 of splitter group 1 of splitter group 1 exists
			delay 0.02
		end repeat
		click menu item "Share" of menu 1 of outline 1 of scroll area 1 of splitter group 1 of splitter group 1
		click menu item "Show People" of menu 1 of menu item "Share" of menu 1 of outline 1 of scroll area 1 of splitter group 1 of splitter group 1
	end tell
	repeat until window 1 exists
		delay 0.02
	end repeat
	tell window 1
		delay 1
		click UI element "Share Options" of group 1 of group 1 of sheet 1
		delay 1
		click UI element "Copy Link" of group 1 of group 1 of sheet 1
		delay 1
		-- stop sharing, because we have the link already
                click UI element "Stop Sharing" of group 1 of group 1 of sheet 1 of window 1
		delay 1
		click UI element "OK" of window 1
		delay 1
	end tell
end tell

tell application "Finder" to close windows

-- RESULT: the iCLOUD LINK is in the clipboard now

Enhanced, stable version. And, unlike the previous script, this one works with already shared files too:


set desktop_iCloude to path to desktop folder -- my iCloud folder
set aFile to choose file default location desktop_iCloude -- chosen file in this folder

tell application "Finder"
	select aFile
	set {fileName, containerName} to {name of aFile, name of container of aFile}
end tell

tell application "System Events" to tell application process "Finder"
	
	repeat until window containerName exists
		delay 0.02
	end repeat
	tell window containerName
		set theRows to rows of outline 1 of scroll area 1 of splitter group 1 of splitter group 1
		repeat with aRow in theRows
			tell (UI element 1 of aRow) to if text field 1 exists then if (value of text field 1) is fileName then exit repeat
		end repeat
		tell text field 1 of UI element 1 of aRow to perform action "AXShowMenu"
		repeat until menu 1 of outline 1 of scroll area 1 of splitter group 1 of splitter group 1 exists
			delay 0.02
		end repeat
		tell menu item "Share" of menu 1 of outline 1 of scroll area 1 of splitter group 1 of splitter group 1
			click
			repeat until menu 1 exists
				delay 0.02
			end repeat
			tell menu 1
				if menu item "Add People" exists then
					click menu item "Add People"
					set isNotShared to true
				else
					click menu item "Show People"
					set isNotShared to false
				end if
			end tell
		end tell
	end tell
	
	if isNotShared then
		repeat until window "Add People" exists
			delay 0.02
		end repeat
		tell window "Add People"
			click button "Share Options"
			repeat until pop up button 1 exists
				delay 0.02
			end repeat
			tell pop up button 1 to if its value is "Only people you invite" then
				click
				repeat until menu 1 exists
					delay 0.02
				end repeat
				click menu item "Anyone with the link" of menu 1
			end if
			click button "Share" -- share with me, because only I have this file currently
		end tell
		-- I don't need to send this file to me, so I will close Mail window here
		repeat until window "Mail" exists
			delay 0.02
		end repeat
		repeat until button "Cancel" of window "Mail" exists
			delay 0.02
		end repeat
		click button "Cancel" of window "Mail"
		repeat while window "Mail" exists
			delay 0.02
		end repeat
		tell window containerName
			tell text field 1 of UI element 1 of aRow to perform action "AXShowMenu"
			repeat until menu 1 of outline 1 of scroll area 1 of splitter group 1 of splitter group 1 exists
				delay 0.02
			end repeat
			tell menu item "Share" of menu 1 of outline 1 of scroll area 1 of splitter group 1 of splitter group 1
				click
				repeat until menu 1 exists
					delay 0.02
				end repeat
				tell menu 1 to click menu item "Show People"
			end tell
		end tell
	end if
	
	-- now this file is already shared on the iCloud, so we can get the link
	repeat until window containerName exists
		delay 0.02
	end repeat
	tell window containerName
		repeat until button "Share Options" of group 1 of group 1 of sheet 1 exists
			delay 0.02
		end repeat
		tell sheet 1 to tell group 1 to tell group 1
			click button "Share Options"
			repeat button "Copy Link" exists
				delay 0.02
			end repeat
			click button "Copy Link"
		end tell
	end tell
	if isNotShared then
		-- restore sharing state, because we have the link already
		click button "Stop Sharing" of group 1 of group 1 of sheet 1 of window containerName
		repeat until (first window whose description is "dialogue") exists
			delay 0.02
		end repeat
		click button "OK" of window 1
	else
		click button "Done" of group 1 of group 1 of sheet 1 of window containerName
	end if
end tell

delay 1
tell application "Finder" to close window containerName

-- RESULT: the iCLOUD LINK is in the clipboard now
-- https://www.icloud.com/iclouddrive/0hTyH0h57qtVrBM1sruuRUnXQ#Untitled