Scheduled URL Launcher AppleScript

This AppleScript prompts the user to input multiple URLs and offers a choice to open them immediately or after a countdown timer. For immediate launches, it confirms the URLs before opening, while for countdown mode it notifies the user that the countdown has started and then provides a final confirmation after the URLs have been opened.

Click to view script…
-- Create a large default text field by including multiple line breaks
set multiLineDefault to return & return & return & return

-- Prompt the user for a list of URLs (one URL per line)
set urlInput to text returned of (display dialog "Enter list of URLs (one per line):" default answer multiLineDefault)

-- Split the input into a list of lines
set urlList to paragraphs of urlInput

-- Ask the user if they want to open the URLs now or after a countdown
set schedulingOption to button returned of (display dialog "Choose when to open the URLs:" buttons {"Now", "Countdown"} default button "Now")

if schedulingOption is "Now" then
	-- Build a confirmation message listing the URLs to be opened immediately
	set confirmDialogText to "The following URLs will be opened now:" & return & return
	repeat with aURL in urlList
		set trimmedURL to trimWhitespace(aURL)
		if trimmedURL is not "" then
			-- Prepend protocol if missing
			if (trimmedURL does not start with "http://") and (trimmedURL does not start with "https://") then
				set trimmedURL to "https://" & trimmedURL
			end if
			set confirmDialogText to confirmDialogText & trimmedURL & return
		end if
	end repeat
	
	-- Show confirmation dialog before launching
	set confirmResponse to button returned of (display dialog confirmDialogText buttons {"Cancel", "Open Now"} default button "Open Now")
	if confirmResponse is "Cancel" then return
	
	-- Open URLs immediately
	repeat with aURL in urlList
		set trimmedURL to trimWhitespace(aURL)
		if trimmedURL is not "" then
			if (trimmedURL does not start with "http://") and (trimmedURL does not start with "https://") then
				set trimmedURL to "https://" & trimmedURL
			end if
			open location trimmedURL
		end if
	end repeat
	
else if schedulingOption is "Countdown" then
	-- Prompt for countdown seconds
	set countdownInput to text returned of (display dialog "Enter countdown time in seconds:" default answer "10")
	try
		set countdownSeconds to countdownInput as integer
	on error
		display dialog "Invalid number. Exiting." buttons {"OK"} default button "OK"
		return
	end try
	
	-- Notify the user that the countdown is starting
	display dialog "Countdown started for " & countdownSeconds & " seconds. The URLs will open automatically afterwards." buttons {"OK"} default button "OK"
	
	-- Delay for the specified number of seconds
	delay countdownSeconds
	
	-- Open URLs after the delay
	repeat with aURL in urlList
		set trimmedURL to trimWhitespace(aURL)
		if trimmedURL is not "" then
			if (trimmedURL does not start with "http://") and (trimmedURL does not start with "https://") then
				set trimmedURL to "https://" & trimmedURL
			end if
			open location trimmedURL
		end if
	end repeat
	
	-- Build a final confirmation message listing the URLs that were opened
	set finalConfirmDialogText to "The following URLs have been opened:" & return & return
	repeat with aURL in urlList
		set trimmedURL to trimWhitespace(aURL)
		if trimmedURL is not "" then
			if (trimmedURL does not start with "http://") and (trimmedURL does not start with "https://") then
				set trimmedURL to "https://" & trimmedURL
			end if
			set finalConfirmDialogText to finalConfirmDialogText & trimmedURL & return
		end if
	end repeat
	
	-- Show the final confirmation dialog
	display dialog finalConfirmDialogText buttons {"OK"} default button "OK"
end if

--------------------------------------------------------------------------------
-- Helper Handler: trimWhitespace
-- Removes leading and trailing whitespace from a string
--------------------------------------------------------------------------------
on trimWhitespace(theText)
	set AppleScript's text item delimiters to {" ", tab, return, linefeed}
	set textItems to text items of theText
	set AppleScript's text item delimiters to space
	set trimmed to textItems as text
	set AppleScript's text item delimiters to ""
	return trimmed
end trimWhitespace