CLick button SAVE no longer works?

I have this script


tell application "TextWrangler"
	activate
	select insertion point before line 2 of document 1
	select line 2 of project window 1
	copy selection
activate application "TextWrangler"
tell application "System Events"
	keystroke "s" using {shift down, command down}
	delay 1
	end tell
	tell application "Finder"
                click button "Save"
	end tell	
	activate application "TextWrangler"
keystroke "w" using command down
	delay 1
	end tell


end tell


How can I paste line 2 as a SAVE AS name for the open Window?

The button Save behaves flawlessly when it it is triggered correctly :wink:


tell application "TextWrangler"
	activate
	select insertion point before line 2 of document 1
	select line 2 of project window 1
	copy selection
end tell
tell application "System Events" to tell process "TextWrangler"
	set frontmost to true
	tell window 1
		class of UI elements --> {static text, grow area, button, button, button, UI element}
		keystroke "v" using {command down}
		keystroke "s" using {shift down, command down} # Save As
		repeat until exists sheet 1
			delay 0.1
		end repeat
		tell sheet 1
			class of UI elements --> {group, button, button, button, group, UI element, text field, static text, static text, text field}
			--keystroke "d" using {command down, shift down} # save to Desktop # DISABLED
			name of buttons --> {"Save", "New Folder", "Cancel"}
			click button 1 -- "Save"
			set cnt to 10
			set pass to 0
			repeat
				set pass to pass + 1
				delay 0.1
				if exists sheet 1 then exit repeat
				if pass = cnt then exit repeat
			end repeat
			if pass < cnt then
				tell sheet 1
					class of UI elements
					# May be : the file already exists --> {image, static text, static text, button, button}
					name of buttons --> {"Replace", "Cancel"}
					click button 1 --(or 2 if you wish to cancel
				end tell
			end if
		end tell
		delay 0.1
		set cnt to 10
		set pass to 0
		repeat
			set pass to pass + 1
			delay 0.1
			if exists sheet 1 then exit repeat
			if pass = cnt then exit repeat
		end repeat
		if pass < cnt then
			tell sheet 1
				class of UI elements --> {static text, image, static text, button, button, button}
				name of buttons --> {"Save", "Cancel", "Don't Save"}
				click button 1
			end tell
		end if
		keystroke "w" using command down
	end tell # window 1
end tell # System Events & process

Yvan KOENIG running Sierra 10.12.2 in French (VALLAURIS, France) vendredi 20 janvier 2017 21:14:48

Thanks Ivan
it works perfectly
However I have very many files in one folder
How to save them in the original folder
and not in the desktop
Thanks again

Just curious:

As far as I remember TextWrangler has got a pretty comprehensive AppleScript dictionary.
Isn’t there a native way to do that?

Thanks Stefan
but unfortunately there is no such thing
or I am not skilled enough to find it

Just one instruction to disable.

I edited the script accordingly.

Here is a code doing the job without using GUIscripting.

tell application "TextWrangler"
	activate
	set itsPath to file of document 1 as text
	select insertion point before line 2 of document 1
	select line 2 of project window 1
	copy selection
	
end tell

set splittedPath to my decoupe(itsPath, ".")
set theBeg to my recolle(items 1 thru -2 of splittedPath, ".")
set newPath to (theBeg & "_new." & last item of splittedPath) as «class furl»
tell application "TextWrangler" to save document 1 to newPath



#=====

on decoupe(t, d)
	local oTIDs, l
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set l to text items of t
	set AppleScript's text item delimiters to oTIDs
	return l
end decoupe

#=====

on recolle(l, d)
	local oTIDs, t
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set t to l as text
	set AppleScript's text item delimiters to oTIDs
	return t
end recolle

#=====

Yvan KOENIG running Sierra 10.12.2 in French (VALLAURIS, France) vendredi 20 janvier 2017 21:29:10

Thanks

danwan,

StefanK is correct that TextWrangler has an applescript dictionary, there is no reason to be doing all this user interface scripting. UI scripting should be a last resort, it is very clunky and error prone compared to using actual script commands.

How did you try to find the dictionary? In Script Editor, if you go to the “File” menu and choose “Open Dictionary,” is TextWrangler not listed?

*** Edit*** - had not seen that Yvan had already revised the script to avoid the UI scripting.