"Can't get alias"

I’m making some applications. Before building new apps the old ones should be deleted. For this I’ve written a simple AppleScript.

But I’m having a senior moment. Why do I get the error “Can’t get alias” on the line with “every folder”:

error “Finder got an error: Can’t get alias "Macci:Users:beatrixwillius 1:Documents:Development:Mail Archiver:code current:".” number -1728 from alias “Macci:Users:beatrixwillius 1:Documents:Development:Mail Archiver:code current:”

set DocumentsFolder to path to documents folder as string
set CodeFolder to DocumentsFolder & "Development:Mail Archiver:code current"
set theFolder to (CodeFolder as alias)
--set theFolder to (choose folder with prompt "Select the start folder")
doSomethingWith(theFolder)

on doSomethingWith(aFolder)
	tell application "Finder"
		set subFolders to every folder of aFolder
		repeat with eachFolder in subFolders
			my doSomethingWith(eachFolder)
		end repeat
	end tell
	
	try
		tell application "Finder"
			delete (every item of aFolder whose name ends with "app" and name starts with "mail archiver")
			delete (every item of aFolder whose name ends with "dmg")
			if name of aFolder starts with "OS X 64 bit" or name of aFolder starts with "Mac OS X (Cocoa Intel)" or name of aFolder starts with "final" or name of aFolder starts with "macOS Universal" or name of aFolder starts with "macOS ARM 64 bit" then
				delete (files of aFolder)
				delete aFolder
			end if
		end tell
	on error error_message number error_number
		display alert ("YIKES! Something's wrong!") message error_message & (" Error number ") & error_number & "."
	end try
end doSomethingWith

The final script needs to run on a fixed folder. But even when I select the folder instead I still get the error.

It seems aliases are broken under APFS.

I suggest passing HFS path strings, not aliases (nor anything else, for that matter).
The 1st part of your script would be:

set DocumentsFolder to path to documents folder as string
set CodeFolder to DocumentsFolder & "Development:Mail Archiver:code current:"-- ADDED colon at end!!
-- DISABLED set theFolder to (CodeFolder as alias)
--set theFolder to (choose folder with prompt "Select the start folder")

doSomethingWith(CodeFolder)-- EDITED

on doSomethingWith(aFolder)
   tell application "Finder"
       set subFolders to every folder of folder aFolder-- EDITED 

The code as posted runs fine on my machine (macOS 12.2.1, 64bit Intel, APFS).

I created an identical folder structure, and then created subfolders and additional subfolders within the subfolders.

The following script is, as far as I can see, identical to @bwill’s in terms of syntax (some variable names are different), except that I’ve added a display dialog command so that dosomethingwith actually does do something with:

set docsFolder to path to documents folder as string
set codeFolder to docsFolder & "Development:Mail Archiver:code current"
set theFolder to (codeFolder as alias)
dosomethingwith(theFolder)
on dosomethingwith(aFolder)
	tell application "Finder"
		set subFolders to every folder of aFolder
		repeat with eachfolder in subFolders
			my dosomethingwith(eachfolder)
			display dialog (name of eachfolder as text)
		end repeat
	end tell
end dosomethingwith

This displays a series of dialogs with the names of all the subfolders in the ‘code current’ folder, and all the lower-level subfolders. No errors. I retyped it rather than copy-pasted it, but I’m pretty sure it follows the original post.

I can’t test on other OS’s, but the problem might be OS-related. My first guess would be to try adding a colon to the end of the codefolder definition:

set codeFolder to docsFolder & "Development:Mail Archiver:code current:"

Hope it helps, not sure it will…

I tested the OP’s script from within Script Editor and Script Debugger, and it works fine for me on Monterey. The error message is an odd one–I wonder what circumstance would cause that.

Error number -1728 is defined as:

One obvious thing to try — although I’ve no idea if it will solve the problem on whatever system bwill’s using — would be to use a Finder reference instead of an alias:

set DocumentsFolder to path to documents folder as text
set CodeFolder to DocumentsFolder & "Development:Mail Archiver:code current"
tell application "Finder" to set theFolder to folder CodeFolder
doSomethingWith(theFolder)

It’s possible to use more efficient logic even with the Finder. At the moment, it’s:

  1. Search out and delete every/any item in the folder whose name satisfies the first two tests.
  2. Search out and delete every/any item in the folder whose name satisfies a third test.
    THEN
  3. Fetch and test the folder’s name up to five times.
  4. If any of the tests scores a hit, delete every item in the folder and then delete the folder itself.

A more efficient approach would be:

  1. Fetch the folder’s name just once.
  2. Test the result up to five times.
    3a. If any of the tests scores a hit, delete the folder with its items still in it.
    3b. Otherwise delete those items from it whose names satisfy the conditions the original code tested first.
on doSomethingWith(aFolder)
	tell application "Finder"
		set subFolders to every folder of aFolder
		repeat with eachFolder in subFolders
			my doSomethingWith(eachFolder)
		end repeat
	end tell
	
	try
		tell application "Finder"
			set folderName to name of aFolder
			if (folderName starts with "OS X 64 bit") or (folderName starts with "Mac OS X (Cocoa Intel)") or (folderName starts with "final") or (folderName starts with "macOS Universal") or (folderName starts with "macOS ARM 64 bit") then
				delete aFolder
			else
				delete (every item of aFolder whose (name ends with "app" and name starts with "mail archiver") or (name ends with "dmg"))
				(* -- Alternatively:
				set itemNames to name of aFolder's items
				repeat with thisName in itemNames
					if ((thisName ends with "app" and thisName starts with "mail archiver") or (thisName ends with "dmg")) then
						delete item thisName of aFolder
					end if
				end repeat *)
			end if
		end tell
	on error error_message number error_number
		display alert ("YIKES! Something's wrong!") message error_message & (" Error number ") & error_number & "."
	end try
end doSomethingWith

I was interested by the approach taken in the OP’s script to iterate through folders, which Nigel incorporated in his script. So, I ran timing tests (2 with each script) and the results were:

bwill’s script - 2.048 seconds 1.751 seconds

Nigel’s script - 1.026 seconds 970 milliseconds

peavine’s script - 584 milliseconds 544 milliseconds

To run these tests, I created a heavily-threaded folder and inserted numerous TXT files in these folders. I then edited the above scripts to delete TXT rather than DMG files (i.e. name ends with “txt”). I mention this because a better test would have been to create a folder structure with more of the folders/files identified in the scripts for deletion.

My script:

set DocumentsFolder to path to documents folder as string
set CodeFolder to DocumentsFolder & "Development:Mail Archiver:code current"
set theFolder to (CodeFolder as alias)
doSomethingWith(theFolder)

on doSomethingWith(targetFolder)
	tell application "Finder"
		set theFolders to every folder of the entire contents of targetFolder as alias list
		set end of theFolders to targetFolder
		
		repeat with aFolder in theFolders -- mostly from Nigel
			try
				if aFolder exists then
					set folderName to name of aFolder
					if (folderName starts with "OS X 64 bit") or (folderName starts with "Mac OS X (Cocoa Intel)") or (folderName starts with "final") or (folderName starts with "macOS Universal") or (folderName starts with "macOS ARM 64 bit") then
						delete aFolder
					else
						delete (every item of aFolder whose (name ends with "app" and name starts with "mail archiver") or (name ends with "txt"))
					end if
				end if
			on error error_message number error_number
				display alert ("YIKES! Something's wrong!") message error_message & (" Error number ") & error_number & "."
			end try
		end repeat
	end tell
end doSomethingWith
 

Thanks for your help.

The speed of the script doesn’t really matter because

a) there aren’t many files in the directories
b) the script is part of an application build process that takes now 15 minutes with making Intel and M1 builds and notarisation.

The script worked fine until a couple of weeks ago on High Sierra. The script also works on my Air on Monterey. I must restart the computer to see if it acts up in some way.