Object Not Found error in simple Finder script

I am going through"Applescript: A Comprehensive Guide to Scripting…" and a simple script is giving me an error I cannot figure out why.

Untitled 2.applescript (512 Bytes)

It looks like the alias is not updating! The delete command is showing as delete alias “/Users/…/untitled folder” Why is the alias not now “Users/…/Folder number 48”?

set my_random to random number 100
set my_random to my_random as text
set folder_name to "Folder number " & my_random
tell application "Finder" to make new folder at desktop
set my_folder to the result as alias
tell application "Finder" to set the name of my_folder to folder_name
display dialog ("Delete new folder " & folder_name & "?") buttons {"Yes", "No"}
set dialog_command_record to the result
if button returned of dialog_command_record is "Yes" then
	-- This line returns an error
	tell application "Finder" to delete my_folder
end if

I am using Script Editor 2.1, Applescript 2.7 in High Sierra 10.13.6

It works for me (running Sierra). If you have only a line with my_folder inside your if…then statement, you will see that ‘my_folder’ is an alias for your new folder.

Dunno if there is a permissions issue. Also, your description of references that begin with /Users/ doesn’t make sense as that is a piece of a posix path rather than an alias, which should look like alias Drive:Users:…. I don’t see anything in your code here that would generate a posix path.

As an aside, I’m not sure how you’re entering your post but you might try this to format your lines of code. Put a line with only three backticks ‘```’ (the key to the left of the ‘1’) above your code and another below your code and then it should come out like this…

set my_random to random number 100

Alternatively, if you have just a line or two of code, you can put 4 spaces in front of it and it will also appear as the line above. Or, after you paste it in the editor’s text area, select the code and click the Preformatted text button (</>).

There is a FAQ which leads to a discourse user guide (click the menu button next to your user icon in the upper right corner to find it) but in general, the site uses a variant of markdown to format text.

1 Like

You’re correct, it shows with colons in the result pane if I delete the script after set the name line. However, with the full script, in the Log History, when I check the result of the second Tell Finder line, I see “Macintosh HD:Users:withanlemmon:Desktop:untitled folder:” but with slashed. Now when I copied and pasted into this reply, the colons appeared.

The big (huge) issue is that it is “untitled folder” !

I fixed the code in the post. Thanks!

Strange. I get this as the result:

folder "Folder number 96" of item ".Trash" of folder "username" of folder "Users" of startup disk of application "Finder"

In the replies, this is how the delete command (that generates the result above) looks. This is normal and the result is the same as the returned value… it’s what the alias is referencing.

	delete alias "Drive:Users:username:.Trash:Folder number 96:"
	--> folder "Folder number 96" of item ".Trash" of folder "username" of folder "Users" of startup disk

Does the dialog display the correct folder name? It does for me.

Yes it does, but the dialog is fed the name by the variable folder_name, not the variable my_folder. The first is a string, but the second an alias. A stuck alias in High Sierra it seems.

Well, you can replace folder_name with my_folder in the dialogue. The dialogue will coerce the alias to text in order to display it but you should get the new name.

Actually, this is not a very good script that creates an untitled folder, then renames it, there is confusion with aliases, Finder references and their textual representations. In addition, an error is thrown when a folder with a certain random number already exists.

It’s easier to immediately create a folder with a target name and work only with its Finder reference. Add a try block and the repetition error will disappear too:

set folder_name to "Folder number " & (random number 100)

try
	tell application "Finder" to set my_folder to make new folder at desktop with properties {name:folder_name}
	display dialog ("Delete new folder \"" & folder_name & "\" ?") buttons {"Yes", "No"}
	if button returned of result is "Yes" then tell application "Finder" to delete my_folder
end try

This did work well.

I’m trying to work through a(some) book(s) on AppleScript and become somewhat proficient. Either the book I have (ISBN 1-59059-404-5, AppleScript a Comprehensive Guide to Scripting and Automation on Mac OS X) is terribly out-of-date, or poorly written, or both. It’s got a 2004 © date.

I’m also working with “Basics of AppleScript,” published in 2014, but it’s got bad grammar and I’ve found obvious errors in scripts, which luckily I was able to correct from context.

A later edition of that book is well-regarded. The 3rd edition, which was co-authored with Hamish Sanderson, was published in 2010 by a different (and presumably larger) publisher. Of note, Applescript 2 was released in 2007 with Leopard so the 1st edition was written prior to a major overhaul of the technology. I’m sure that many developments would have made their way into the later edition.