try
	-- Prompt the user to choose multiple files.
	set chosenFiles to choose file with prompt "Select the files you want to include in the password-protected ZIP file..." with multiple selections allowed
	
	-- Convert file aliases to POSIX paths.
	set posixChosenFiles to {}
	repeat with chosenFile in chosenFiles
		set posixChosenFile to POSIX path of chosenFile
		set end of posixChosenFiles to posixChosenFile
	end repeat
	
	-- Define the destination ZIP file path.
	set zipFilePath to choose file name with prompt "Choose a name and location for the password-protected ZIP file:" default name "Archive.zip"
	
	-- Convert the ZIP file path to a POSIX path.
	set posixZipFilePath to POSIX path of zipFilePath
	
	-- Prompt the user to enter a password.
	display dialog "Enter a password for the ZIP file:" default answer "" buttons {"Cancel", "OK"} default button "OK" with hidden answer
	set zipPassword to text returned of the result
	
	-- Only runs if ZIP password is defined.
	if zipPassword is not equal to "" then
		set filesToZip to my joinListWithQuotes(posixChosenFiles, " ")
		
		-- Shell script logic.
		do shell script "zip -P " & zipPassword & " -rj " & posixZipFilePath & " " & filesToZip
		
		-- Delay after running script logic.
		delay 1
		
		-- Check if the zip file was created successfully.
		if (do shell script "[ -e " & quoted form of posixZipFilePath & " ] && echo 'true' || echo 'false'") is equal to "true" then
			display dialog "ZIP file created successfully!" buttons {"OK"} default button "OK"
		else
			display dialog "Error creating ZIP file." buttons {"OK"} default button "OK" with icon stop
		end if
	else
		display dialog "Error creating ZIP file. Password is required." buttons {"OK"} default button "OK" with icon stop
	end if
	
on error errMsg
	if errMsg does not contain "User canceled." then
		display dialog errMsg buttons {"OK"} default button "OK" with icon stop
	end if
end try

-- Join list with quotes function.
on joinListWithQuotes(theList, delimiter)
	set theResult to ""
	repeat with i from 1 to count of theList
		set theResult to theResult & "\"" & item i of theList & "\""
		if i is not equal to (count of theList) then
			set theResult to theResult & delimiter
		end if
	end repeat
	return theResult
end joinListWithQuotes
