Archive Script Fails when Selected Item is a Folder

Hello Again,

This is a continuation of a previous script, but since it’s a different question altogether I thought best to create a new topic. If I did wrong in this regard please let me know.

I’m using Alfred to call the Applescript below, which creates an archive using Entropy. So, {query} is referring to the input from Alfred.

My Problem:

The archive is not created when the selected finder item is a folder. I think this has something to do with the string being passed to the variable thePath, but as I’m currently just starting with Applescript it could be anything as far as I’m concerned. (I think its a string; I’m still a little confused when it comes to string, POSIX, alias, etc).

I’m thinking that if the problem is indeed what is being passed along to thePath, then I need the script to identify when a folder is selected, and to modify the string (?) before it is received by the variable. I found this topic about checking whether a selected item is a file or a folder, but I’m uncertain how to make this work within my script, or if it would work at all.

regards,

DrLulz

tell application "Finder" to set theSel to selection as alias list

set theQ to "{query}" -- Alfred Query (Archive Name)

if (count of theSel) is 1 then
	if theQ is "" then
		set shortName to the POSIX path of theSel & ".zip" -- If Query is Empty, Use Name and Path of Item
		set thePath to text 1 thru ((offset of "." in shortName) - 1) of shortName & ".zip" -- Remove File Extension from Archive Name
	else
		set thePath to ((path to desktop from user domain) as string) & theQ & ".zip" 
	end if

else
	repeat with i from 1 to count of theSel
		set item i of theSel to POSIX path of item i of theSel
	end repeat
	
	set archiveName to getEmptyPath(path to desktop as text, "archive", "zip")
	
	if theQ is "" then
		set thePath to ((path to desktop from user domain) as string) & archiveName & ".zip"
	else
		set thePath to ((path to desktop from user domain) as string) & theQ & ".zip"
	end if
	
end if

--set thePassword to the text returned of (display dialog "Enter Password" default answer "")

tell application "Entropy"
	archive thePath files theSel --settings {password:thePassword, encryption method:0}
end tell

---------------------------------------------------------------
-- See if a file with the passed name exists
-- If so, add an integer to the name, and check again
-- Keep checking until an unused name is found
-- Parameters:    path of file's container
--                       file name, no extension
--                       file extension
-- Returns:         updated file name
---------------------------------------------------------------

on getEmptyPath(containerPath, fileName, fileExtension)
	set filePath to containerPath & fileName
	tell application "Finder"
		if exists file (filePath & "." & fileExtension) then
			set x to 1 -- Start Counting
			repeat
				set cFilePath to filePath & " " & x & "." & fileExtension
				if exists file cFilePath then -- Are we There?
					set x to x + 1 -- No, Increment Counter
				else
					exit repeat -- Yes, Leave
				end if
			end repeat
			return fileName & " " & x -- This Name is Safe to Use
		else
			return fileName -- Use Original Name
		end if
	end tell
end getEmptyPath

I think I’ve got it, but if someone see’s something I can “clean up”, please advise.

tell application "Finder" to set theSel to selection as alias list

set theQ to "{query}" -- Alfred Query (Archive Name)


if (count of theSel) is 1 then
	if theQ is "" then
		if (theSel as string) ends with ":" then
			set folderName to the POSIX path of theSel
			set thePath to text 1 thru ((offset of ":" in folderName) - 2) of folderName & ".zip"
			tell application "System Events"
				if (exists file (thePath)) then
					display dialog ("Archive Exists")
					return
				end if
			end tell
		else
			set shortName to the POSIX path of theSel 
			set thePath to text 1 thru ((offset of "." in shortName) - 1) of shortName & ".zip" 
		end if
	else
		set thePath to ((path to desktop from user domain) as string) & theQ & ".zip"
	end if
else
	repeat with i from 1 to count of theSel
		set item i of theSel to POSIX path of item i of theSel
	end repeat
	set archiveName to getEmptyPath(path to desktop as text, "archive", "zip")
	if theQ is "" then
		set thePath to ((path to desktop from user domain) as string) & archiveName & ".zip" 
	else
		set thePath to ((path to desktop from user domain) as string) & theQ & ".zip" 
	end if
end if


-- Set Password for Archive
--set thePassword to the text returned of (display dialog "Enter Password" default answer "")


tell application "Entropy"
		archive thePath files theSel --settings {password:thePassword, encryption method:0}
end tell


---------------------------------------------------------------
-- See if a file with the passed name exists
-- If so, add an integer to the name, and check again
-- Keep checking until an unused name is found
-- Parameters:    path of file's container
--                file name, no extension
--                file extension
-- Returns:       updated file name
---------------------------------------------------------------

on getEmptyPath(containerPath, fileName, fileExtension)
	set filePath to containerPath & fileName
	tell application "Finder"
		if exists file (filePath & "." & fileExtension) then
			set x to 1 -- Start Counting
			repeat
				set cFilePath to filePath & " " & x & "." & fileExtension
				if exists file cFilePath then -- Are we There?
					set x to x + 1 -- No, Increment Counter
				else
					exit repeat -- Yes, Leave
				end if
			end repeat
			return fileName & " " & x -- This Name is Safe to Use
		else
			return fileName -- Use Original Name
		end if
	end tell
end getEmptyPath