script launches app and then spits error

This is a strange one: If the app FootTrack is not open the script works fine. If the app is open the script returns an error “Connection is invalid” and highlights the last “activate” in the script

tell application "System Events"
	if (name of every application process) contains "FootTrack" then
		display dialog "FootTrack needs to quit to switch libraries" buttons {"Cancel", "OK"} default button 2
		tell application "FootTrack"
		end tell
		-- FootTrack is not running
	end if
end tell

	set new_path to "/Users/tim/Movies/FootTrack/Family"
	set this_data to do shell script "defaults write com.foottrack.FootTrack CatalogLocation  " & " " & new_path
on error
	display dialog "Could not change libraries." buttons {"OK"} default button 1
end try

tell application "FootTrack"
end tell

Am I stuck in a loop I can’t see? Any ideas are most appreciated!

What happens if you just run this in another script when FootTrack is running (just trying to isolate the problem)?

tell application “FootTrack” to activate

I haven’t tried that. What good that would do? ('m pretty new at this :slight_smile:

As I said when making the suggestion: “(just trying to isolate the problem)”

I added the following (from another script) to the end of the script and it still gives an error.

tell application "Finder"
	if the (count of windows) is not 0 then
		set collapsed of every window to false
	end if
end tell

:shock: I don’t know what that has to do with the original script and I don’t have any more suggestions to ignore. Hopefully someone else can help you with your problem. :slight_smile:

My guess is that you’re not giving enough time for the app to quit before trying to re-activate it.

In one statement you tell the app to quit. The process of doing this establishes a connection between AppleScript and the app.

You then shuffle the files around and rewrite the preferences before the app has finished quitting. When you try and re-activate the app, AppleScript is still trying to target the quitting app.

Try adding a delay to your quit routine - either a simple delay x statement or, better still, a check for its actual status, like:

tell application "FootTrack"
end tell
repeat until (name of every process) does not contain "FootTrack"
  delay 1
end repeat

In this case the code will loop until the app really has quit, then you’re safe to continue.

That’s pretty much what I meant by “loop”, except that I couldn’t figure out how to stop it.

Your example did the trick. Thank you! :slight_smile: