GUI script hanging

Greetings, all! :slight_smile:

The part of my script that is hanging is designed to simply open a page in Safari and copy the contents to the clipboard. The copy functionality was working just fine when that was all there was, but now that I’ve added a loop to open any object dragged onto the script before copying them, it has started hanging inside the convertFrontSafariWindow function on the GUI elements, until I bring any other application to the foreground, at which point it complains that Safari isn’t in front anymore. Any ideas why this might have stopped working?

on open of finderObjects -- "open" handler triggered by drag'n'drop launches 
	repeat with i in (finderObjects) -- in case multiple objects dropped on applet 
		tell application "Safari"
			activate
			make new document at beginning of documents
			open i as alias
		end tell
		convertFrontSafariWindow( )
	end repeat --each finderobject dropped
end open

on convertFrontSafariWindow( )
	--copy all text of Safari front window to clipboard
	tell application "Safari"
		activate
		set windowTitle to name of window 1
	end tell
	tell application "System Events"
		tell process "Safari"
			click menu item "Select All" of menu "Edit" of menu bar 1
			click menu item "Copy" of menu "Edit" of menu bar 1
		end tell
	end tell
	tell application "Safari"
		close window 1
	end tell

	(* do other stuff *)

end convertFrontSafariWindow

Thanks for your help! I’m still using Jaguar, if that matters (I don’t think it should…).

You can replace the System Events stuff with this code:

tell application "Safari"
	text of document 1
end tell

This works fine, unless you need styles (color, bolds, etc.).
If you need work on your own code, you could try adding a “set frontmost to true” just before the “click menu item” statements…

No, my code isn’t sacred - I’d be happy to have a better way. :slight_smile: It’s just the last time I tried using a built-in copy command, I got a single line with all the line-break characters and other such stuff, with no formatting at all - I only need plain text, but I do need the formatting (I paste into BBEdit next).

I’ll try this when I get home tonight - thanks! 8)