The script in a file was wiped out during a save

Mark & Shane,

I was writing a script get the contents of Safari history from the the web page after I did a “Show All History” from the History menu of Safari 10.0.2. I am running the released Script Debugger 6.0.4 (6A198). I was using UI Browser 2.5.0 to script the interface of the Safari window. The script script was to read the history from safari and put that history in Apple’s Numbers. It was reading the history with no problems before the the saving problem. I am running under 10.11.6 El Capitan. I had no indication of any problems with anything while doing this until the contents of the window disappeared and a dialog showed up after I tried to save the script by pressing command-S. I did include of a picture of the dialog in this post but the dialog said something about global variables or properties had exceeded AppleScripts limits (errOSAInternalTableOverflow). I also included the text of the script I was working on.

I check the console and no errors or any kind of status appeared there. The I clicked the “Recompile & save” buttons and I got the spinning beach ball for a while, maybe a minute to a minute and a half. Then it saved. Then I pressed command-Z to undo until I got my original script back. I copied my script from the window to another application with no problems. Then I went to Finder and double clicked the file and a blank window opened in Script Debugger. There had been something in the file before. I quit Script Debugger and restarted it and tried opening the file from both Finder and from Script Debugger and the text I typed was still gone.

After I clicked the “Save & compile” button something did come up in in the console. It said “2/5/17 1:45:22.242 AM Script Debugger[3884]: void CGSUpdateManager::log() const: conn 0x1b167: spurious update.” I am not sure what to make of that that. It doesn’t sound like it’s related but I thought I would include it anyway.

I did have Script Debugger in debug mode while I was working on the script but I had not used the debug function to step through any thing for about a half an hour before the problem occurred. I used the debugger to watch the script and make sure it was really doing what I wanted it to. The script was not having any problems before I ran the debugger.

I did upload a zip version of the file that was blanked out after the save. I’m not sure what information you can get from that but I figured I would in case you thought of something you wanted to check.

Normally I quit “UI Browser” as soon as I get all the information I need for the script. Then I start writing the script. But this time I forgot and left “UI Browser” running while I wrote the script. This might have caused the problem. “UI Browser” does strain the application a bit when it’s running. But at the same time there are probably a number of people who use UI Browser with Script Debugger.

While it did destroy the file contents but the text I retrieve from pressing undo did get the full text of my script. I tried to include everything I could think of. But it well of been the case that “UI Browser” was interacting with Script Debugger when the problem occurred, although Script Debugger was not acting sluggish before the problem. Unfortunately in this case I have no idea if “UI Browser” caused this problem.

Bill

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions


on GetNumberOfRows()
	set AppName to "Safari"
	tell application AppName to activate
	tell application "System Events"
		tell process AppName
			set NumberOfRows to count of rows of outline 1 of scroll area 1 of group 1 of group 1 of tab group 1 of splitter group 1 of window 1
		end tell
	end tell
end GetNumberOfRows

on GetFieldValue(TheField, TheRow)
	script ReturnObj
		property FieldValue : -1
		property Successful : false
		property DisclosureTriangleLine : false
	end script
	
	set AppName to "Safari"
	tell application AppName to activate
	tell application "System Events"
		tell process AppName
			try
				if ((TheField ≠ 1) and (TheField ≠ 2)) then
					display dialog "An invalid field number of \"" & TheField & "\" was sent to the GetFieldValue handler." buttons {"OK"} default button "OK" with title "Error"
					set (Successful of ReturnObj) to false
					return ReturnObj
				end if
				
				if (exists (text field 1 of group 1 of row TheRow of outline 1 of scroll area 1 of group 1 of group 1 of tab group 1 of splitter group 1 of window 1)) then
					-- This is a line with the date and disclosure triangle on it
					set DisclosureTriangleLine to true
					if (TheField = 1) then
						set (FieldValue of ReturnObj) to value of text field 1 of group 1 of row TheRow of outline 1 of scroll area 1 of group 1 of group 1 of tab group 1 of splitter group 1 of window 1
					else if (TheField = 2) then
						set (FieldValue of ReturnObj) to ""
					end if
				else
					set DisclosureTriangleLine to false
					set (FieldValue of ReturnObj) to value of text field TheField of row TheRow of outline 1 of scroll area 1 of group 1 of group 1 of tab group 1 of splitter group 1 of window 1
				end if
				set (Successful of ReturnObj) to true
				return ReturnObj
			on error errMsg number errNum from offendingObject partial result resultList to expectedType
				display dialog "Error " & (errNum as string) & " occured trying to get the value of field " & TheField & " in row " & TheRow & "." & return & return & errMsg ¬
					buttons {"OK"} default button "OK" with title "Error"
				display dialog "Error " & (errNum as string) & " occured." & return & return & errMsg ¬
					with title "Error"
				set (Successful of ReturnObj) to false
				return ReturnObj
			end try
		end tell
	end tell
end GetFieldValue


return GetFieldValue(1, 2)

set NumberOfRows to GetNumberOfRows()


set ListOfLines to {}
repeat with TheLine from 1 to 3
	-- Get the value for field 1
	set TheResult1 to GetFieldValue(1, TheLine)
	if (not Successful of TheResult1) or (not Successful of TheResult1) then return false
	
	-- Get the value for field 1
	set TheResult2 to GetFieldValue(2, TheLine)
	if (not Successful of TheResult2) then return false
	
	if (DisclosureTriangleLine of TheResult1) ≠ (DisclosureTriangleLine of TheResult2) then
		display dialog "message" buttons {"Cancel", "OK"} default button "OK"
	end if
	
	DisclosureTriangleLine
	FieldValue
	
	set LinePart1 to GetFieldValue(1, TheLine)
	set LinePart2 to GetFieldValue(2, TheLine)
	set OutputLine to {Title:LinePart1, Adress:LinePart2, DisclosureTriangleLine:IsDisclosureTriangleLine, ErrorOccured:ErrorDidOccur}
	if (ListOfLines = {}) then
		set ListOfLines to {OutputLine}
	else
		set ListOfLines to ListOfLines & {OutputLine}
	end if
end repeat

Safari 10.0.2 list out history.scpt.zip (246.8 KB)

Did you try “Recover Damaged Script…” from the file menu?

It doesn’t work in the script you attached, but that may be due to stuffing and unstuffing.

I tried running your script multiple times on my system with no crashes or errors. But it didn’t seem to do anything.

Ed

–>Script Debugger 6.0.4 (6A198) on OSX 10.11.6

Ed,

Using “Recover Damaged Script…” just opens an empty file. The contents of the script are gone.

In this post I enclosed an image of a the entire contents of the file as hex (the first image) and as text (the second image). When you look at it you can see there is no real script there. The second image is what you would see if you opened a compiled script from a text editor.

The last image is the script recovered by using undos and saving to a new file. Again this is what you would see if you opened the script in a text editor. I included the last one so you can see what the script should have looked like. Comparing the second and third images you can see there is a lot missing. Compiled script always have the names for variables in them as well as the straight double quoted text used in the script. Basically little bits of the english readable script are scattered throughout a compiled script. When you look at the middle image you can see none of that is there. That’s is why I can said the script is not there. The third image is pretty compressed. You might need to click the image in the post to read the contents or download the third image.

Bill

Got it. When it works Recover Damaged Script opens the script’s preview, not the contents of the script so it’s always worth a try. But, if it doesn’t work here, it doesn’t work.

Ed,

My script was a compiled script. So there was no preview image to recover from.

Bill

If you ever saved it, it will have a preview, even if it’s a compiled script.

You can test that by selecting Recover Damaged Script and navigating to any compiled, saved script. (It won’t change the script itself, it opens the preview in a new SD window.

That said, I just tried it with a few different scripts and it worked with all but one. The one script it didn’t work in was saved in debugging mode (as was yours, right?) I’ll try a few more times, but I can see the preview in finder and in the navigation window? It should work, if it doesn’t that’s the bug.

Do you have time machine? If you’re lucky your script might be there preview intact.

Yup, I think you’ve found a bug, Bill.

Some (not all) of the scripts I’ve saved in debugging mode will not open a recovered version. Even if I can see the preview in finder and the navigation window.

The script that you attached to your message does have a preview that should open if you choose Recover Damaged Script, but it doesn’t. If you look closely after selecting Recover Damaged Script the window flashes and sometimes you get the spinning pizza, but the result is an empty window.

For the script’s saved in debugging mode that will open, there always seems to be a pause and a short hang, and then it opens.

Ed,

Ok you got me on that one. The “Quick Look” feature in Finder does work on my bad script. If I select the messed up script in Finder and press the space bar the full text of the script comes up in Finder even though the script appears to be empty in Script Debugger.

I looked in the Script Debugger manual and it says right in the manual “Script Debugger contains a ‘Quick Look’ generator for compiled script files and applets.” So after opening the script in “Quick Look” from the Finder I clicked the “Open with Script Debugger” button in the top right of the “Quick Look” window but the page that opened in SD was blank.

So I looked for resources in the script file. There were resources. There was a ‘CFLD’, ‘CWst’, ‘DBGh’, ‘icns’, ‘TEXT’ resources and to my surprise there is a 'RTF ’ resource called "02000 ‘QuickLook Preview’ " All the script text was there in the 10,970 byte "RTF " resource. So if this is where the “Recover Damaged Script…” was looking to recover the file it should have worked. If anyone is curious I used an app called “rezycle” from the App Store to read the resources from the file.

So the compiled script got wiped out but the “Rich Text Format” version used for “Quick Look” still had the text. It is strange that the “Quick Look” generator could read the RTF info but the “Recover Damaged Script…” could not read it.

Bill

The Recover Damaged Script... command uses a TEXT Source Text resource, not the RTF resource used for QuickLook. The Source Text resource is present in your file and valid, but it contains just an empty string.

So yesterday when I was toying with this it seemed that scripts saved in debugging mode, that hadn’t been previously saved in normal mode and had been stepped through would open a blank page on Recover Damaged Script.

However, my mac was running very slowly, which may or may not have been related to SD, and I had to force a restart. After restarting, those same script opened correctly on Recover Damaged Script, but Bill’s script did not.

Shane,

I see the ‘TEXT’ resource called 02001 “Source Text”.txt in the file. I looked at other compiled scripts and the text was in there but I did come across a few compiled scripts files that did not have that particular resource in the file but they were all scripts saved that had no text in the file (i.e. for some reason I saved a script without entering text.)

Is it correct for for the resource to be missing if the save was done with no text in the script window?

I was going to poke around and see if I can find another compiled script with an empty resource. Do you have any suggestions what I could check to see if my Mac caused the problem?

Bill