Having trouble with repeat loop executing properly

Hi Everyone,

I have a fairly complicated script to do a pretty simple task, use the dropbox API to get a link to a given file or folder. I’m using something called Service Station to create an item in the context menu that runs the script. The problem is, I want the script to wait for the link if the file isn’t uploaded, and then present the link when it’s done, but instead the dialog just stays there until you manually dismiss it, and it doesn’t periodically check for the link again. I’m sure it’s something simple that I’m missing, but here’s the script. I also asked chat gpt for assistance, but no luck :wink:

on serviceStationDidSelect(targetedURL, selectedItemURLs, menuKind)
	set inputPath to selectedItemURLs
	set inputPath to POSIX path of inputPath
	set outputPath to replaceText(inputPath, "/Volumes/Dropbox", "/Users/roccofiorentino/Audio Production Dropbox/Rocco Fiorentino")
	
	try
		set dropboxLink to do shell script "/Users/roccofiorentino/.pyenv/shims/python3 /Users/roccofiorentino/bin/get_dropbox_link.py '" & outputPath & "'"
		if dropboxLink contains "https://" then
			set the clipboard to dropboxLink
			do shell script "afplay ‘/System/Library/Sounds/Purr.aiff'"
		else
			display dialog "The shell script produced this output: " & dropboxLink buttons {"OK"} default button "OK"
		end if
	on error errStr
		if errStr contains "LookupError" then
			repeat
				display dialog "The file you requested hasn't finished uploading yet. Once the upload is complete, a link will be generated." buttons {"Please Wait…"} with title "Waiting for upload…"
				delay 2
				set dropboxLink to do shell script "/Users/roccofiorentino/.pyenv/shims/python3 /Users/roccofiorentino/bin/get_dropbox_link.py '" & outputPath & "'"
				set errStr to ""
				try
					if dropboxLink contains "https://" then
						set the clipboard to dropboxLink
						do shell script "afplay ‘/System/Library/Sounds/Purr.aiff'"
						exit repeat
					end if
				on error newErrStr
					set errStr to newErrStr
				end try
			end repeat
		else
			display dialog "An error occurred: " & errStr buttons {"OK"} default button "OK"
		end if
	end try
end serviceStationDidSelect

on replaceText(inputString, findText, replaceText)
	set AppleScript's text item delimiters to findText
	set itemList to every text item of inputString
	set AppleScript's text item delimiters to replaceText
	set outputString to itemList as string
	set AppleScript's text item delimiters to ""
	
	return outputString
end replaceText

Code posting backticks added by NG.

I think, you just built the repeat loop wrong. I can’t test it myself:
 

on serviceStationDidSelect(targetedURL, selectedItemURLs, menuKind)
	set inputPath to selectedItemURLs
	set inputPath to POSIX path of inputPath
	set outputPath to replaceText(inputPath, "/Volumes/Dropbox", "/Users/roccofiorentino/Audio Production Dropbox/Rocco Fiorentino")
	
	repeat -- or,- repeat 20 times - to avoid possible endless repeat loop
		try
			set dropboxLink to do shell script "/Users/roccofiorentino/.pyenv/shims/python3 /Users/roccofiorentino/bin/get_dropbox_link.py '" & outputPath & "'"
			if dropboxLink contains "https://" then
				set the clipboard to dropboxLink
				do shell script "afplay ‘/System/Library/Sounds/Purr.aiff'"
			else
				display dialog "The shell script produced this output: " & dropboxLink buttons {"OK"} default button "OK"
			end if
			exit repeat
		on error errStr
			if not (errStr contains "LookupError") then
				display dialog "An error occurred: " & errStr buttons {"OK"} default button "OK"
				exit repeat
			end if
			display dialog "The file you requested hasn't finished uploading yet. Once the upload is complete, a link will be generated." buttons {"Please Wait…"} with title "Waiting for upload…" giving up after 1
		end try
	end repeat
end serviceStationDidSelect

on replaceText(inputString, findText, replaceText)
	set AppleScript's text item delimiters to findText
	set itemList to every text item of inputString
	set AppleScript's text item delimiters to replaceText
	set outputString to itemList as string
	set AppleScript's text item delimiters to ""
	return outputString
end replaceText

 

Hi.

display dialog has an optional giving up after parameter which automatically dismisses the dialog after a given number of seconds if the user hasn’t clicked any buttons in the meantime. You could trying using this instead of the delay command:

display dialog "The file you requested hasn't finished uploading yet. Once the upload is complete, a link will be generated." buttons {"Please Wait…"} with title "Waiting for upload…" giving up after 2
-- delay 2

@rfscripter I have to retrieve a share link with dropbox and start playing with the API. Would you be wiling to share your script of the portions that do not expose credentials?

Absolutely. The Python script I’m using is from this GitHub repository:
Get Dropbox Link

1 Like