Moving a file occasionally results in an error

Bear with me… I’m not very fluent in AppleScript

I wrote a script that duplicates a file (from a folder named “TrackballWorks plists” in the “Documents Folder”) to the Desktop, changes the name of the duplicated file, and then moves it to the user “Preferences Folder”. The majority of the time this script works perfectly, but on occasion, it fails with a “Connection is invalid” error. Can anyone tell me what might be causing the error?

set working_file to "Default.plist"
set documents_folder to path to documents folder
set nonuniversal_path to "TrackballWorks plists:" & working_file as text
set new_trackball_setting to (documents_folder & nonuniversal_path) as text
set preferences_folder to path to preferences folder
set trackball_plist_name to "com.kensington.trackballworks.settings.plist"

tell application "Finder"
	--Replace current Trackball plist with the Default pilst
	duplicate new_trackball_setting to desktop
	set name of file working_file of desktop to trackball_plist_name
	move file trackball_plist_name of desktop to preferences_folder with replacing
end tell

tell application "System Preferences"
	--Launch TrackballWorks preference pane to activate the new setting
	set current pane to pane "com.kensington.trackballworks.prefpane"
	-- Make function keys special function keys
	set current pane to pane "com.apple.preference.keyboard"
	
	tell application "System Events"
		tell application process "System Preferences"
			click radio button "Keyboard" of tab group 1 of window "Keyboard"
			if value of checkbox "Use all F1, F2, etc. keys as standard function keys" of tab group 1 of window "Keyboard" = 1 then
				click checkbox "Use all F1, F2, etc. keys as standard function keys" of tab group 1 of window "Keyboard"
			end if
		end tell
		
	end tell
	quit application "System Preferences"
end tell

Here’s the text from the Events pane of Applescript Editor when the error rears its head

tell current application
	path to documents folder
		--> alias "Macintosh HD:Users:~:Documents:"
	path to preferences
		--> alias "Macintosh HD:Users:~:Library:Preferences:"
end tell
tell application "AppleScript Editor"
	copy "Macintosh HD:Users:~:Documents:TrackballWorks plists:Default.plist" to «class desk»
		--> document file "Default.plist" of folder "Desktop" of folder "~" of folder "Users" of startup disk
		--> error number 0
	set name of file "Default.plist" of «class desk» to "com.kensington.trackballworks.settings.plist"
		--> "com.kensington.trackballworks.settings.plist"
	move file "com.kensington.trackballworks.settings.plist" of «class desk» to alias "Macintosh HD:Users:~:Library:Preferences:" with «class alrp»
Result:
error "Finder got an error: Connection is invalid." number -609

If I read well,the instruction :
duplicate new_trackball_setting to desktop
try to duplicate a string to the Desktop.
Not sure that it’s a good idea.

Yvan KOENIG (VALLAURIS, France) samedi 28 juin 2014 09:53:38

Alright… But when I’ve seen the script fail (like in the example above), it does so at the move command, not the duplicate command. It actually duplicates the file, changes the name, and then hiccups on moving the file to the preferences folder (leaving the newly created and renamed file, “com.kensington.trackballworks.settings.plist” on the Desktop).

Hi.

The “Connection is invalid” error is usually thrown if an application disappears (ie. quits) while it’s being spoken to. But your Events pane text is interesting for application “Finder” having been replaced with application “AppleScript Editor”, with the consequent failure of the Finder class codes to decompile to ‘desktop’ and ‘replacing’. And yet it’s the Finder reporting the error. :confused:

I’ve not been able to reproduce this. So far, the duplication’s always successful and application “Finder” and the Finder keywords always appear in the Events pane. Is there any more to your script than you’ve posted? Are you running it from AppleScript Editor or in some other way? Is TrackballWorks running while you do this?

Hi,

try either the “alias”-version which uses an alias specifier instead of a Finder specifier

tell application "Finder"
	--Replace current Trackball plist with the Default pilst
	set trackball_plist to (duplicate file new_trackball_setting to desktop) as alias
	set name of trackball_plist to trackball_plist_name
	move trackball_plist to preferences_folder with replacing
end tell

or the shell version which is able to copy and rename at the same time
In this case replace the whole Finder tell block with


do shell script "/bin/cp " & quoted form of POSIX path of new_trackball_setting & space & quoted form of (POSIX path of preferences_folder & trackball_plist_name)

No, that’s the script in its entirety.
Again, I must say that the majority of the time, this script does work, but I occasionally get the error from running it within "AppleScript Editor"and when it’s saved as an app. I haven’t used it in any other way.
There’s a separate background process running called “TrackballWorksHelper”, but I can’t see how that would be affected by anything in this script.

Basically what I’m doing here is overwriting a preference file with one that contains different settings. I’m then launching the preference pane in “System Preferences” so that the changes take affect. If I overwrite the preference file without launching the preference pane (or restarting the computer), those changes won’t take place.

I’ll give the “alias”-version a shot and post back later.
I have a hard enough time speaking English and Applescript (and I’m not very good at the latter of the two). I’m not about to delve into something I’m not familiar with. :smiley:

Well, replacing the Finder tell block with…

tell application "Finder"
	--Replace current Trackball plist with the Default pilst
	set trackball_plist to (duplicate file new_trackball_setting to desktop) as alias
	set name of trackball_plist to trackball_plist_name
	move trackball_plist to preferences_folder with replacing
end tell

…doesn’t seems to make it any more stable than before. It’s still failing about 5-10% of the time when run within “AppleScript Editor”.

Here’s the result details for one of the failures…

tell current application
	path to documents folder
		--> alias "Macintosh HD:Users:~:Documents:"
	path to preferences
		--> alias "Macintosh HD:Users:~:Library:Preferences:"
end tell
tell application "Finder"
	copy file "Macintosh HD:Users:~:Documents:TrackballWorks plists:Default.plist" to desktop
		--> document file "Default.plist" of folder "Desktop" of folder "~" of folder "Users" of startup disk
		--> error number 0
	get document file "Default.plist" of folder "Desktop" of folder "~" of folder "Users" of startup disk
		--> alias "Macintosh HD:Users:~:Desktop:com.kensington.trackballworks.settings.plist"
	set name of alias "Macintosh HD:Users:~:Desktop:Default.plist" to "com.kensington.trackballworks.settings.plist"
		--> "com.kensington.trackballworks.settings.plist"
	move alias "Macintosh HD:Users:~:Desktop:com.kensington.trackballworks.settings.plist" to alias "Macintosh HD:Users:~:Library:Preferences:" with replacing
Result:
error "Finder got an error: Connection is invalid." number -609

Is there a chance that the name change is not taking place fast enough and when it tries the move command there is no file named, “com.kensington.trackballworks.settings.plist” on the Desktop?

that’s why I prefer the alias version. AppleScript identifies an alias specifier by a file reference number regardless of the name

What happens if you simply overwrite the contents of the file in the Preferences folder with the data from the Default?

set working_file to "Default.plist"
set documents_folder to (path to documents folder as text)
set nonuniversal_path to "TrackballWorks plists:" & working_file
set new_trackball_setting to (documents_folder & nonuniversal_path)
set preferences_folder to (path to preferences folder as text)
set trackball_plist_name to "com.kensington.trackballworks.settings.plist"

set plist_data to (read file new_trackball_setting as data)
set fRef to (open for access file (preferences_folder & trackball_plist_name) with write permission) -- Also creates the file if it doesn't exist.
try
	set eof fRef to 0
	write plist_data to fRef
end try
close access fRef

tell application "System Preferences"
	--Launch TrackballWorks preference pane to activate the new setting
	set current pane to pane "com.kensington.trackballworks.prefpane"
	-- Make function keys special function keys
	set current pane to pane "com.apple.preference.keyboard"
	
	tell application "System Events"
		tell application process "System Preferences"
			click radio button "Keyboard" of tab group 1 of window "Keyboard"
			if value of checkbox "Use all F1, F2, etc. keys as standard function keys" of tab group 1 of window "Keyboard" = 1 then
				click checkbox "Use all F1, F2, etc. keys as standard function keys" of tab group 1 of window "Keyboard"
			end if
		end tell
		
	end tell
	quit application "System Preferences"
end tell

If it’s reading or writing the same property list file, it might well be the key to the problem. The errors could be the result of the script trying to swap the file while TrackballWorksHelper is dealing with it.

It should be easy enough to test; try running the script without TrackballWorksHelper running.

I’ve run your script at least 30 times, from “AppleScript Editor”, and it hasn’t failed once. This seems like the best solution… Thank you very much, Nigel. :cool:

Good point. But, it seems that the best option is to overwrite the contents of the file, as Nigel suggested, since I can’t get that script to fail.

I want to thank everyone who replied to this thread. My knowledge of AppleScript is pretty limited and when I run into issues with my scripts this board has been a invaluable resource for problem solving.
Thanks again! :cool: