'exists folder' gives false positive result

I have a script that moves files in a folder (eg. Media Backup:Projects:testfolder: stored in the variable movielocpath) into a subfolder called “temptrash”. If there is not a subfolder called “temptrash” in that location I have some code that creates the “temptrash” folder. The way I determine if the subfolder needs to be created is the ‘exists folder’ command. However occasionally this gives a false positive, it says the folder exists when it doesn’t. This means the “temptrash” folder is not created and then later a line in the script that moves a file to the “temptrash” folder will throw an error message. I added the display dialog commands to try and debug the script. Just before the error is thrown the dialog is displayed showing the ‘newfolderpath & file exists’ message, when I look for the folder in the movielocpath using the finder it is not there.

The error generally occurs when there was a folder “temptrash” folder that was previously in the movielocpath but that was moved to the trash by another script. The finder seems to keep the information about that trashed folder in memory. It seems the information about the contents of the movielocpath folder was not updated when the folder was moved to trash. Now this is not always the case sometimes the script works fine in the same senario.

I am using OS X 10.5.5. is there an problem with the info about folder content not being updated immediately? Is there a way to make sure it is updated?

I haven’t included the whole script but Just the lines that illustrate where I think the problem lies.


set video_movie to "Media Backup:Projects:testfolder:testfolder.mov"
				 copy AppleScript's text item delimiters to olddelim
				set AppleScript's text item delimiters to ":"
				
				copy (the number of text items of video_movie) - 1 to itemsnumber
				set movielocation to ((text items 1 thru itemsnumber) of video_movie)
				set movielocpath to movielocation as string
				set AppleScript's text item delimiters to olddelim
				copy "temptrash" to newfoldername
				
				copy movielocpath & ":" & newfoldername to newfolderpath

tell application "finder"
				 				

				 if (exists folder newfolderpath) then
					--some times this gives a true result when the folder newfolderpath has been moved
					--to trash by another script that was run before this one.
					display dialog newfolderpath & " folder exists"
				else
					
					display dialog "folder does not exist"
					make new folder at movielocpath with properties {name:newfoldername}
				end if
end tell


Thanks in advance for any suggestions

Martin

Hi,

this behavior could happen, if you’re working with an alias instead of a file specifier.
If an alias has been moved or renamed, the AppleScript reference doesn’t change.

Try this shell version, the -p flag of mkdir includes the folder check


set video_movie to "Media Backup:Projects:testfolder:testfolder.mov"
set {TID, text item delimiters} to {text item delimiters, "/"}
set movielocation to text items 1 thru -2 of (POSIX path of video_movie) as text
set text item delimiters to TID
set newfoldername to "temptrash"
do shell script "/bin/mkdir -p " & quoted form of (movielocation & "/" & newfoldername)

Although what Stefan says about aliases is true, Martin’s script clearly shows ‘newfolderpath’ being set to a path. I can’t duplicate the problem when it’s the Finder that’s deleted the folder. But if it’s been deleted some other way, the Finder continues to think the folder exists. Updating the items of the main folder before testing for the subfolder’s existence takes care of this.

tell application "Finder"
	update items of folder movielocpath
	if exists folder newfolderpath then
		-- ...
	else
		-- ...
	end
end

The ‘mkdir’ shell script is a fast and problem freed way to create the folder ” although an update may still be necessary if the Finder’s then going to be used to move items into it.

Thanks to both Stefan and Nigel

The way the ‘temptrash’ folder is deleted is that it is first renamed by a command in in a handler of an application I have created with the Revolution IDE. Then that renamed folder is moved to the trash using an applescript that is run by the same revolution handler.

tell application "Finder"
	delete foldertodelete -- this is a variable that is set in the handler
end tell

later when another button is pressed the script that I was having difficulty with is run.

I have added the line of script to ‘update items of folder’ to my script that Nigel had suggested

It seems to have resolved the problem.

I am also going to look at the other way of creating a folder using a shell script that Stefan suggested.

Thanks again.

Martin

As suspected, the class of the variable foldertodelete in this syntax is an alias, which is “rename- and move-proof”

What puzzles me is that most of the time it works. Only in a particular situation does it fail. It took me a while to track it down, Usually the first time the delete foldertodelete script is run the folder is successfully deleted(well, it is not visible in finder but it appears to exist to applescript), Then when the script with " if (exists folder newfolderpath)" is run on the same folder the script fails since it incorrectly assumes the folder exists. When I run the same script with " if (exists folder newfolderpath)" again in the script editor(not from within the application) it succeeds and creates a new folder. Then the script with “delete foldertodelete” is run it again works successfully. Then when I again run the script with " if (exists folder newfolderpath)" in the application a second time it works, and it works every time after that.

Still it seems to work now with adding the update command.

I don’t fully understand the difference between alias and other types of variables for files and folders. Is there a class that I should change it to? how would I do that.

Martin

The class alias in AppleScript is like an alias in the Finder GUI.
You can move it or rename it, but it never looses the connection to the original file/folder.

If this behavior is welcome, use an alias.
If not, use a string path with file / folder specifier.