Leopard Applescript problem?

Thanks in advance. I’d be grateful for any advice on this.

I’ve been having a problem with a script that I wrote for teachers at my school. It worked fine under Tiger, but it doesn’t work properly under Leopard.

Among other things, the script duplicates files into folders on a network volume. The network volume contains about 1,000 folders, which have permissions set (not my idea to put all 1,000 folders in the same place, btw). Each student has read/write permission for only one folder, while all teachers have write permission for all of the folders.

In Tiger (10.4.9) the script runs and Finder duplicates the files in a matter of seconds. In Leopard (10.5.1), the script runs but Finder goes unresponsive for about 5 full minutes before it accomplishes the duplication (meanwhile the script has long since timed out.)

The following simple script produces the same behavior:

tell application "finder"
 open disk "student" -->disk "student" contains about 1,000 folders
end tell

Under Tiger (10.4.9), The script runs, Finder pops up a window within seconds, and the script quits. But then Finder takes a minute or two to load up the 1,000 folders in that window. During that time, Finder remains responsive.

Under Leopard (10.5.1), Finder goes unresponsive for 5 full minutes before it pops up the window, meanwhile the script throws the timeout error message. When the Finder window does finally pop up, though, it is displaying all of the folders already.

Has anybody else experienced this behavior? Is this an Applescript issue? A Finder issue? A network permissions issue? Should I tell Apple about it?

I’d be grateful for any advice.

Model: macbook
AppleScript: 1.10.7
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Leopard’s applescript does have some odd issues, I’ll grant that.

may I ask why you’re using the ‘open’ command with a disk? that doesn’t make sense to me. if the disk is already mounted, I’m not sure what ‘opening’ it would mean, and the Finder might not be sure about it either. plus, I don’t think that’s the command you’d use to mount an offline disk (at least that’s not the way I’d do it…)

try taking the open command out entirely, and going straight to the duplicate command, see if that fixes the problem.

Thanks for the quick reply.

The script that I wrote is much longer and does many other things. Since the problem can be reproduced with the shorter script I posted, I thought the problem might be easier to analyze that way.

Sorry; I should have been more clear.

Hi,

It is not necessary at all to open a Finder window to copy files.
Quite the contrary it wastes time either to open Finder windows or to bring the Finder frontmost with the activate command.

The fastest way to copy files even to a network volume is to use the shell

What is “use the shell”?

In the script, I don’t ever open a window. It’s just that that command reproduced the same behavior.

I guess, to avoid confusion, I should reproduce the whole script.

on open the_item_list
	
	tell application "Finder"
		
		--MAKE SURE THAT USER IS CONNECTED TO STUDENT SHARED VOLUME
		
		try
			mount volume "smb://fs2/student"
		end try
		
		
		--GET EACH OF THE FILES THAT WERE DRAGGED TO THE SCRIPT FOR COPYING BACK TO STUDENT'S H-DRIVE
		repeat with i from 1 to the number of items in the_item_list
			
			--PARSE USERNAME OUT OF NAME OF ITEM: IT'S BETWEEN THE FIRST AND SECOND HYPHENS
			try
				
				set the_item to item i of the_item_list as alias
				
				set full_name_of_item to the name of the_item as text
				
				set hyphen_location to the offset of "-" in full_name_of_item
				
				set username_originalname to text (hyphen_location + 1) thru -1 in full_name_of_item
				
				set hyphen_location to the offset of "-" in username_originalname
				
				set user_name to text 1 thru (hyphen_location - 1) of username_originalname
				
				set originalname to text (hyphen_location + 1) thru -1 in username_originalname
				
			on error
				display dialog "I'm sorry, but I couldn't get the name of the item you gave me."
			end try

			try
					--CHANGE THE COLOR OF THE ITEM TO INDICATE THAT IT'S BEEN RETURNED
					set old_color to the label index of the_item -->PRESERVE THE ORIGINAL COLOR OF FILE
					
					set label index of the_item to 1
					set the name of the_item to ("g_" & originalname)
					
					if folder user_name of disk "student" is not full then
						
						duplicate file the_item to folder user_name of disk "STUDENT" with replacing
						
						set the label index of the_item to old_color
						set the name of the_item to ("g_" & full_name_of_item)
					else
						display dialog "I'm sorry, but I was unable to copy the folder to the student's H-drive"
						
						--CHANGE THE COLOR BACK  AND THE NAME BACK BECAUSE IT WAS NOT HANDED BACK
						set label index of the_item to old_color
						set the name of the_item to full_name_of_item
					end if
				on error
					display dialog "I'm sorry, but I was unable to copy the file to the student's H-drive"
					
					--CHANGE THE COLOR BACK  AND THE NAME BACK BECAUSE IT WAS NOT HANDED BACK
					set label index of the_item to old_color
					set the name of the_item to full_name_of_item
				end try
			end if
			
		end repeat
	end tell
	
	
end open


My apologies to everyone who posted so far. I should have just posted the original script from the start.

I thought it would be easier to figure out what’s going on by giving you a shorter, simpler script which reproduced the same behavior. There is no open command in the original; it merely reproduced the same behavior in Leopard.

Again, I apologize. I appreciate your time and didn’t mean to waste it.

I changed the script a bit

¢ the Finder duplicate line has been replaced with the shell equivalent.
¢ I couldn’t find a property full of folder, so I commented out the part.
¢ The existence of the disk will be checked before mounting it. If the disk cannot be mounted, the script will be aborted
¢ A much easier way to strip the user name

property diskname : "student"
-- property user_name : "xyz" -- only for test

on open the_item_list
	--MAKE SURE THAT USER IS CONNECTED TO STUDENT SHARED VOLUME
	if diskname is not in (do shell script "ls /Volumes") then
		try
			mount volume "smb://fs2/" & diskname
		on error
			display dialog "volume " & quote & diskname & quote & " couldn't be mounted" buttons {"Cancel"} default button 1
			return
		end try
	end if
	
	--GET EACH OF THE FILES THAT WERE DRAGGED TO THE SCRIPT FOR COPYING BACK TO STUDENT'S H-DRIVE
	repeat with the_item in the_item_list
		
		--PARSE USERNAME OUT OF NAME OF ITEM: IT'S BETWEEN THE FIRST AND SECOND HYPHENS
		try
			set full_name_of_item to name of (info for the_item)
			set {TID, text item delimiters} to {text item delimiters, "-"}
			set originalname to text item 2 of full_name_of_item
			set text item delimiters to TID
		on error
			display dialog "I'm sorry, but I couldn't get the name of the item you gave me."
		end try
		tell application "Finder"
			try
				--CHANGE THE COLOR OF THE ITEM TO INDICATE THAT IT'S BEEN RETURNED
				
				set old_color to the label index of the_item -->PRESERVE THE ORIGINAL COLOR OF FILE
				set label index of the_item to 1
				set the name of the_item to ("g_" & originalname)
				
				-- 	if folder user_name of disk diskname is not full then
				
				-- duplicate file the_item to folder user_name of disk "STUDENT" with replacing
				do shell script "cp " & quoted form of POSIX path of contents of the_item & space & quoted form of ("/Volumes/" & diskname & "/" & user_name)
				set the label index of the_item to old_color
				set the name of the_item to ("g_" & full_name_of_item)
				(*
					else
						display dialog "I'm sorry, but I was unable to copy the folder to the student's H-drive"
						
						--CHANGE THE COLOR BACK  AND THE NAME BACK BECAUSE IT WAS NOT HANDED BACK
						set label index of the_item to old_color
						set the name of the_item to full_name_of_item
					end if
				*)
			on error
				display dialog "I'm sorry, but I was unable to copy the file to the student's H-drive"
				
				--CHANGE THE COLOR BACK  AND THE NAME BACK BECAUSE IT WAS NOT HANDED BACK
				set label index of the_item to old_color
				set the name of the_item to full_name_of_item
			end try
			-- end if
		end tell
	end repeat
end open

Thanks, Stefan. I’ll try it out on Monday.

At this point, do you think this is a Leopard issue or was it my script? It seemed to work fine in Tiger, although I guess that doesn’t necessarily mean anything. Being fairly new at Applescript, I’m sure my scripts could be cleaner.

I’ll post again Monday to let you know what happened.

It might be, that the Finder behaves different in Tiger and Leopard.
Anyway I always prefer the shell to copy files to a network volume

It worked beatifully! I can see I’m going to have to brush up on shell scripts.

I did have to redefine the user_name variable - it got lost in the edit.

I am very grateful to you, Stefan, and to everyone who posted.

I’ve run into a snag. The script copies files like a champ, but won’t copy package folders (like Omnigraffle files). Is there a way to identify and copy package folders properly?

Of course, sorry, I forgot that


.
do shell script "cp -pR " & quoted form of POSIX path of contents of the_item & space & quoted form of ("/Volumes/" & diskname & "/" & user_name)
.

Note: the R flag copies the (entire) contents of folders and package folders,
the p flag preserves in the copy attributes like modification time, access time, file flags, file mode,
user ID, and group ID, as allowed by permissions. Access Control Lists (ACLs) will also be preserved