Can't save files correctly when calling script from script

G’day

I’m trying to call a very lengthy script I have from this script (saved as an App.). However, for some reason the files I’m trying to save in the long script are causing the finder to trip my try - on error - end try statements embedded in that script. The files get copied/shifted, but trigger an error on completion of the copy/shift.

I’ve tried the calling script without the file renaming lines, but the problem still occurrs.

If I don’t call the long script from this script, but instead give the finder time to idle, then manually use the long script, the long script works fine.

Any hints as to what’s going on, and how to fix it, please.

EDIT the error code is 8726 or 8704

Regards

Santa


on run
	tell application "Finder"
		set tempSwitch to my ReadFile2(".SwitchMailManagerFlag")
		if tempSwitch = "OFF" then
			say "Mail Manager ON"
			my WriteFile3(".SwitchMailManagerFlag", "ON")
			set TheDesktopDisk to (path to startup disk) as text
			set temp to the path to me as text
			try
				set the name of file temp to "Switch OFF Mail Manager"
			end try
			run script alias (TheDesktopDisk & "Library:Scripts:Folder Action Scripts:Mail Item Mover.scpt")
		else
			my WriteFile3(".SwitchMailManagerFlag", "OFF")
			say "Mail Manager OFF"
			set temp to the path to me as text
			try
				set the name of file temp to "Switch ON Mail Manager"
			end try
		end if
	end tell
end run

on WriteFile3(TheFileName, TheWriteItem)
	set PathToDesktop to path to desktop as Unicode text
	set TheFileName to (PathToDesktop) & TheFileName as text
	set fRef to (open for access file TheFileName with write permission)
	set eof fRef to 0
	try
		write TheWriteItem to fRef starting at eof
	end try
	close access fRef
end WriteFile3

on ReadFile2(TheFileName)
	set PathToDesktop to path to desktop as Unicode text
	set TheFileName to (PathToDesktop) & TheFileName as text
	tell application "Finder"
		try
			set WholeList to read file (TheFileName)
			return WholeList
		on error
			return {}
		end try
	end tell
end ReadFile2


Model: 24" al. imac
AppleScript: latest for 10.5
Browser: Safari 525.20.1
Operating System: Mac OS X (10.5)

G’day

I’ve written a small version of the script to show what happens, and it crashes with an ‘Apple event timed out’ after shifting the new files.

There are two scripts, a revised version of the script above, which must be saved as an Application, and the second, which must be save as a script called ‘Mail Item Mover’, both on the desktop. Run the App.

If anyone would be kind enough to download and set these up and run them, it would be much appreciated.

Regards

Santa


on run
	set PathToDesktop to path to desktop as Unicode text
	tell application "Finder"
		set tempSwitch to my ReadFile2("  SwitchMailManagerFlag")
		if tempSwitch = "OFF" then
			say "Mail Manager ON"
			my WriteFile3("  SwitchMailManagerFlag", "ON")
			set temp to the path to me as text
			try
				set the name of file temp to "Switch OFF Mail Manager"
			end try
			run script alias (PathToDesktop & "Mail Item Mover.scpt")
			--set TheDesktopDisk to (path to startup disk) as text
			--run script alias (TheDesktopDisk & "Library:Scripts:Folder Action Scripts:Mail Item Mover.scpt")
		else
			my WriteFile3("  SwitchMailManagerFlag", "OFF")
			say "Mail Manager OFF"
			set temp to the path to me as text
			try
				set the name of file temp to "Switch ON Mail Manager"
			end try
		end if
	end tell
end run

on WriteFile3(TheFileName, TheWriteItem)
	set PathToDesktop to path to desktop as Unicode text
	set TheFileName to (PathToDesktop) & TheFileName as text
	--set TheFileName to (PathToDesktop) & "Mail Printing Folder:" & TheFileName as text
	set fRef to (open for access file TheFileName with write permission)
	set eof fRef to 0
	try
		write TheWriteItem to fRef starting at eof
	end try
	close access fRef
end WriteFile3

on ReadFile2(TheFileName)
	set PathToDesktop to path to desktop as Unicode text
	set TheFileName to (PathToDesktop) & TheFileName as text
	--set TheFileName to (PathToDesktop) & "Mail Printing Folder:" & TheFileName as text
	tell application "Finder"
		try
			set WholeList to read file (TheFileName)
			return WholeList
		on error
			return {}
		end try
	end tell
end ReadFile2

Save this as script called ‘Mail Item Mover’


on run
	set PathToDesktop to path to desktop as Unicode text
	tell application "TextEdit"
		activate
		try
			repeat while exists window 1
				close window 1 saving no
			end repeat
		end try
		tell application "System Events" to tell process "TextEdit"
			click menu item "New" of menu 1 of menu bar item "File" of menu bar 1
			
			keystroke "s" using {command down, shift down}
			tell window 1
				set x to 0
				repeat until sheet 1 exists
					delay 0.1
					set x to x + 1
					if x = 150 then exit repeat
				end repeat
				if exists checkbox 2 of sheet 1 then
					if (value of checkbox 2 of sheet 1) = 1 then click checkbox 2 of sheet 1
				end if
				click pop up button 1 of sheet 1
				click menu item "Desktop" of menu 1 of pop up button 1 of sheet 1
				
				click button "Save" of sheet 1
				set temp to name
				tell application "Finder"
					if not (exists folder "This is a test") then make new folder with properties {name:"This is a test"}
					set x to 0
					set temp2 to name of file (PathToDesktop & temp)
					repeat
						if not (exists file temp in folder "This is a test") then exit repeat
						set x to x + 1
						set temp to x & temp2 as text
					end repeat
					set the name of file (PathToDesktop & temp2) to temp
					try
						move file (PathToDesktop & temp) to folder "This is a test"
					on error ErrName number errNum
						display dialog ErrName & return & errNum
					end try
				end tell
				
			end tell
		end tell
		try
			repeat while exists window 1
				close window 1 saving no
			end repeat
		end try
	end tell
end run

Nevermind folks, I found the answer. Turns out it’s (sometimes) a bug to run a script without loading it first.

I altered the following lines, and all appears well.



set TheDesktopDisk to (path to startup disk) as text
set ScriptSource to (load script alias (TheDesktopDisk & "Library:Scripts:Folder Action Scripts:Mail Item Mover.scpt"))
tell ScriptSource to run